简体   繁体   中英

reading multiple lines from file into array of structs c

I'm trying to read data from a file into an array of structures to use later for a hash table. I finally stored to data from 1st line into a struct successfully, however, when reading from 2nd line, the output of the information is not correct.

this is part of the sample data

"K300" "Keyboard" "US Generic" 150.00 50

"R576" "16-inch Rims" "Toyota Verossa" 800.00 48

"HL412" "Headlight" "Ford Taurus 2010" 600.00 52

and this is the code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define n 99

typedef struct partInfo {
  char number[6];
  char name[20];
  char description[30];
  double price;
  int qty;
}Part;

Part part[n]; 

int main() {

char num[6], name[20], desc[30], space[10];
int q, i = 0;
double p;
char ch;

FILE * in = fopen("input.txt", "r");

fscanf(in,"\"%[^\"]", &num);
               fscanf(in,"\"%[^\"]", &space);

               fscanf(in, "\"%[^\"]", &name);
               fscanf(in,"\"%[^\"]", &space);

               fscanf(in, " \"%[^\"]s\"", desc);
               fscanf(in, "%[^ ]s", &space);

               fscanf(in,"%lf", &p);
               fscanf(in, "%[^ ]s", &space);

               fscanf(in, " %d", &q);
               fscanf(in, "%[^\n]s", &space);

               strcpy(part[i].number, num);
               strcpy(part[i].name, name);
               strcpy(part[i].description, desc);
               part[i].price = p;
               part[i].qty = q;


printf("%s\n", part[i].number);
printf("%s\n", part[i].name);
printf("%s\n", part[i].description);
printf("%.2f\n", part[i].price);
printf("%d\n\n", part[i].qty);
fscanf(in,"%[^\n]", &space); // right here seems to be the problem getting to next line

               fscanf(in, "\"%[^\"]", &name);
               fscanf(in,"\"%[^\"]", &space);

               fscanf(in, " \"%[^\"]s\"", desc);
               fscanf(in, "%[^ ]s", &space);

               fscanf(in,"%lf", &p);
               fscanf(in, "%[^ ]s", &space);
               fscanf(in, " %d", &q);

               i++;
               strcpy(part[i].number, num);
               strcpy(part[i].name, name);
               strcpy(part[i].description, desc);
               part[i].price = p;
               part[i].qty = q;

printf("%s\n", part[i].number);
printf("%s\n", part[i].name);
printf("%s\n", part[i].description);
printf("%.2f\n", part[i].price);
printf("%d\n\n", part[i].qty);
fscanf(in,"\"%[^\"]", &space);

fclose(in);
return 0;
}

I didn't want to use a while loop as yet, I wanted to see if I can get the code working properly before implementing the while loop

you can do that like this.

fscanf(in, " \"%[^\"]", num);
fscanf(in, "\"%[^\"]s", space);

fscanf(in, " \"%[^\"]", name);
fscanf(in, "\"%[^\"]",space);

fscanf(in, " \"%[^\"]", desc);
fscanf(in, "%[^ ]s", space);

fscanf(in, "%lf", &p);
fscanf(in, "%[^ ]s",space);

fscanf(in, "%d", &q);
fscanf(in, "%[^\n]s", &space);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM