LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   c++ append data to file (https://www.linuxquestions.org/questions/programming-9/c-append-data-to-file-339528/)

blizunt7 07-03-2005 03:02 AM

c++ append data to file
 
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

cout << "somewhere" << endl;
fout << "hello world" << endl; // ISSSUUUEEEEE
// itemList << *next << ":" << endl;
item[++lastItem].setNumber(next);

cout << "last item " << lastItem << " itemget " << item[lastItem].getNumber() << endl;

}
fout.close();
}

rjlee 07-03-2005 09:40 AM

Re: c++ append data to file
 
Quote:

Originally posted by blizunt7
Code:

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.

blizunt7 07-03-2005 02:10 PM

wow, thanks so much, WHo knew, hours of debugging, and its a simple memory issue.

haha


thanks again.

Josh

enemorales 07-04-2005 12:47 AM

Well, then we have learned that memory issues are not so simple ;)


All times are GMT -5. The time now is 04:56 AM.