I'm very noob in C, and also we are not allowed to use ftell() or something similar. I can't print out the contents of my file as it is asked from me.This is eventually task in which I should've created functions that reads contents of file and store it in array then returns number of items in file, and in main() I had to print out using readStations() function. In main() also there should've been railwayLine[100] array of type station.
File has text as follows:
1. 0.0 London-Kings-Cross*
2. 3.9 Finsbury-Park*
...
First of all, I created typedef struct called station with properties km and name which are the distance and name of stations. I've tried to create function readStations(char filename[20], station line[])
My attempt is as follows:
#include <stdio.h>
typedef struct {
char name[30];
double km;
} station;
int readStations(char filename[20], station line[]){
FILE* openedFile;
openedFile = fopen(filename, "r");
if(openedFile == NULL){
printf("Some problem occured with opening of file");
return 1;
}
station stations;
int count = 0;
for (; !feof(openedFile); count++){
fscanf(openedFile, "%lf %s", &stations.km, stations.name);
}
int numberOfStations = count;
return count;
}
int main(){
station railwayLine[100];
printf("");
}
Actually, it returns me the number of items of .txt file but in main I don't know how to print out all items as they look in .txt file.