ifstream, reading from text file

Hi, I have a lab in which I was tasked with using ifstream to read data from a file. Ive never used this before, so being tasked with more than just reading from it is causing me issues. I am meant to read 1 int from the text file then multiple floats that would make an array.
Like I said ive never done this so I used an identifier before each value so I could use that to determine if it was the int, or the floats. But I am having issues getting this to work. The code I have is :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
ifstream myfile;
	string line;
	myfile.open("MeshData.txt");
	if (myfile.is_open())
	{
		string text;
		while (getline(myfile, line))
		{
			myfile >> text;

			if (text == "c")
			{
				line >> LoadedVertexCount;
			}
			if (text == "v")
			{
				line >> LoadedVertices[];
			}
			cout << line << endl;

			
		}myfile.close();
	}


So my questions is, 1. When i check if the text is == to c or v, is it that what i am trying to assign to the LoadedVertexCount etc, because I want it to be the numerical value after that. 2.How do i load in the rest of the numbers to the array.

Like I already said, ive never done this before so not sure if this way is actually correct.

The text doc is

c 6
v 0.0
v 0.0
v 0.0
v 0.0
v -1.0
v 0.0
v 1.0
v 0.0
v 0.0
v 0.0
v 1.0
v 0.0
How would you do this if you were getting the numbers from the user via the console?

Remember a stream is a stream, they all act the same the only difference is what is attached to the stream. In the console the input stream is attached to cin and the output stream is attached to cout.


I am still not understanding it, I get that its just input and output, but Its not helping me solve my issue. I can get the numbers to print fine to console, so I know how to get it set up to read everything, its just getting the data into the LoadedVertexCount, and the LoadedVertices array that im not understanding.
So then what you don't understand is the "array", not how to read from a stream?

Do you realize that you need to read each "element" into an "array" element and that you can't read the array like it was a single instance of some type?

ok if I just concentrate on the first issue. Getting the first number in the text doc to be assigned to LoadedVertexCount. Can I do that with the way I have above, checking the line for the correct letter then assigning line to the LoadedVertexCount, or does that not work like that. I have tried this with only

1
2
3
4
5
if (text == "c")
			{
				line >> LoadedVertexCount;
                                cout << LoadedVertexCount
			}


Which doesnt even return a number, I have also tried it without the if statement and just have it in the while statement, in console this returns the number 0, when it should be 6.

Hoping someone else might be able to shed some light on this issue
Line 7: getline() is going to read the whole line into line, so line will contain "c 6".

Line 9: will read the first character ("v") of the next line into text.

I suggest you use a stringstream to parse your records.

Line 17: You need a subscript to read into an array element.

Something like this:
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
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;

ifstream myfile;
int LoadedVertexCount;
int LoadedVertices[20];

bool read_file ()
{   string line;
    int count = 0;

    myfile.open("MeshData.txt");
    if (! myfile.is_open())
        return false;
    
    while (getline(myfile, line))
    {
        stringstream ss(line);
        string text;

        myfile >> text;     // get the first letter

        if (text == "c")
            ss >> LoadedVertexCount;

        if (text == "v")
            ss >> LoadedVertices[count++];

        cout << line << endl;
    }
    myfile.close();
    return true;
}
Topic archived. No new replies allowed.