1

I'm currently doing some schoolwork and I can't get this simple piece of code (I've extracted it) to work. I just need it to open the file so I can read and write to it. The file (Sedes.txt) is in the same location as both the .cpp and the .exe, in the current working directory. Even adding the path with C:\ or C:\ or C:// or C:/ doesn't work. I'm using DEV C++ with Compiler Code Generation option -std ISO C++11

I've also confirmed using this link with the solution code to corroborate the directory. It outputs in the same folder.

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

using namespace std;

fstream aSedes("Sedes.txt");

int main(){
   string txtWrite = "";
   string txtRead = "";

   aSedes.open("Sedes.txt", ios_base::in | ios_base::out);
   if(aSedes.fail()){
       cout << "!---ERROR: Opening file ln 446---!";
   } else {
       cout << "Sedes.txt opened successfully." << endl;
       cout << "Text in file: " << endl;
       while(aSedes.good()){
           getline(aSedes, txtRead);
           cout << txtRead << "\n";
       }
   }
   aSedes.close();

   return 0;
}

I'm honestly, completley lost. I've tried switching it out everywhere to no avail.

  • What makes you think that a file must open just because it's in the same location as the cpp file? That's not (necessarily) true. You should put the file wherever the *current working directory* is when you run the program. – john Jul 06 '20 at 05:00
  • put it in the directory of the produced .exe – asmmo Jul 06 '20 at 05:01
  • @asmmo That's not (necessarily) correct either. – john Jul 06 '20 at 05:02
  • @john it's in the current working directory, too. Where the .exe is at. I'll edit the question. – Daniel Pantigoso Jul 06 '20 at 05:02
  • 1
    Where the current working directory is when you run your pgroam depends on many different factors, liek which IDE you are using, and how you run the program. – john Jul 06 '20 at 05:02
  • @DanielPantigoso Where is exe is, is **not** the current working directory if you are using (for instance) Visual Studio. – john Jul 06 '20 at 05:03
  • OK I can see the problem. – john Jul 06 '20 at 05:04
  • Possible duplicate of https://stackoverflow.com/questions/40445685/error-in-reading-from-a-txt-file-in-c – Jerry Jeremiah Jul 06 '20 at 05:11
  • ALWAYS use absolute paths, NEVER use relative paths, then the *current working directory* becomes irrelevant. – Remy Lebeau Jul 06 '20 at 05:12
  • Does this answer your question? [Error in reading from a .txt file in c++](https://stackoverflow.com/questions/40445685/error-in-reading-from-a-txt-file-in-c) – Jerry Jeremiah Jul 06 '20 at 05:28

1 Answers1

1

You are opening the file twice, once with the constructor and once with open

fstream aSedes("Sedes.txt"); // opened here

int main(){
   string txtWrite = "";
   string txtRead = "";

   aSedes.open("Sedes.txt", ios_base::in | ios_base::out); // and here again
   if(aSedes.fail()){

Try this instead

int main(){
   string txtWrite = "";
   string txtRead = "";

   fstream aSedes("Sedes.txt", ios_base::in | ios_base::out);
   if(!aSedes.is_open()){

You should probably prefer is_open for checking that a file is open. And you should probably use a local variable for the stream. But if you want a global variable then this should also work

fstream aSedes;

int main(){
   string txtWrite = "";
   string txtRead = "";

   aSedes.open("Sedes.txt", ios_base::in | ios_base::out);
   if(!aSedes.is_open()){
john
  • 85,011
  • 4
  • 57
  • 81