In C++ it seems like every single time you open a file, the inner contents get deleted quickly. Is there a way to open the file at the end of the last character while keeping all contents within the file? I've been looking at something with the End of file function but I don't know if that's the right way to go. Any advice?
Kirielson
0
Light Poster
Recommended Answers
Jump to Postofstream f(filename, ios_base::out | ios_base::app);
iosbase::app means that you want to append to the file.
Jump to PostBy default, an output file opens in "truncate" mode which dumps all of the file's current contents and starts the file over. To prevent this, you need to use "append" mode.
Jump to Postifstream in; in.open("filename",std::ios::app); in.seekg(in.tellg(),std::ios::end);//this places the file pointer at the end
Jump to PostI know exactly what you are saying.But here.. you can go backwords too ;)).
#include <iostream> #include <fstream> int main () { std::ifstream in("asd.cpp",std::ios::in); in.seekg(in.tellg(),std::ios::end); int posit=in.tellg(); while (posit--) { in.seekg(posit); std::cout << (char) in.get(); } }
All 14 Replies
arkoenig
340
Practically a Master Poster
Fbody
682
Posting Maven
Featured Poster
caut_baia
9
Posting Whiz
arkoenig
340
Practically a Master Poster
caut_baia
9
Posting Whiz
Fbody
682
Posting Maven
Featured Poster
arkoenig
340
Practically a Master Poster
caut_baia
9
Posting Whiz
Fbody
682
Posting Maven
Featured Poster
caut_baia
9
Posting Whiz
Fbody
682
Posting Maven
Featured Poster
Kirielson
0
Light Poster
caut_baia
9
Posting Whiz
ankit1990rana
0
Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.