K
ketema
Hello, I was wondering if someone could help me with a function I am
trying to write. The purpose of the function is to read in text from a
file in the following format:
FIRSTNAME LASTNAME SCORE SCORE SCORE SCORE
example:
Karen Smith 100 100 100 100
John Oliver 78 90 65 51
Henry Green 44 55 44 44
I basically want to pass the number of lines or records, a file
pointer, a pointer to a char array, a pointer to another char array,
and a pointer to an int array to a function that will read all the file
input up to the first space into the first element of the first char
array, then all the next characters up to the second space into the
first element of the second char array, then all the integers into the
first element of the int array. This is what I have so far:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
void readLines(int numLines, FILE *, char *firstNames[], char
*lastNames[], int *scores[]);
//variables
FILE *inputFile;
int numRecords = 0;
...get number of records from user...
char *firstNames[numRecords]; //array of pointers
char *lastNames[numRecords];
int *scores[4]; //We know there are always 4 for each line
scores
inputFile = fopen("input.txt", "r"); //open the input file
readLines(numRecords, inputFile, firstNames, lastNames,
scores);
void readLines(int numLines, FILE *file, char *firstNames[], char
*lastNames[], int *scores[]){
int i = 0;
for(i = 0; i < numLines; i++){
while(firstNames = (getc(FILE *file)) ){//this is
where i need
help...
want to read placing each char into firstNames
until i hit a space,then
start putting chars into lastNames until i hit
the next space then read each int placing
each one into a spot in scores[0]
}
}
}
Thanks for the feedback
Ketema
trying to write. The purpose of the function is to read in text from a
file in the following format:
FIRSTNAME LASTNAME SCORE SCORE SCORE SCORE
example:
Karen Smith 100 100 100 100
John Oliver 78 90 65 51
Henry Green 44 55 44 44
I basically want to pass the number of lines or records, a file
pointer, a pointer to a char array, a pointer to another char array,
and a pointer to an int array to a function that will read all the file
input up to the first space into the first element of the first char
array, then all the next characters up to the second space into the
first element of the second char array, then all the integers into the
first element of the int array. This is what I have so far:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
void readLines(int numLines, FILE *, char *firstNames[], char
*lastNames[], int *scores[]);
//variables
FILE *inputFile;
int numRecords = 0;
...get number of records from user...
char *firstNames[numRecords]; //array of pointers
char *lastNames[numRecords];
int *scores[4]; //We know there are always 4 for each line
scores
inputFile = fopen("input.txt", "r"); //open the input file
readLines(numRecords, inputFile, firstNames, lastNames,
scores);
void readLines(int numLines, FILE *file, char *firstNames[], char
*lastNames[], int *scores[]){
int i = 0;
for(i = 0; i < numLines; i++){
while(firstNames = (getc(FILE *file)) ){//this is
where i need
help...
want to read placing each char into firstNames
until i hit a space,then
start putting chars into lastNames until i hit
the next space then read each int placing
each one into a spot in scores[0]
}
}
}
Thanks for the feedback
Ketema