C-programming

Hi, I´ve had some problems completing a program that I´ve worked with for a while now. Basically, i need to read data from a textfile with 30 beers each containing 10 elements(each element is seperated by a comma). This data is supposed to be stored in a struct with maximum capacity of 100 items(beers). Can someone explain how you´re supposed to solve it? Here is an example of my input file(1 beer):

82476,Watermelon Dorado Double India Pale Ale,31.90,355.00,Ale brittisk-amerikansk stil,Imperial/Dubbel IPA,Flaska,USA,Ballast Point Brewing,10.00


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);
	

}
Last edited on
Please post a small sample of your input file.

Do you realize that lines 30 - 41 are all invoking undefined behavior since none of the variables have been initialized?

82476,Watermelon Dorado Double India Pale Ale,31.90,355.00,Ale brittisk-amerikansk stil,Imperial/Dubbel IPA,Flaska,USA,Ballast Point Brewing,10.00
Here is an example of my input file.

yes I do realize that, but i cant figure out a solution...
Last edited on
yes I do realize that, but i cant figure out a solution...

Maybe you should wait to use pch until after you start processing the line?

Also I hope you realize that your strncpy() is also incorrect. Do you realize that strlen(tok) could be much larger that the size of your array? You should be using the size of your array not the length of the incoming string.

actually no, that´s why im asking for help haha. This is a project that is supposed to be done in a group. But my groupmates are lazy and I´m the only one working on it, with my limited skill in C. I could really use som detailed solution/alternatives so i can pass this class somehow. I know that I´m the worst but I`ve been trying for weeks. I´m desperate....
This is a project that is supposed to be done in a group. But my groupmates are lazy and I´m the only one working on it, with my limited skill in C.

Then I recommend that you talk to your instructor about your group. Also even working with a group you should of all been exposed to about the same material so you should have more of an idea of how to proceed than what you've shown, IMO, reading a CSV file in C is not a beginning project, unless the proper framework has been supplied.

I suggest you find and study the documentation for the functions you're trying to use, strncpy(), strtok(), strtol(), strtod() etc. There are quite a few places to find the documentation and most of these sites even have decent example programs illustrating how to use those functions.



Topic archived. No new replies allowed.