Jump to content

  • Log In with Google      Sign In   
  • Create Account


Program can't locate .txt file


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
12 replies to this topic

#1 theBegger   Members   -  Reputation: 107

Like
0Likes
Like

Posted 11 May 2014 - 11:12 PM

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;

     }

}

                 

Attached Files



Sponsor:

#2 frob   Moderators   -  Reputation: 20491

Like
1Likes
Like

Posted 11 May 2014 - 11:29 PM

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.
Check out my personal indie blog at bryanwagstaff.com.

#3 theBegger   Members   -  Reputation: 107

Like
0Likes
Like

Posted 11 May 2014 - 11:32 PM

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??



#4 theBegger   Members   -  Reputation: 107

Like
0Likes
Like

Posted 11 May 2014 - 11:35 PM

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



#5 theBegger   Members   -  Reputation: 107

Like
0Likes
Like

Posted 11 May 2014 - 11:41 PM

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.

 



#6 frob   Moderators   -  Reputation: 20491

Like
2Likes
Like

Posted 11 May 2014 - 11:46 PM

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.
Check out my personal indie blog at bryanwagstaff.com.

#7 theBegger   Members   -  Reputation: 107

Like
0Likes
Like

Posted 11 May 2014 - 11:52 PM

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.


Edited by theBegger, 11 May 2014 - 11:53 PM.


#8 Strewya   Members   -  Reputation: 1373

Like
1Likes
Like

Posted 12 May 2014 - 01:08 AM

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


#9 theBegger   Members   -  Reputation: 107

Like
0Likes
Like

Posted 12 May 2014 - 01:21 AM

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



#10 theBegger   Members   -  Reputation: 107

Like
0Likes
Like

Posted 12 May 2014 - 01:31 AM

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?

#11 Strewya   Members   -  Reputation: 1373

Like
1Likes
Like

Posted 12 May 2014 - 01:38 AM

I personally use two sites:

http://en.cppreference.com/w/

http://www.cplusplus.com/reference/

 

As for the reloading the output, i'm guessing you have the output file open in your IDE or another editor, each time you run the program, the file is modified, so you get asked if you want to reload it.


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


#12 theBegger   Members   -  Reputation: 107

Like
0Likes
Like

Posted 12 May 2014 - 02:32 AM

I got it to work, but then I started writing again sleep.png.  I can't figure out why I'm getting this error. It's calling for a declaration where I thought I had already declared things....

 

 

107 expected declaration before '}' token

        [Build Error]  [Merger.1.o] Error 1

 

 

 

The code is mostly the same, except I hadn't ended the while loop in the main function. So when I added a braket to close that loop, I got this pretty little error.

#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;
    if(hisFamily.fail())
    {
        cerr << "ERROR: Cannot open " << theAdopted << ". \n";
        return EXIT_FAILURE;
    } 
    //Retreive her family's grades.
    herFamily;
    if(herFamily.fail())
    {
        cerr << "ERROR: Cannot open " << theOriginals << ". \n";
        return EXIT_FAILURE;
    }
    //Call theBigPicture.
    ourFamily;
    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();
    ourFamily.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.ignore(nextCh);
             hisFamily.get (nextCh);
         }
         if(!hisFamily.eof())
         {
             ourFamily.put(NWLN);
             charCount++;
         }
         return charCount;
     }
}


#13 theBegger   Members   -  Reputation: 107

Like
0Likes
Like

Posted 12 May 2014 - 02:33 AM

Wow disregard that....I had an extra bracket unsure.png






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS