my program not responding , need input below.
text file:
mu-547 china eastern 10.55 1.20 every day, via shianghai + 1 day mu-541 china eastern 13.50 1.20 every day, via shianghai + 1 day ci-835 china airlines 9.40 16.00 every day, via taipei ci-065 china airlines 16.25 1.10 every day, via taipei sq-970 singapore airlines 20.50 8.40 every day, via singapore sq-972 singapore airlines 20.50 11.05 every day, via singapore sq-974 singapore airlines 20.50 14.35 every day, via singapore sq-976 singapore airlines 20.50 17.55 every day, via singapore sq-978 singapore airlines 20.50 20.05 every day, via singapore cx-751 cathay pacific 9.15 16.20 every day, via hong kong cx-701 cathay pacific 10.45 17.55 every day, via hong kong
#include <stdio.h> typedef struct { char id[7]; char airlinename[31]; float arrive,depart; char notes[100]; } airline; void openfile(const char *data2) { airline plane[12] = {0}; int = 0; file *file = fopen(data2, "r"); if (file) { char line[83]; while(fgets(line, sizeof line, file) && < 6) { fputs(line, stdout); if(sscanf(line, "%6s %30c%f%f%99c", plane[i].id, plane[i].airlinename, plane[i].arrive, plane[i].depart, plane[i].notes) == 5) { printf(" %s ", plane[i].id); printf(" %c ", plane[i].airlinename); printf(" %f ", plane[i].arrive); printf(" %f ", plane[i].depart); printf(" %c ", plane[i].notes); i++; } } fclose(file); } else perror(data2); } int main(void) { openfile("data2.txt"); return 0; }
when scanning float %f address of variable needed , there missing & values.
scanning %c not terminate variable '\0' , printf can problem.
format "%6s %30[^0-9]%f%f %99[^\n]" scan 6 characters scan 30 characters not digits, scan 2 floats , 99 characters stopping @ newline.
#include <stdio.h> typedef struct { char id[7]; char airlinename[31]; float arrive,depart; char notes[100]; } airline; void openfile(const char *data2) { airline plane[12] = {{{0}}}; int = 0; file *file = fopen(data2,"r"); if ( file ) { char line[83]; while( < 6 && fgets(line,sizeof line, file)) { fputs(line, stdout); if( sscanf(line,"%6s %30[^0-9]%f%f %99[^\n]", plane[i].id, plane[i].airlinename, &plane[i].arrive, // needed & &plane[i].depart, // needed & plane[i].notes) == 5 ) { printf(" %s ",plane[i].id); printf(" %s ",plane[i].airlinename); // use %s string printf(" %f ",plane[i].arrive); printf(" %f ",plane[i].depart); printf(" %s ",plane[i].notes); i++; } } fclose(file); } else { perror(data2); } } int main(void) { openfile("data2.txt"); return 0; }
Comments
Post a Comment