is there any other way to open a file for append output other than:
??Code:ofstream afile("filename",ios::app);
is there any other way to open a file for append output other than:
??Code:ofstream afile("filename",ios::app);
CreateFile() and FILE
im sure there are some other way, if you could be more specifc in
what you where trying to accomplish youd get more info.
opening a file in append mode so I can write info into it, only reason i m asking is because ios:app is hard to remember sometimes.
just other choices for opening a file that might be easier to remember.
as well, later on i want to say.. write to the file the using information from a class.
as well input from teh file will automatically place into the class' variable
Last edited by hoangvo; 07-23-2005 at 01:34 AM.
well, you could do this:it's essentially the same thing.Code:#include<iostream> #include<fstream> int main() { std::fstream file("test.in",std::ios::out|std::ios::app); file<<"\nHello World"; file.close(); return 0; }
my point: you're going to have to memorize things to learn programming. ios::app is one of the easier things to remember. just remember that you're appending things, not truncating the file.
to demonstrate my point, here's one way of reading and writing data from a struct to a file:Code:#include<iostream> #include<fstream> int main() { struct myStruct { int a; int b; char c; } test; test.a=50; test.b=60; test.c='A'; /* open a binary file for reading and writing by truncating */ std::fstream file("test.in",std::ios::binary|std::ios::in|std::ios::out|std::ios::trunc); /* write to the file */ file.write(reinterpret_cast<char*>(&test),sizeof(myStruct)); /* then to read from it, starting at the beginning */ file.seekg(0,std::ios::beg); file.read(reinterpret_cast<char*>(&test),sizeof(test)); /* close the file */ file.close(); /* output what you just read */ std::cout<<"int a: "<<test.a<<"\nint b: "<<test.b<<"\nchar c: "<<test.c<<std::endl; return 0; }
Last edited by major_small; 07-23-2005 at 02:11 AM.
Join is in our Unofficial Cprog IRC channel
Server: irc.phoenixradio.org
Channel: #Tech
Team Cprog Folding@Home: Team #43476
Download it Here
Detailed Stats Here
More Detailed Stats
52 Members so far, are YOU a member?
Current team score: 1223226 (ranked 374 of 45152)
The CBoard team is doing better than 99.16% of the other teams
Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)
Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT
Thank you,
I got it to work via binary method.
its a bit to remember but as i constantly type it i will eventually remember.
its abit confusing as well cause of reinterpret_cast<char*>(&variable)
can u explain that part?
i know that read accepts an array of char
how does it re-cast into a char array from class/struct type?
it basically turns the struct into an array of bytes and writes those bytes to the file as characters.
Join is in our Unofficial Cprog IRC channel
Server: irc.phoenixradio.org
Channel: #Tech
Team Cprog Folding@Home: Team #43476
Download it Here
Detailed Stats Here
More Detailed Stats
52 Members so far, are YOU a member?
Current team score: 1223226 (ranked 374 of 45152)
The CBoard team is doing better than 99.16% of the other teams
Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)
Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT