简体   繁体   中英

ofstream and ifstream path works writing not reading in C++ Builder

I can create a text file as follows using ofstream, to the specified path:

std::string path = "c:\users\john\file.txt";
std::string str = "some text";

ofstream myfile;
myfile.open (path);
myfile << str; // write string to text file
myfile.close();    //close file

When I try to open/read the file the system seems to open the file but throws the "no data found" exception here...even though the file is there and contains the text.


std::string line = "";
std::string str = "";
std::string path = "c:\users\john\file.txt";

ifstream file (path);  
if (file.is_open())
{
   while ( getline (file,line) )
   {
      str = str + line;
   }

   file.close();

   if (str == "")
   {
      throw(Exception("Error: No data found..."));
   }
}

else

throw(Exception("Error: File not found..."));

This only seems to happen when trying to read from some location other than the debug folder...

So if I can create the file in the user directory why can't I read it??

Can anyone help?

UPDATE:

I just discovered that if the write function is run and the read function is run right after that with the app still running it works. If however the write function is run then the app closed and reopened the read function then fails as described above.

Use a single forward slash (/) instead.

Writing operations on text files are performed in the same way we operated with cout:

// writing on a text file
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  ofstream myfile ("example.txt");
  if (myfile.is_open())
  {
    myfile << "This is a line.\n";
    myfile << "This is another line.\n";
    myfile.close();
  }
  else cout << "Unable to open file";
  return 0;
}

Reading from a file can also be performed in the same way that we did with cin:

// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  string line;
  ifstream myfile ("example.txt");
  if (myfile.is_open())
  {
    while ( getline (myfile,line) )
    {
      cout << line << '\n';
    }
    myfile.close();
  }

  else cout << "Unable to open file"; 

  return 0;
}

Your writing code fails silently, you should change that to

std::string path = "c:\users\john\file.txt";
std::string str = "some text";

ofstream myfile;
myfile.open (path);
if(myfile.is_open()) // <<<<<<<
    myfile << str; // write string to text file
}
else {
    std::cout << "Cannot open file." << std::endl;
}
myfile.close();    //close file

Your primary problem is that backslashes in string literals need to be escaped:

std::string path = "c:\\users\\john\\file.txt";
                   // ^      ^     ^

so the file couldn't be opened for writing at all, and you didn't notice that, because your code never checked about it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM