Pressure
Newbie
- Joined
- Jul 3, 2003
- Messages
- 5,065
- Reaction score
- 0
Ok, I'm working on a program right now and I need to know how to read a name from a file. Reading just one word works fine but the name is in the format "last first". That space throws it off and it only reads in the last name. Example:
Sample file, grades.txt
That's the file I'm reading in info from. Here's my code for the function:
I only have it reading in the last name because I can't figure out got to get it to read both.
fscanf(infile, "%s %s", last_name, first_name);
That doesn't work, I've already tried it. Does anyone know how to do it?
Sample file, grades.txt
Code:
11 5
Gupta Anil
96 90 85 90 80
Jeffers Jose
60 0 70 50 30
Evans Rudy
80 60 70 50 75
Williams Jason
50 40 0 0 70
Ball Adam
90 80 60 55 60
Arnold Joshua
0 0 0 0 0
Scot Michael
80 70 0 0 60
Cook Bret
95 80 80 70 60
Davis Ronald
0 0 0 0 0
Fox Brian
70 60 60 40 50
Hale Frank
60 50 60 30 40
That's the file I'm reading in info from. Here's my code for the function:
Code:
void get_info(struct students student[], int *num_students, int *exams)
{
int i, j, grades = 0;
char first_name[30], last_name[30], file[40];
FILE *infile;
// Get file name
printf("Enter the name of the file containing the grades: ");
scanf("%s", file);
// Open file and read in info
infile = fopen(file, "r");
fscanf(infile, "%d %d", &num_students, &exams);
for(i = 0; i < *num_students; i++)
{
fscanf(infile, "%s", last_name);
strcpy(student[i].l_name, last_name);
for(j = 0; j < *exams; j++)
{
fscanf(infile, "%d", &grades);
student[i].grade[j] = grades;
}
}
// Close file
fclose(infile);
}
I only have it reading in the last name because I can't figure out got to get it to read both.
fscanf(infile, "%s %s", last_name, first_name);
That doesn't work, I've already tried it. Does anyone know how to do it?