Well I am going to get right to it and say I am 100% clueless as I have scoured the web and texts looking for a clear cut approach and solution to my issue. My title really says it all, I have a file that has 10 or so lines of text that all pretty much look like this...
1301,105515018,"Boatswain","Michael R.",CSE, 230,="R01"
Now I am trying to get each one of those values, delimited by the commas, into their proper variables that belong to a common struct. I have gotten as far as the first 2 integers as well as getting the first string (a last name) but it includes the quotes, which I do not want. I can't seem to get anything other then a bunch of random numbers and letters as well as some goofy symbols when attempting to get the rest. I have tried putting the entire line into an fscanf and it stops at the first string. I know this a poor approach and I have attempt to make a function that word parse the line and get the Strings out, but I couldn't get to the Strings and didn't know how to cut them up. I have also tried strtok but that failed miserably because I had no idea how to get things on the right side of being in between quotes. So here is my code so far...
Any amount of help would go a long way because I am totally stumped, so please and thanks, very much appreciate any help!
1301,105515018,"Boatswain","Michael R.",CSE, 230,="R01"
Now I am trying to get each one of those values, delimited by the commas, into their proper variables that belong to a common struct. I have gotten as far as the first 2 integers as well as getting the first string (a last name) but it includes the quotes, which I do not want. I can't seem to get anything other then a bunch of random numbers and letters as well as some goofy symbols when attempting to get the rest. I have tried putting the entire line into an fscanf and it stops at the first string. I know this a poor approach and I have attempt to make a function that word parse the line and get the Strings out, but I couldn't get to the Strings and didn't know how to cut them up. I have also tried strtok but that failed miserably because I had no idea how to get things on the right side of being in between quotes. So here is my code so far...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME 15
#define MAX_SUBSEC 3
#define N 128
struct student{
int term;
int id;
char lastname[MAX_NAME];
char firstname[MAX_NAME];
char subjectname[MAX_SUBSEC];
int catalog;
char section[MAX_SUBSEC];
}students[10];
int main(){
int term;
int id;
char lastname[MAX_NAME];
char firstname[MAX_NAME];
char sub[MAX_SUBSEC];
int cat;
char sec[MAX_SUBSEC];
char fname[N];
FILE *inputf;
printf("Enter the name of the text file: ");
scanf("%123s",fname);
strcat(fname,".txt");
inputf = fopen(fname,"r");
if (inputf == NULL){
printf("I couldn't open the file for reading.\n");
exit(0);
}
fscanf(inputf, "%d,%d,%[^,]s", &students[0].term, &students[0].id, students[0].lastname);
printf("%d\n", students[0].term);
printf("%d\n", students[0].id);
printf("%s\n", students[0].lastname);
/*for (j = 1 ; j <= 10-1 ; j++){
for(k = 0 ; k <= 10-2 ; k++){
if(students[k] > students[k+1]){
temp = students[k];
students[k] = students[k+1];
students[k+1] = temp;
}
}
}*/
fclose(inputf);
return 0;
}
Any amount of help would go a long way because I am totally stumped, so please and thanks, very much appreciate any help!