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?

ofstream f(filename, ios_base::out | ios_base::app);

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

By 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.

Look through this page for more info.

ifstream in;
in.open("filename",std::ios::app);
in.seekg(in.tellg(),std::ios::end);//this places the file pointer at the end
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.

You're right.But this is read.

This is an output question, you can't write/output to an input file stream...

You're right.But this is read.

Really? What leads you to that conclusion?

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.

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.

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?

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.

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

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

 }

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

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.