skip to Main Content

This is the part of code where I try to open the file f1.txt, it is complete path is C:UsersHpDesktopNSGA2-CDSDataSetf1.txt

ifstream fichier("C:UsersHpDesktopNSGA2-CDSDataSetf1.txt", ios::in);

The file cannot be opened and I don’t know why?!

NSGA2-CDS is the folder that contain the visual studio solution

2

Answers


  1. You have to escape backslashes in the path string:

    ifstream fichier("C:\Users\Hp\Desktop\NSGA2-CDS\DataSet\f1.txt", ios::in);
    

    This has nothing to do with file I/O as such; it’s a feature of string literals: is used to escape special characters (such as n, t), so when it appears in a string, it needs to be escaped as well.

    Login or Signup to reply.
  2. you can also use raw string literal so you don’t need to escape individual char.

    ifstream fichier(R"(C:UsersHpDesktopNSGA2-CDSDataSetf1.txt)", ios::in);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search