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;
}
Arkanosis
  • 2,229
  • 1
  • 12
  • 18
Mudit Kapoor
  • 351
  • 2
  • 3
  • 9

5 Answers5

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!

raymelfrancisco
  • 828
  • 2
  • 11
  • 21
1

Try this code

fs.open("fsfile2", ios::app|ios::in|ios::out|ios::binary);
Akim
  • 136
  • 1
  • 9
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.

Community
  • 1
  • 1
anatp2015
  • 170
  • 9
1

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

Semen Tykhonenko
  • 312
  • 3
  • 14
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.

Andreas DM
  • 10,685
  • 6
  • 35
  • 62