I can't write to the file after reading completes! Why?
Recently a question appeared in MSDN Visual C++ forum regarding the mess ups happned in reading and writing to file. The problem was very simple. I read the file till the end, after that I tried to write to the same file using the same pointer. there’s no problem with file open and it’s rights. What could be the problem.
as the question is simple, the answer too and it should have taken care by the creator of C++ libraries.
The following was my answer to the post.
int main(void){
fstream myfile(“a.txt”,ios::in|ios::out|ios::app);
string line;
//read and go to end of file
while (! myfile.eof() ){
getline(myfile,line);
cout<<line<<endl;
}
myfile.clear(); // clear the flags (eof)
myfile.seekp( ios::end );
/*write to file*/
char *line1=”testing line1″;
char *line2=”testing line2″;
myfile.write(line1,15);
myfile.write(line2,15);
}
eof means, it set as the error flag to the file stream and it will check this flag before do any read or write operation. You have to clear it using clear function
about 2 years ago
Hello,
My name is kartik.I recently stumbled on your blog while searching for some thing on google.I found your articles interesting and when i read the ‘About me ‘ section on ur blog,I realised that we both share very similar interests.
Currently I am pursuing my B.tech(cse) from Ip university,Delhi.I have completed my second year and right now I am pursuing In house training in my college,where we are required to make a project.
Like you ,i have a keen interest in C++ and very much believe in its power.I have thought of making a lan chat messenger using MSDN visual c++ .But the problem is that I have no previous experience of working with visual c++(although I have been working on turbo c++ for quite sometime now).
I would be really grateful to you if you could guide me how to start,where to learn visual c++ from.
One more issue that concerns me right now,is the choice of selection of the version of visual c++.Visual studio 2005 Express(which contains vc++ 8.0) is available for download free from MSDN site.I have downloaded it and tried to play with some of its features.The problem is that I have a book called ’sams teach yourself Vc++ 6.0 in 21 days ‘ and when I started using this book for working with vc++ 8.0, i found out that there were some changes.So I am a little confused,wether to download and use Vc++ 6.0 or continue working with Vc++ 8.0 .Are there any major differences in these 2 versions?And as I am going to need to implement networking for lan messenger,which version do you suggest ?
I know I have written this long letter,but believe me I am really in need of urgent help right now. Please reply,as per your convenience,as soon as possible.Please reach me at kartiksatija@gmail.com or drop a comment at my blog http://technogeekzz.blogspot.com/ .I would be very very grateful to you.Waiting for your reply very eagerly.
Thanking You,
Kartik Satija.
about 2 years ago
Hey
I was surfing the web and i saw this site, pretty cool.
Currently im running and adult site:Reachton
k, just want to say hi
Can i link you from my site? im looking for quality content like yours. If no let me know if i can add u in exchange for a montly fee or something.
about 2 years ago
Hey
I was surfing the web and i saw this site, pretty cool.
Currently im running and adult site:Reachton
k, just want to say hi
Can i link you from my site? im looking for quality content like yours. If no let me know if i can add u in exchange for a montly fee or something.
about 2 years ago
Nice Site!