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. |
| 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
07-03-2005, 03:02 AM
|
#1
|
|
Member
Registered: Mar 2004
Distribution: Fedora Core 1,2,3, RHEL3,4,5 Ubuntu
Posts: 272
Rep:
|
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();
}
|
|
|
|
07-03-2005, 09:40 AM
|
#2
|
|
Senior Member
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,990
Rep:
|
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 , 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.
|
|
|
|
07-03-2005, 02:10 PM
|
#3
|
|
Member
Registered: Mar 2004
Distribution: Fedora Core 1,2,3, RHEL3,4,5 Ubuntu
Posts: 272
Original Poster
Rep:
|
wow, thanks so much, WHo knew, hours of debugging, and its a simple memory issue.
haha
thanks again.
Josh
|
|
|
|
07-04-2005, 12:47 AM
|
#4
|
|
Member
Registered: Jul 2004
Location: Santiago, Chile
Distribution: Ubuntu
Posts: 410
Rep:
|
Well, then we have learned that memory issues are not so simple 
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 02:14 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|