File Handling C++

The line of code given below is not making a new file named file7.cpp. And hence it is showing the value of tellp and tellg as -1 and seekg and seekp does not work. Moreover, if I add ios::app or ios::trunc then the file if being made and the program works perfectly. Wy is this?

 
    finout.open("file7.txt", ios::in | ios::out);
closed account (SECMoG1T)
use
1
2
3
4
5
6
7
std::fstream///use this

//like this
std::fstream file("help.txt");
http://www.cplusplus.com/reference/fstream/fstream/
http://www.cplusplus.com/reference/fstream/ifstream/
http://www.cplusplus.com/reference/fstream/ofstream/ 


reading list:
https://www.uow.edu.au/~lukes/TEXTBOOK/notes-cpp/io/readtextfile.html
http://www.cplusplus.com/doc/tutorial/files/
I am working in std namespace, so I don't think that is the problem.
If you want to know "why" then you need to read up on the various parameters used when opening an fstream.

http://www.cplusplus.com/reference/fstream/fstream/open/
http://en.cppreference.com/w/cpp/io/basic_fstream/open

and also the links already given.

It depends what you want to do. If you only want output, just use an ofstream,
 
    std::ofstream finout("file7.txt");


If you need to both read and write to the same file in the same program, it may be useful to use fstream rather than ofstream. If you intend to read, the file should already exist. But you can force it to create a file if it doesn't exist with particular combinations of openmode as you already found.



Topic archived. No new replies allowed.