need help with fscanf in c++

I new to this forum so i not sure where to put this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void FlightData :: ReadFlightData(){
	FILE * FD;

        FD = fopen("FlightDetail.txt","r");
	int i = 0;
	if (FD == NULL) perror ("Error opening file");
	else{
		while(i < 24){
			fscanf(FD,"%s %s %s %s %s %s", OriAirport, DesAirport,
                                 FlightNumber, AircraftType, DepTime, ArrTime);
			Origin[i] = OriAirport;
			Des[i] = DesAirport;
			cout << Des[i] << endl;
			FlightNo[i] = FlightNumber;
			Aircraft[i] = AircraftType;
			Dep[i] = DepTime;
			Arr[i] = ArrTime;
			i++;
		}
		fclose(FD);
	}
}


I have a problem that i found is fscanf(FD,"%s %s %s %s %s %s", OriAirport, DesAirport, FlightNumber, AircraftType, DepTime, ArrTime); it not reading from the txt.file and not printing the error for opening the file. Is some where it went wrong.
Why don't you use file streams?
I have include <iostream> and <stdio>. Only that part doen't work.
Why not use the C++ streams instead of the C I/O?
I am using c++ stream. I using mircosoft visual C++ 2010 express
You are not using streams for file I/O you are using the C library for that.
I have a side question. I've found that reading/writing to text files is much easier using the C I/O - fscanf, fprintf etc. Can anybody give me a good reason to use C++ I/O (umm I think ifstream, ofstream) over the C I/O? It might just be that I was using the C++ I/O in an inefficient way - it just seemed like I had to write a lot more code to get the job done.

As for the original question. I can't see anything wrong with your code. Can you please post a sample of the file you are trying to read?

Nick.
Pauraic1990,

fscanf can be pretty sensitive to the input file format, so it would help us a great deal if you could post a snippet of the file.
@NickPaul
C++ streams are type safe, flexible and extensible
eg: you can't use printf with a custom type but in C++ you can overload the ostream << operator
Topic archived. No new replies allowed.