-4

I am currently working on a cricket scoreboard project. And I would like to keep the teams data in a single folder.

I tried a lot of things but I wasn't able to open the file kept in a different folder.

Here is a snippet of my code:

if(strcmp(TeamName,"Australia")==0||strcmp(TeamName,"australia")==0)
            rf.open("/teams/australia.dat",ios::in|ios::binary);
        else if(strcmp(TeamName,"india")==0||strcmp(TeamName,"India")==0)
            rf.open("india.dat",ios::in|ios::binary);
        else if(strcmp(TeamName,"England")==0||strcmp(TeamName,"england")==0)
            rf.open("england.dat",ios::in|ios::binary);
        else if(strcmp(TeamName,"pakistan")==0||strcmp(TeamName,"Pakistan")==0)
            rf.open("pakistan.dat",ios::in|ios::binary);
        else if(rf)
        {
            cout<<"\n\t\t FILE NOT FOUND";
            return ;
        }

        if(!rf.is_open())
            cerr<<"\n\n\t\tFile not open!";
        rf.read((char*)&rec,sizeof(rec));
        cout<<"\n\t\t Team "<<TeamName;
        while(!rf.eof())
        {
            cout<<"\n\t\t "<<rec.PlayerNumber<<"\t"<<rec.PlayerName;
            rf.read((char*)&rec,sizeof(rec));
        }

There is no compile time error. The file just fails to open at runtime. if(!rf.is_open()) cerr<<"\n\n\t\tFile not open!"; This error message is printed on the console and it goes in infinite loop.

How do I open it using file.open();

Anurag
  • 51
  • 7
  • What does error message say? – Shibli Mar 19 '17 at 12:46
  • Note that a leading slash in a path means that it's an absolute path from the root directory. From that we can deduce that paths not starting with a slash are relative, and they are relative from the programs current working directory. Absolute paths are usually good if you can't be sure what the programs working directory might be. – Some programmer dude Mar 19 '17 at 12:47
  • Also please read [Why is iostream::eof inside a loop condition considered wrong?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – Some programmer dude Mar 19 '17 at 12:48
  • There is no compile time error. The file just fails to open at runtime. if(!rf.is_open()) cerr< – Anurag Mar 19 '17 at 12:48
  • Some programmer dude: Thank you! Giving the full address works! But what if we move the whole project somewhere else, then everytime I would have to change the address in the code. Isn't there a more simpler way? – Anurag Mar 19 '17 at 12:54
  • Configuration files containing the path to the files? Command-line arguments? That's how it's commonly done. – Some programmer dude Mar 19 '17 at 12:57
  • Some Programmer Dude: Can you explain a bit on how to do those? – Anurag Mar 19 '17 at 12:59

1 Answers1

1

I wasn't able to open the file kept in a different folder.

I would guess that your system probably does not have a "/teams" 'folder.

On Linux, you might have "/home/anurag/teams/australia.dat" that you created.

I suggest you try relative paths in your C++ code.

If your code and executable is in /home/anurag/teams/src and If your files are in /home/anurag/teams/

then your relative path is 'up one' directory.

  "../australia.dat"  
2785528
  • 5,162
  • 2
  • 16
  • 18
  • With more info, like OS and the barest outline of the path to where your code is and where the executable is and where the data files are, we might be able to help more. – 2785528 Mar 19 '17 at 14:58