0

I am trying to run below code but it is neither showing any file on the path nor reading anything from it. Whatever I am writing into the file through "cin >>" it is not being written. Can anybody please let me know mistake in my code below:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    char string[80];

    cout << "Enter Input" << endl;
    cin >> string;

    int len = strlen(string);

    fstream file;
    file.open("TEXT", ios::in | ios::out);

    for (int i = 0; i < len; i++)
        file.put(string[i]);

    file.close();
    file.open("TEXT", ios::in | ios::out);

    file.seekg(0);

    cout << "Output" << endl;
    while (file) {
        char ch;
        file.get(ch);
        cout << ch;
    }

    file.close();
    return 0;

}
  • 1
    Do you get any errors? Are you able to write to the string? Have you tried stepping it through with a debugger? – Ted Klein Bergman Jul 04 '20 at 19:34
  • 2
    `file.open("TEXT", ios::in | ios::out);` fails if the file does not already exist. I can't help but notice that your program is not doing any error handling. – Igor Tandetnik Jul 04 '20 at 19:36
  • Duplicate of https://stackoverflow.com/questions/23967697/if-file-exist-work-with-it-if-no-create-it ? – cocool97 Jul 04 '20 at 19:38

2 Answers2

1

You should add the fstream::app flag in your open call and this will do the trick !

cocool97
  • 1,201
  • 1
  • 10
  • 22
  • Thanks cocool97. It's working by adding ios::app during opening of file. But why is it so ? Opening a file in append mode is working here. But I am not really appending anything here. Then why need to add it ? – Navneet Goel Jul 04 '20 at 19:58
  • Well you aren't inputting anything either, but you're apparently happy to use `ios::in`. Then when you are inputing something you use `ios::out`. Like I said, in my answer don't use file modes you don't really need. But the link I gave in my answer explains the difference between `ios::in | ios::out` and `ios::in | ios::out | ios::app`. – john Jul 04 '20 at 20:01
1

Don't mess with file modes you don't need (or understand). Open the file for writing only first, and then open it for reading only next. Use different streams for reading and writing.

ofstream file_out;
file_out.open("TEXT");
for (int i = 0; i < len; i++)
    file_out.put(string[i]);
file_out.close();

ifstream file_in;
file_in.open("TEXT");
cout << "Output" << endl;
while (file_in) {
    char ch;
    file_in.get(ch);
    cout << ch;
}

Unless you actually understand the rules concerning ios::in and ios::out it's safer to just use ifstream when you want input and ofstream when you want output.

More reading if you do want to understand the rules.

john
  • 85,011
  • 4
  • 57
  • 81
  • Thanks John. Yes it works by making separate ifstream and ofstream objects. But fstream is a common class for both of them so I am not able to understand why isn't that working. – Navneet Goel Jul 04 '20 at 19:59
  • Because like Igor explained above (and explained on the page I linked to) `ios::in | ios::out` does not create a file where none exists. – john Jul 04 '20 at 20:04
  • If you do want to use just one stream object, then at least use only the modes you need. `ios::in` for reading and `ios::out` for writing. – john Jul 04 '20 at 20:06