0

I am working on dev c++. I am using file stream to open a file which is already placed in a folder. Below is my code.

int main(){
  ifstream file; // File stream object
  string name; // To hold the file name

  //Write the path of the folder in which your file is placed
  string path = "C:\\Users\\Faisal\\Desktop\\Programs\\";

  string inputLine; // To hold a line of input
  int lines = 0; // Line counter
  int lineNum = 1; // Line number to display

  // Get the file name.
  cout << "Enter the file name: ";

  getline(cin, name);// Open the file.

  string fileToOpen = path + name + ".txt";
  file.open(fileToOpen.c_str(),ios::in);// Test for errors.

  if (!file){
    // There was an error so display an error
    // message and end the PROGRAM.
    cout << "Error opening " << name << endl;
    exit(EXIT_FAILURE);
  }

  // Read the contents of the file and display
  // each line with a line number.
  // Get a line from the file.
  getline(file, inputLine, '\n');

  while (!file.fail()){
    // Display the line.
    cout << setw(3) << right << lineNum<< ":" << inputLine << endl;

    // Update the line DISPLAY COUNTER for the next line.
    lineNum++;// Update the total line counter.

    lines++;// If we've displayed the 24th line, pause the screen.

    if (lines == 24){
      cout << "Press ENTER to CONTINUE...";
      cin.get();
      lines = 0;
    }

    // Get a line from the file.
    getline(file, inputLine, '\n');
  }

  //Close the file.
  file.close();

  return 0;
}

My file is in the same folder where my program resides i.e. in my C:\\Users\\Faisal\\Desktop\\Programs\\. However, I want to use a relative path, so whoever runs the program can access the file.

How can I pass the relative path?

Any help would be highly appreciated.

Lauren Yim
  • 12,700
  • 2
  • 32
  • 59
Faisal Qayyum
  • 132
  • 13
  • You pass the relative path the same way you pass the absolute path. E.g. `file.open("somename.txt", ...)` – Igor Tandetnik Aug 30 '20 at 17:57
  • Pass the relative path exactly the same way you pass and absolute path, there's no difference. Of course the problem is that you may be mistaken where the relative path is relative to. There is nothing that says it must be relative to the program folder. – john Aug 30 '20 at 17:57
  • Relative paths can be a bit tricky. If you use `somename.txt` it is relative to the working directory, which does not necessarily need to be the location of the application. – t.niese Aug 30 '20 at 17:58
  • @IgorTandetnik I have tried this way but it didn't work – Faisal Qayyum Aug 30 '20 at 17:58
  • 1
    Then either the file is not where you think it is, or the current working directory is not what you think it is. – Igor Tandetnik Aug 30 '20 at 17:59
  • @FaisalQayyum Then the problem is that the path is not relative to the place you think it is relative to. – john Aug 30 '20 at 17:59
  • The relative path would need to be relative to the current working directory. – Eljay Aug 30 '20 at 17:59
  • Where the current working directory gets set is controlled by many factors. If you want to set a particular current working directory then you will have to do it yourself. Your problem is going to be that there is no standard C++ way to get the program directory (I believe). – john Aug 30 '20 at 18:03

1 Answers1

0

Consider using /tmp directory on unices and System.IO.Path.GetTempPath() on Windows.

If you decide to use current working directory, you need to find current working directory executable cwd which is not at the same place on all platforms.

Check out https://en.cppreference.com/w/cpp/filesystem/current_path also

puio
  • 1,208
  • 6
  • 19