1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <locale.h>
void main(void)
{
char *oneline, *tok;
char envara[256];
char delim[] = ",";
FILE *fp;
int i;
struct vara
{
int nummer;
char namn[100];
float pris;
float volym;
char typ[100];
char stil[100];
char forpackning[20];
char land[20];
char producent[50];
float alkoholhalt;
} items[100];
items[i].nummer = atoi(tok);
items[i].pris = atof(tok);
items[i].volym = atof(tok);
items[i].alkoholhalt = atof(tok);
strncpy(&items[i].namn, tok, strlen(tok));
strncpy(&items[i].typ, tok, strlen(tok));
strncpy(&items[i].stil, tok, strlen(tok));
strncpy(&items[i].forpackning, tok, strlen(tok));
strncpy(&items[i].land, tok, strlen(tok));
strncpy(&items[i].producent, tok, strlen(tok));
if ((fp = fopen("varor.csv", "r")) == NULL)
{
fprintf(stderr, "Filen varor.csv gick inte att öppna\n");
exit(-1);
}
for (i = 0; i < 100 && fgets(envara, 256, fp); i++)
{
envara[strlen(envara) - 1] = '0'; // Ta bort radslutstecknet
printf("%s\n\n", envara);
oneline = strdup(envara);
tok = strtok(oneline, delim);
while (tok != NULL)
{
printf("%s\n", tok);
tok = strtok(NULL, delim);
}
}
free(oneline); free(tok);
fclose(fp);
}
|