0

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?

5
Contributors
14
Replies
15
Views
7 Years
Discussion Span
Last Post by ankit1990rana
0
ofstream f(filename, ios_base::out | ios_base::app);

iosbase::app means that you want to append to the file.

0
ifstream in;
in.open("filename",std::ios::app);
in.seekg(in.tellg(),std::ios::end);//this places the file pointer at the end
0
ifstream in;
in.open("filename",std::ios::app);
in.seekg(in.tellg(),std::ios::end);//this places the file pointer at the end

I don't think the last line is necessary -- once you've opened it with ios::app, every write seeks to the end.

0

Who said anything about writing?In fact .. there's no sense in moving the file pointer at the end of an input stream , it's just an example.

0

Writing is implied. The behavior described is default behavior for an output file stream (an ofstream).

When you open an output file, it's default mode is ios::out|ios::trunc. An input file stream (ifstream) doesn't behave like that, its default mode is ios::in. To prevent this default output stream behavior, you need to change the output mode from truncate to append. To do that, you need to override the default value applied to the mode parameter by the function definition. The new mode value needed is ios::out|ios::app.

Edited by Fbody: n/a

0

Yes you're right sorry for that.ios::app doesn't make sense in input stream.In an input stream the default position is the beginning of the file.But why would the second line be an invalid example?

0

There are 2 types of "pointers" into files; a put pointer used for output and a get pointer used for input.

An input file stream (ifstream) relies on a get pointer. If you place a file's get pointer at the end of the file, it doesn't matter how, or how many times, you try to read the file, you'll always get EOF instead of any useful input. You'll have to do a seekg() to reposition the get pointer and begin getting anything useful.

Edited by Fbody: n/a

0

Thank you everyone for helping me out with it. It works for now.

0

I 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();
      }

 }
0

@arkoenig ,sir...how to add new data..without replacing previous one in another line?..in a file.,iostream..c++

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.