i want to read to a 2d-array from a file, but for some reason the file is not opening (at least thats what the output says)

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

using namespace std;

 int main(){
   
      string filename;
      cout<<"Please type the name of the file";
      cin>>filename;
      fstream file(filename.c_str());//same result with  fstream file(filename.c_str(), fstream::in | fstream::out);

      cout<<filename.c_str();
      if (!file)
      {
         cout<<"file could not be opened"<<endl;
         exit(1);
      }
        //...
   }

every time, i get "fiile could not be opened". the file is in the same directory, with the same name. im using windows xp

Try this and see if you get more information:

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

using namespace std;

int main()
{
    string filename;
    cout<<"Please type the name of the file";
    cin>>filename;
    fstream file(filename.c_str());

    cout<<filename.c_str();
    if (!file)
    {
        perror("file could not be opened");
        exit(1);
    }
    //...
}

Well for one thing, you need to define something like:

ofstream fout;
ifstream fin;

for it to work. You can use whatever you want but most people use fout and fin.

To open the file for reading you type

fin.open(filename)

To open it for writing you type

fout.open(filename)

Also, it might work better if you changed string filename to char filename[<your length here>] and use cin.getline(filename,<your length from before here>). That way the file name can have multiple words and you can pass it strait to fin/fout.open().

Ps You may have to use cin.ignore(1,'\n') to ignore the last enter pressed.

Hurr, it might be a really stupid "error".

One time (at band camp, xD) I was trying to open a file from an app I wrote in Code::Blocks on windows. So I put that file in project/bin/debug/, but code::blocks launched it in project/, so it couldn't find the file.

Try to open an absolute path like "C:\test.txt".

the error it gave me was that no file or directory existed. the program's source code and executable are on the desktop, along with the text document

i tried hardcoding the path, but same problem

i tried using command line instead of my ide, same problem, likewise with when i crt'd it over to a linux lab... same issue

as far as file.open(filename)... i had that later in the coding, but even when i put it before the (!file) check, the results didn't change

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

   using namespace std;

    int main()
   {
      
      string filename;
      cout<<"Please type the name of the file";
      cin>>filename;
      ifstream file(filename.c_str());
      file.open(filename.c_str());
      cout<<filename.c_str();
      if (!file)
      {
         perror("file could not be opened");
         exit(1);
      }
    //...
   }

Wait, doesn't the fstream constructor *always* create something? Check if file.good() is true.

If the constructor is not successful in opening the file, the object is still created although no file is associated to the stream buffer and the stream's failbit is set (which can be checked with inherited member fail).

Oh and, don't open it twice. In the constructor OR with fstream.open(), not both.

ok so i checked the file.good() instead of file, but im getting the same error... i also took out the redundancy.

so even if the object is created, if its not recognized, what good does that do me? how to i fix that....

thanks in advance
Lost

Oh it does you no good, but you can't really check for !file, since file would be.. something. Although it "worked", maybe by accident.

Anyway, Could you hardcode the path, "C:\test.txt", and put the file up here? I'll test it.

Also, on what OS/compiler are you? Just to make sure.

i tried the hardcore path, same

linux and windows- jgrasp using g++ and g++ on its own... and c++

This may sound silly but I have found that if I have this many problems with a project working with a file, if I make a new project and just add all the same source code and re-compile it works for me. Just make sure that your code doesn't look like it has any errors first so you don't have to do it again. Let me know if it works so I can chalk one up for superstition.

... sadly that didn't help either, i've actually made 3 completely different files, in 3 different places, all trying to do the same thing...

WOOOOWWWWWW loser of the year award right here. so what happened was when i named the file "example.txt" windows took that as the filename and wrote in the .txt at the end... so i had example.txt.txt *bangs head repeatedly against wall*

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.