Hello, I can not read 8 values ​​from the file (1 2 3 4 5 6 7 8).

#include <fstream> #include <iostream> using namespace std; int main() { ifstream f ("file.txt"); for (int i=0; i<8; i++) { int a; f >> a; cout << a << endl; } return 0; } 

Console output:

2665544 2665544 2665544 2665544 2665544 2665544 2665544 2665544

SUCCESSFUL (total time: 136ms)

I work in NetBeans. The file itself is in the project folder.

What am I doing wrong? Thank.

    1 answer 1

    Everything indicates that the file did not open - it did not have enough rights, or it is not in the same directory as the program. 2665544 - the values ​​of the uninitialized variable a.

     #include <fstream> #include <iostream> using namespace std; int main() { ifstream f ("file.txt"); if (!f) { cerr <<"Error open file!"<<endl; } for (int i=0; i<8; i++) { int a = -13666; f >> a; cout << a << endl; } return 0; } 

    try it so it will be seen. Then try ifstream f ("Полный путь до файла>");

    • the file really does not open. and the full path to the file does not help - knoxx
    • How does the file path look like? Some editors to the article, prohibit access to open files, maybe that's why? - Dith
    • C: \\ file.txt and tried it on everyone: C: \ file.txt C: /file.txt C: //file.txt I used a couple of months ago when I wrote, read and wrote to the file, but using other operators, which I do not remember. All in the same netbins - knoxx
    • It will be correct "C: \\ file.txt" or "C: / file.txt". And what about reading rights? - Dith
    • one
      with the rights everything is OK in general, moved to Linux, everything is OK there) - knoxx