Program can't locate .txt file

Started by
11 comments, last by theBegger 9 years, 11 months ago

I'm running DEV and am having trouble with my program finding the .txt files I'm trying to open.

I've had a hard time finding any curriculum on where to store files that need to be called by a program.

Right now, I have the program, both input files, and single output file all in the same folder.

The program will not open any of the files.

Is that an issue with the location of the files, or with the program itself??


#include <iostream>

#include <fstream>

#include <cstdlib>

#include <string>

using namespace std;



#define theAdopted "The Adopted.txt"            //His family

#define theOriginals "The Originals.txt"        //Her family

#define theBigPicture "The Big Picture.txt"        //Our family



//Function Prototypes



class KeepRunning {                            //Prototype needed to keep console from closing.

  public:

    ~KeepRunning() {

      cin.get();}};



//Copies text

int copyLine(ifstream&, ifstream&, ofstream&);



int main()                                                                  

{   

    KeepRunning kr;  

     

    ifstream hisFamily("The Adopted.txt");

    ifstream herFamily("The Originals.txt");

    ofstream ourFamily("The Big Picture.txt");

    

    int SIZE1 = 200, SIZE2 = 200;  

    int insA[SIZE1];

    int insB[SIZE2];

    int outs[SIZE1 + SIZE2];

    int lineCount;

    int index1 = 0;

    int index2 = 0;

    int A = 0;

    int B = 0;

    

    //Retreive his family's grades.

    hisFamily.open(theAdopted);

    if(hisFamily.fail())

    {

        cerr << "ERROR: Cannot open " << theAdopted << ". \n";

        return EXIT_FAILURE;

    }

    //Retreive her family's grades.

    herFamily.open(theOriginals);

    if(herFamily.fail())

    {

        cerr << "ERROR: Cannot open " << theOriginals << ". \n";

        return EXIT_FAILURE;

    }

    //Call theBigPicture.

    ourFamily.open(theBigPicture);

    if(ourFamily.fail())

    {

        cerr << "ERROR: Cannot open " << theBigPicture << ". \n";

        return EXIT_FAILURE;

    }        

    //Copy data hisFamily to ourFamily.

    string line;

    lineCount = 0;

    getline(hisFamily, line);

    while(line.length() != 0)

    {

        lineCount++;

        ourFamily << line << endl;

        getline(hisFamily,line);

    

    cout << "Input data mergered to file 'The Big Picture.exe'." << endl;

    //Close files.

    hisFamily.close();

    herFamily.close();

    return 0;

}  

int copyLine

    (ifstream& hisFamily,

     ifstream& herFamily,

     ofstream& ourFamily);

     {

         const char NWLN = '\n';

         char nextCh;

         int charCount = 0;

         

         //Merge

         hisFamily.get(nextCh);

         while ((nextCh != NWLN) && !hisFamily.eof())

         {

             ourFamily.put(nextCh);

             charCount++;

             hisFamily.get (nextCh);

         }

         if(!hisFamily.eof())

         {

             ourFamily.put(NWLN);

             charCount++;

         }

         return charCount;

     }

}

                 

Advertisement
File locations can be tricky.

If you don't specify exactly where the files are located, usually the file names are considered relative the the executable.

There is the "current working directory", which may be different from the program's location, which may also be different from the source code location.

If you are running your program inside an IDE like Visual Studio or Eclipse, look inside the project settings for the program's working directory. It might default to a ../bin/ or ../output/ or some other directory. You write you are using "DEV", which could be several programs. If you mean Dev-C++, there are several different forks of the project (Bloodshed Dev-C++, WX Dev-C++, Orwell MinGW Dev-C++, Orwell TDM Dev-C++, ...) so specify which fork and the version.

Some options available to you include copying the files to the output directory (perhaps adding them to the project file and marking them to 'deploy' with the project), or to manually copy the files to the final destination, or to modify the working directory to the location of the files. You could also require the full path to the files rather than just the file name. There are many other options as well, those should hopefully be enough to unblock you.

Oh goodness, I thought there was only one DEV! I'm running Blooddshed's DEV-C++, the latest version, 5. I'm not working in a project, I just have a plain jane source file. Does this mean I need to creat a project and add my current program to said project? Then place my files into said project's location folder??

Wow that helped about a hundred fold! Thank you!!!

Hmm. I think I got a little ahead of myself. It's still not going past the first code. I just get

ERROR: Cannot open The Adopted.exe.

Google finds quite a few topics on it. It looks like by default they use a subfolder named 'bin', try making a copy of the file there, or adjusting the settings.

Also, the Bloodshed version of the compiler is quite old. It is known to have many bugs, and does not support modern C++ features. You might consider moving to a more modern compiler and environment, especially if you want to take advantage of the language changes made in 2005, 2007, 2011, and those being added in 2014. If you are content working with an older version of the C++ language, then continue as you see fit.

I tried using CodeBlocks because I'd read so many good things about it, but I couldn't get the compilers to work correctly. I think I was messing up my paths. I have pretty limited knoweledge, so I figured I'll wait until I have an instructor to help me figure out the compiler issues I was having. Thanks for all your help!!

Also, I should add that the error isn't coming from the compiler. It's output by the program because it's failing to locate the file. I'm pretty sure you know, that, though.

I think you should start checking the reference manuals for functions you use ;)

You first create an ifstream with the filename passed in, which internally opens the file.

Then you call .open(), for which the docs say that if the file is already opened, the functions fails, sets the failbit, which you then check, and the program closes.

Simply removing the .open() call for each file should do the trick.

devstropo.blogspot.com - Random stuff about my gamedev hobby

I got my format from a reference, maybe a bad one? Ha. Thank you so much!!

Sorry to be a pain, but the Windows wants to know if I want to

reload the output file from disk?
each time the program compiles. Is this because I'm not closing the file out?

This topic is closed to new replies.

Advertisement