Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 07-03-2005, 04:02 AM   #1
Registered: Mar 2004
Distribution: Fedora Core 1,2,3, RHEL3,4,5 Ubuntu
Posts: 274

Rep: Reputation: 30
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

void newItem()
char cont;

ofstream fout; // object for writing to file itemList"",ios::app);
if (!fout.good())
cout << "Error Creating file\n";

assert (!;

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;

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

Old 07-03-2005, 10:40 AM   #2
Senior Member
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,991

Rep: Reputation: 76
Re: c++ append data to file

Originally posted by blizunt7
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
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.
Old 07-03-2005, 03:10 PM   #3
Registered: Mar 2004
Distribution: Fedora Core 1,2,3, RHEL3,4,5 Ubuntu
Posts: 274

Original Poster
Rep: Reputation: 30
wow, thanks so much, WHo knew, hours of debugging, and its a simple memory issue.


thanks again.

Old 07-04-2005, 01:47 AM   #4
Registered: Jul 2004
Location: Santiago, Chile
Distribution: Ubuntu
Posts: 410

Rep: Reputation: 31
Well, then we have learned that memory issues are not so simple


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
pipe output to append to a text file davee Linux - Newbie 5 03-22-2016 08:44 PM
> writes a new file, how to append? luwigie Linux - General 1 08-26-2004 09:42 PM
radius append/add data ethanchic Linux - Software 0 04-16-2003 03:05 AM
tar append file juno Linux - General 5 10-24-2002 03:59 AM
append to file d-rez Linux - Newbie 2 06-20-2002 05:55 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:01 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration