my input file will contain:
ABC|DEF|91
XYZ|LDS|78
Output should be:
First Name..........Last Name..........Grade
ABC......................DEF...................... .91
XYZ......................LDS...................... .78
I am trying to get it to split at the '|' points.
Question:
I am trying to break this up into first name, last name, and grade. I have been able to get it to split the first name and grade and print those out, but getting it to split the 3rd time is giving me trouble.
In present form the current code will take:Code:#include <stdio.h> #include <stdlib.h> #include <string.h> struct person{ char name[30]; int age; }; int main(int args, char *argv[]) { struct person list[30]; int i,n; char *ch,*ch1; char buf[256]; FILE *fp; fp = fopen(argv[1],"r"); if(fp == NULL){ printf("Cannot open file %s\n", argv[1]); exit(0); } i=0; fgets(buf,256,fp); while(!feof(fp)){ ch = strchr(buf,'\n'); if(ch != NULL) *ch = '\0'; else break; ch = strchr(buf,'|'); if(ch != NULL){ *ch = '\0';ch1 = ++ch; } else break; strcpy(list[i].name,buf); list[i].age = atoi(ch1); i++; fgets(buf,256,fp); } n = i; for (i = 0; i < n; i++) printf("%s is %d years old\n",list[i].name,list[i].age); return 0; printf(" }
ABC|78
XYZ|56
and outprint:
First Name.......................Last Name.......................Grade
ABC...................................________.... ......................78
XYZ...................................________.... ......................56
So I feel like I'm pretty close. Anybody have ideas or pointers? THANKS!



LinkBack URL
About LinkBacks


