• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
theBegger

Program can't locate .txt file

12 posts in this topic

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;

     }

}

                 

0

Share this post


Link to post
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.
1

Share this post


Link to post
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??

0

Share this post


Link to post
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

ERROR: Cannot open The Adopted.exe.

 

0

Share this post


Link to post
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.
2

Share this post


Link to post
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
0

Share this post


Link to post
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.

1

Share this post


Link to post
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?
0

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0