Reading in basic C File I/O help

Hello everyone. Im fairly new to programming and could really use some help.

1
2
3
4
5
48604394 100.00 63.97 99.77
83304736 55.48 66.41 42.86
64035580 82.19 99.34 87.90
56635584 86.17 56.61 80.59
16209188 75.29 94.86 0.00


This is what i have so far:
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
#include <iostream>
#include <stdio.h>
#include <cmath>

using namespace std;


int main()
{

  FILE *fp;
  char studentID[100];
 
  fp = fopen("input.txt","r");
  
 
  while ( !feof (fp) )
    {
	  fscanf(fp,"%s[^\n]",studentID);
	  fgets (studentID , 100 , fp);
	}

  // printf("Student ID: %s\n", studentID);
  
 return 0;
}


So, the program i am to creating is reading in the above input.txt file. The first number of each line represents a StudentID. The following 3 numbers are grades of the student. However, my problem is that i need to read in ONLY the studentID portion and ignore the following 3 grades for the moment. I just have no clue how I should go about reading in the 48604394, then skip to the 83304736. The program should run regardless of how many studentIDs there are.
Last edited on
I would read the first element in, and throw the rest into a variable I didn't care about.

Then you just need to process the sId howevery you need to.


Where sId and junk are strings and input is ifstream obj.
1
2
3
4
while (input >> sId){//Read until whitespace
	getline(input, junk);//Read rest of line
	std::cout << sId << std::endl;//Process sId
}
Last edited on
Topic archived. No new replies allowed.