File not reading in and stuck in a While loop

Can anyone help me, I've been trying to open my txt file but it doesn't read in. The fail statement leaves me in an infinite loop telling me to enter a file name and path, and once I do enter the file name and path it tells me to enter it again.

I've tried refreshing the project, cleaning it, and closing/opening the program altogether. And I still don't know what's wrong.

*NOTE: I am currently using Eclipse Oxygen 2017




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
int main()
{
	ifstream fin;
	string userfile;
	string str;
	double a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
	vector<Student> student;
	Student stu;

	fin.open ("lab2.txt");
		if (fin.fail())
		{
			while(fin.fail())
			{
				cout << "Input file failed to open.\n";

				cout << "\nPlease enter a file name/path ";

		    		cin >> userfile;

		    		cout << endl;

		    		fin.open (userfile.c_str());

	    		if(fin)
	    			{
	    				while(fin >> str >> a1 >> a2 >> a3 >> a4 >> a5 >> a6 >> a7 >> a8 >> a9 >> a10)
	    				{
	    					stu.name=str;
	    					stu.score=a1;
	    					stu.score=a2;
	    					stu.score=a3;
	    					stu.score=a4;
	    					stu.score=a5;
	    					stu.score=a6;
	    					stu.score=a7;
	    					stu.score=a8;
	    					stu.score=a9;
	    					stu.score=a10;

	    					student.push_back(stu);
	    				}

	    			}


	    			fin.close();
			}
		    //fin.open (userfile);
		}

	if(fin)
	{
		while(fin >> str >> a1 >> a2 >> a3 >> a4 >> a5 >> a6 >> a7 >> a8 >> a9 >> a10)
		{
			stu.name=str;
			stu.score=a1;
			stu.score=a2;
			stu.score=a3;
			stu.score=a4;
			stu.score=a5;
			stu.score=a6;
			stu.score=a7;
			stu.score=a8;
			stu.score=a9;
			stu.score=a10;

			student.push_back(stu);
		}
	}

	fin.close();
	return 0;
}

/******************************************************************

                            OUTPUT

Input file failed to open.

Please enter a file name/path lab2.txt

Input file failed to open.

Please enter a file name/path lab2.txt

Input file failed to open.

Please enter a file name/path 

*******************************************************************/
Last edited on
Wrap your program in [code][/code] tags when posting (you can edit your post and add them). And post the entire program, including headers.

Maybe lab2.txt doesn't exist or is in the wrong directory?
Try this version.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

const int NumScores = 10;

struct Student
{
    string name;
    double score[NumScores];
};

int main()
{
    vector<Student> student;
    
    string userfile("lab2.txt");
    ifstream fin(userfile);
    while (!fin)
    {
        cout << "Input file failed to open.\n";
        cout << "\nPlease enter a file name/path: ";
        cin >> userfile;
        fin.open(userfile);
    }

    Student stu;
    while (fin >> stu.name)
    {
        for (int i = 0; i < NumScores; ++i)
            fin >> stu.score[i];
        student.push_back(stu);
    }

    fin.close();

    for (const auto& st: student)
    {
        cout << st.name << '\n';
        for (int i = 0; i < NumScores; ++i)
            cout << st.score[i] << ' ';
        cout << '\n';
    }
}

Last edited on
The version isn't working either.

And my lab2.txt is in the src where the project is also located.
Try entering src/lab2.txt or ../src/lab2.txt

Or try moving it into the directory that the executable is in.
still stuck in the loop
I assume you are on Windows, right? (Unfortunately, I'm not, so I can't test this.)
If you are on Windows, try adding this at the beginning of main:

1
2
3
4
5
    char buf[1000];
    if (GetCurrentDirectory(sizeof buf, buf) == 0)
        cout << "Can't get directory\n";
    else
        cout << "Dir: " << buf << '\n';    

You'll also need to add #include <windows.h> to your headers.

What does it print when you run it?
And how are you compiling it? Are you using Visual Studio?
Last edited on
I'm using Eclipse to compile my functions.

And I'm trying to read out these:

1
2
3
4
5
6
7
8
9
10
Anna 70 79 72 78 71 73 68 74 75 70
Jason 78 89 96 91 94 95 92 88 95 92
kim 83 81 93 85 84 79 78 90 88 79
Maria 93 100 86 99 98 97 96 95 94 92
Daniel 72 60 82 64 65 63 62 61 67 64
Tricia 80 92 100 81 82 83 84 85 86 87
Peter 92 91 74 76 77 78 81 83 80 88
Lawrence 67 60 65 68 72 74 76 77 78 73
Helena 93 99 91 93 94 96 95 97 98 74
Max 82 81 89 75 77 79 81 83 85 78


