L
L
Lisik2019-04-13 23:31:51
C++ / C#
Lisik, 2019-04-13 23:31:51

How to correctly read the file when compiling?

When compiling the project, the program does not read the "text.txt" file, although the path is correct. The if (!fin) option always passes. How to fix?

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
  string str;
  cout << "Vvedite slovo" << endl;
  cin >> str;

  ifstream fin("D:\\Program files\\VisualStudio\\Projects\\Dliya DZ\\Debug\\text.txt");
  if (!fin) {
    cout << "Fail";
    system("pause");
    return 0;
  }
...

Answer the question

In order to leave comments, you need to log in

4 answer(s)
R
Roman, 2019-04-14
@Lisik

When compiling
already when running
Relative to the project folder ( not a solution ) -> or whatever you like. or put text.txt directly in your project folder then
ifstream ifs("text.txt");

V
vreitech, 2019-04-13
@fzfx

I think that you should either rename the file so that there is no space in the full file name, or add enclosing double quotes to the string (do not forget to escape them).

E
Evgeny Petryaev, 2019-04-14
@Gremlin92

Try changing the slashes from backticks to regular slashes (from \\ to //)

N
Nikita, 2019-04-14
@NS33

I tried it myself: it finds through spaces and such slashes - "\\".
There is only one conclusion - your path is wrong. In such cases, I use the button "copy names with paths to clipboard" in Total Commander or collect "Location" + "File name" through the file properties. Somewhere the Russian x got in instead of the Latin x, and that's it, the path is wrong.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question