getline(ifstream, string)

One more question:

I have the following file:
AIRPORT	GND	TWR	ARR	DEP	CLNC	APRN
EHAM	121.7	119.22	121.2	119.05	121.97	121.65
OMDB	118.35	118.75	124.9	124.45	119.55	118.85
VHHH	118.4	118.2	126.5	123.8	122.55	121.6


And I have the following code to read the data and store it into a 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
ifstream ffq("./conf/AP_Freqs.txt");
string Line;
getline(ffq,Line); // Waste the first line, it's a header

while (getline(ffq,Line))
{
  stringstream iss(Line);

  FREQUENCIES temp;		//Structure FREQUENCIES (has 1 string and 6 floats)
  iss >> temp.airport;		//string
  iss >> temp.ground;		//float
  iss >> temp.tower;		//float
  iss >> temp.arrival;		//float
  iss >> temp.departure;	//float
  iss >> temp.clearance;	//float
  iss >> temp.apron;		//float

  vFreqs.push_back(temp);	//This is a vector

  cout << temp.airport << "\t" << std::fixed << std::setprecision(1) 
  << temp.ground << "\t" << temp.tower << "\t" << temp.arrival << "\t" 
  << temp.departure << "\t" << temp.clearance << "\t" << temp.apron 
  << endl;
}


Unfortunately, I am never able to enter the while loop above, as ffq apparently has a badbit set on the first line. If I use this following code instead (checking specifically for an eof()), then I get an infinate loop where none of the values are correct:
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
ifstream ffq("./conf/AP_Freqs.txt");

string Line;
getline(ffq,Line); // Waste the first line, it's a header

while (!ffq.eof())
{
  getline(ffq,Line);
  stringstream iss(Line);

  FREQUENCIES temp;		//Structure FREQUENCIES
  iss >> temp.airport;		//string
  iss >> temp.ground;		//float
  iss >> temp.tower;		//float
  iss >> temp.arrival;		//float
  iss >> temp.departure;	//float
  iss >> temp.clearance;	//float
  iss >> temp.apron;		//float

  vFreqs.push_back(temp);	//This is a vector

  cout << temp.airport << "\t" << std::fixed << std::setprecision(1) 
  << temp.ground << "\t" << temp.tower << "\t" << temp.arrival << "\t" 
  << temp.departure << "\t" << temp.clearance << "\t" << temp.apron 
  << endl;
}
	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0
	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0
	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0
	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0
	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0
	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0
	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0
	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0
	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0
	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0
	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0
	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0
	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0
	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0
	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0
	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0
	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0
	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0
	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0
	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0
	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0
	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0
	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0
	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0
	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0	-107374176.0
...


What am I doing wrong?
Last edited on
Did you already check if you actually got to open the file? You might be looking for it in the wrong directory or something (e.g. your working directory may be different from what you expect it to be).
I just tried changing the path of the file to the root of the .exe and .dll and moving the file to that location. There was no difference. I have other files that I am working with in the ./conf/ directory and these are read without problem (but they aren't in the same format).
So, was the file open or not?
Yes, the file was open.

Edit: No wait....

I added this line after line 1 above:
if (ffq.good()) cout << "File Open!" << endl;

I didn't get the cout. HMMMMM!

I have another file opened before this code is called with the following code:
1
2
3
4
5
ifstream fid("./conf/AC_Idents.txt");
	
string Ident;
while (getline(fid,Ident))
	vIdents.push_back(Ident);


This one works without issue. Why isn't my file opening?

Edit2: Nevermind!!!! I had a problem on my end with the file name. Oooooo. Thanks for the idea of checking the file status.
Last edited on
Topic archived. No new replies allowed.