# Program can't locate .txt file

This topic is 1348 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

if(hisFamily.fail())

{

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

return EXIT_FAILURE;

}

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;

}

}



##### Share on other sites
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.

##### Share on other sites

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

##### Share on other sites

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

##### Share on other sites

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

##### Share on other sites
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.

##### Share on other sites

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

##### Share on other sites

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.

##### Share on other sites

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

##### Share on other sites

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?

##### Share on other sites

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.

##### Share on other sites

I got it to work, but then I started writing again .  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 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 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;

hisFamily;
if(hisFamily.fail())
{
cerr << "ERROR: Cannot open " << theAdopted << ". \n";
return EXIT_FAILURE;
}
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;
}
}