Sadly I can't use a header as my Instructor only wants us to pass structs by vectors.
> *NOTE: I am currently using Eclipse Oxygen 2017
This is your editor.
What is your OS and Compiler?

If you're using win-blows, then check your file explorer isn't hiding file extensions.
https://www.thewindowsclub.com/show-file-extensions-in-windows
Because it's all too easy for you to end up with a file that is actually called lab2.txt.txt

Next, you can massively simplify your test case to be
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <fstream>
#include <cstring>
#include <cerrno>
using namespace std;
int main ( ) {
  ifstream fin;
  fin.open("lab2.txt");
  if ( fin.fail() ) {
      cout << "Error=" << strerror(errno) << endl;
  }
}


You'll probably get
Error=No such file or directory
Which means you're looking in the wrong place.
How are you running the executable - by clicking "Run..." in your IDE?
The idea of "current working directory" can vary depending on whether you run the program from the IDE, run the program by clicking on it in a file manager, or by running it from the command line.

If you get this, you have a different problem to solve.
Error=Permission denied


With regard to your post, the whole copy/paste of your "while (fin >> str >> a1......" code is bad.
1
2
3
4
5
6
7
8
  fin.open("lab2.txt");
  while (fin.fail()) {
    // your attempts to open the file
  }
  // When you get here, fin is good to go.
  while (fin >> str >> ... ) {
     // more code
  }

I can't use a header as my Instructor only wants us to pass structs by vectors.

WTF?! I wasn't saying stick that code in and hand it in that way. I was trying to find your current directory. Jesus!

Anyway, it looks like salemc knows/remembers more about win-blows(!) than I do (I forgot all about the extension hiding thing) and I'm getting tired of this, so good luck.
For whatever reason, you can't open lab2.txt so the open() at line 10 fails.

This gets you to the while loop at line 13.
You enter a valid file name.
The loop at line 27 runs. By the way, lines 30-38 have no effect. For each line, the very next line blows away the assignment.

Eventually you read all the data at line 27 and the next extraction at line 27 fails.
So now fin.fail() is true.
You close the file at line 47. fin.fail() is still true.
The loop at line 13 executes again. fin.fail() is still true so around you go.

Did you mean to have line 25 inside the loop at line 13? The indentation suggests not. Here is your code with the indentation reflecting the actual block structure.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

struct Student {
    string name;
    double score;
};

int
main()
{
    ifstream fin;
    string userfile;
    string str;
    double a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
    vector < Student > student;
    Student stu;

    fin.open("lab2.txt");
    if (fin.fail()) {
	while (fin.fail()) {
	    cout << "Input file failed to open.\n";
	    cout << "\nPlease enter a file name/path ";
	    cin >> userfile;
	    cout << endl;
	    cout << "You entered \"" << userfile << "\"\n";
	    fin.open(userfile.c_str());

	    if (fin) {
		cout << "input file is valid\n";
		while (fin >> str >> a1 >> a2 >> a3 >> a4 >> a5 >> a6 >> a7 >> a8 >> a9 >>
		       a10) {
		    stu.name = str;
		    stu.score = a1;
		    stu.score = a2;
		    stu.score = a3;
		    stu.score = a4;
		    stu.score = a5;
		    stu.score = a6;
		    stu.score = a7;
		    stu.score = a8;
		    stu.score = a9;
		    stu.score = a10;

		    student.push_back(stu);
		}
	    }

	    cout << "before close fail is " << fin.fail() << '\n';
	    fin.close();
	    cout << "after close fail is " << fin.fail() << '\n';
	}
	//fin.open (userfile);
    }

    if (fin) {
	while (fin >> str >> a1 >> a2 >> a3 >> a4 >> a5 >> a6 >> a7 >> a8 >> a9 >> a10) {
	    stu.name = str;
	    stu.score = a1;
	    stu.score = a2;
	    stu.score = a3;
	    stu.score = a4;
	    stu.score = a5;
	    stu.score = a6;
	    stu.score = a7;
	    stu.score = a8;
	    stu.score = a9;
	    stu.score = a10;

	    student.push_back(stu);
	}
    }

    fin.close();
    return 0;
}


You shouldn't repeat the code that reads the data. Write a block of code that loops to get the name of an existing file. When you exit that loop, run the loop to extract the data.
Topic archived. No new replies allowed.