Close

Storing a CSV text file into a C Struct

I came across a problem where I wanted to grab some data off a CSV text file (see sample below) and plug it into a data structure, but — and there’s always a “but” — I had to do it in ANSI C. Eyeroll. Doing it in Python would be a snap:

1
2
3
4
5
6
7
8
9
10
path = "players.txt"	
dbfile = file(path, "r")	
players = []
 
for line in dbfile:
	temp = line.split(",")
	temp[4] = float(temp[4])
	players.append(temp)
 
dbfile.close()

Since I *had* to do it in C anyway, I decided I wanted to use Structs; I haven’t used those in a while (ok, never — but I love structs!) and since this is a learning exercise anyway, I thought (as I am wont to do) why the hell not? Of course I immediately threw that line of reasoning out the window by arbitrarily setting the struct array to 5 items because I didn’t want to shoot a bullet in my brainpan *squish* do a bunch of malloc()s and calloc()s. Anyway

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
#include<stdio.h>
 
typedef struct Player{
	char 	*name;
	char 	*team;
	char 	*position;
	int 	number;
	double salary;
} My_Player;
 
int main(void){
 
	My_Player players[5];
	char *path = "players.txt";
	char tmpstr[254];
	FILE *dbfile;
	int line_counter = 0;
	int parse_counter = 0;
	char *temp;
 
	dbfile = fopen(path, "r");
 
	while(!feof(dbfile)){
		if(fgets(tmpstr, 255, dbfile)){
			temp = strtok(tmpstr, ",");
			while(temp != NULL){
 
				switch(parse_counter){
					case 0:
						players[line_counter].name = temp;
						break;
					case 1:
						players[line_counter].team = temp;
						break;
					case 2:
						players[line_counter].position = temp;
						break;
					case 3:
						players[line_counter].number = temp;
						break;
					case 4:
						players[line_counter].salary = atof(temp);
						break;
				}
				parse_counter++;
 
				temp = strtok(NULL, ",");
 
			}
			printf("%s", players[line_counter].name);
			parse_counter = 0;
			line_counter++;
		}
	}
	fclose(dbfile);
 
	return 0;
}

Now I *seriously* hope there’s a better way to do the strtok() without passing it through a clunky switch statement and resetting the parse_counter variable after every line; reader (yes, singular), enlighten me please!

Here’s the sample data file (players.txt)

Jason Kidd,Dallas Mavericks,Point Guard,5,21000000
Kevin Garnett,Boston Celtics,Power Forward,5,22000000
Kobe Bryant,LA Lakers,Shooting Guard,24,19000000
Shane Battier,Houston Rockets,Small Forward,31,6864200
Andris Biedrins,Golden State Warriors,Center,15,9000000

And the source (be gentle): nba.py nba.c

  • del.icio.us
  • Facebook
  • Google Bookmarks
  • StumbleUpon
  • Technorati
  • Tumblr
  • Twitter

Related posts:

  1. More practice: sorting
  2. No Fun with Roman Numerals
  3. Client From Hell
  4. What’s Eating the Mavs?
  5. Chris Benoit tragedy

Or, you can just browse

This post has no comments yet. Be the first? →

Post a Comment

Your email is never shared. Required fields are marked *

*
*

By submitting a comment here you grant this site a perpetual license to reproduce your words and name/web site in attribution.