ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hey all, tryin a lil something, and its not working.
I have this code, which i want to be able to write to (append to end) of a file.
My issue is actually writing to the file. This code, and the file to write are in the same directory, the file to write to both the owner and group have RW permissions. I have opened the file in append mode, so i keep previous data, and just continue writting on the last line.
I also want to be able to insert like..word by word..
so at the end of each:
fout << "write this string to file"; // notice no << endl;
i do NOT want to end the line, and start a new line. BUt this is not my issue now. WHen i run this i get to the line before that outputs to the konsole, "somewhere", the next line, which writes to the file, i get a Segmenation fault.
ANy ideas, would be great!!! thanks so much
JOsh
void newItem()
{
char cont;
ofstream fout; // object for writing to file itemList
fout.open("data.gp",ios::app);
if (!fout.good())
{
cout << "Error Creating file\n";
}
assert (!fout.fail());
cout << "Continue with next number (Y/N)? ";
cin >> cont;
cout << endl;
if (cont == 'Y'||'y')
{
int nextItem = lastItem+1+(4000);
//cout << "nextItem: " << nextItem << endl;
char *next;
sprintf(next,"%d",nextItem); // convert from int to char
char *next;
sprintf(next,"%d",nextItem); // convert from int to char
A segmentation fault is generated when you write to an area of memory that doesn't belong to your program.
When you declare a primative (i.e. non-object) variable, such as next, it can contains any value until the point where it is assigned to. That's why it's always a good idea to initialise your variables.
next is not initialised here; it could point to anywhere in your memory address space. You then attempt to write to that location (which you don't know where it is), which is probably not available to your program, causing a segmentation fault.
The C++ compiler that comes with GCC will issue a warning about this if you use the -Wall option, which is usually a good idea.
You could fix this using
Code:
char next[N]
, where N is defined as a number big enough to hold any int in a string, plus a leading whitespace character. This value will vary between computers, so that's not a good solution. Also, the way that numbers are formatted varies from country to country (the British use comma as a thousands-seperator whereas the French use it as a decimal point).
This is not the easiest way to write a number to a stream (or a string); you probably want to look at the std::num_put class defined in <locale>; this can be used to write numbers to a stringstream if you like.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.