0

Can anyone tell me what is wrong with this code? I always get not open.

#include <iostream>
#include <fstream>

using namespace std;

int main(){
    fstream fs;
    fs.open("fsfile2",ios::in|ios::out|ios::binary);
    if(fs.is_open()){
    fs.write("wow",sizeof("wow"));
    char str[20];
    fs.read((char*)str,sizeof(str));
    cout<<str<<endl;}
    else 
        cout<<"Not open\n";
    return 0;
}
5
  • 2
    Apart from the indentation, which makes it unnecessarily hard for us to read it? Commented May 13, 2015 at 15:41
  • 1
    And you are sure that fsfile2 resides in the same directory where your program is running? Commented May 13, 2015 at 15:42
  • 1
    Does the file already exist? If not, then add ios::app or ios::trunc to the flags. Commented May 13, 2015 at 15:46
  • No. i think that is the problem. Thank you. By the way can you give me indentation tips. I am a beginner. Commented May 13, 2015 at 15:48
  • 3
    Your fs.read will try to read what's after the "wow" you just wrote. If you want to read the "wow" back, you need to seek back to the file start, or close & reopen. Commented May 13, 2015 at 16:09

5 Answers 5

1

Try this code

fs.open("fsfile2", ios::app|ios::in|ios::out|ios::binary);
Sign up to request clarification or add additional context in comments.

Comments

1

By using the open() like you are that file will not be created if that is your goal. If you want to create a new file please look at: fstream won't create a file

If the file exists, you are not looking for it in the right path. Or change the file name to the full path or put the executable in the folder where the file is.

Hope this helps.

Comments

1

Probably, you do not have permissions to create files in the directory, where your executable is.

Comments

1

Solution:

Please add a file extension to the filename. If it's a text file, it will be

"fsfile2.txt"

Then, I tried removing

ios::in

since the first process only writes to file, and by removing that, the file is created and "wow" is also written at it.

In order for these lines

fs.read((char*)str,sizeof(str));
cout<<str<<endl;

to work, You need to close the stream after writing to it, then open the stream in read mode, then read the contents. Take note that closing the stream will save the edited file.

Additional:

You can also change

fs.write("wow",sizeof("wow"));

to

fs << "wow";

You can do the same when reading from file,

fs >> str;

You can also use the string class of C++, instead of char array so that the number of characters inside the file won't be your problem anymore.

#include <string>
string str;

Checking for EOF (end-of-file) is recommended since files are read line by line. Once you add a new line and add a character to the line, the code that doesn't loop until EOF will only read the first line of the file.

In order to solve this, it is recommended to loop until EOF is reached.

while(!fs.eof()) {
     fs >> str;
     cout << str << endl;
}

So here is the improved snippet:

#include <string>

fs.open("fsfile2.txt", ios::out); // ios::out for write only
if(fs.is_open()) {

    // writes "wow" to file
    fs << "wow"; 

    // closes the file
    fs.close();

    // ios::in for read only
    fs.open("fsfile2.txt", ios::in); 

    // better to define variable just before using it
    string str;

    // loops until end-of-file
    while(!fs.eof()) {

        // reads a line from file, stores it to str
        fs >> str;

        // shows str to screen
        cout << str << endl;
    }
}

*Note: I removed

ios::binary

Since your code is not dealing with binary files yet.

I tried these and it worked fine! Have a nice day!

Comments

1

fstream fs; does not create a new file for you.
You need to make sure that the file exists in your project directory.
On the other hand, if you were to use ofstream fs("file.txt"); it would create the file for you. Or use only ios::out when you open fstream fs, this will create the file for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.