PDA

View Full Version : C Help Required


kennez
10-27-2005, 11:02 PM
This is not exactly graphics related programming, but I wanted to ask here because the community is very helpful.

I have a mini-assignment to do at University over the next two weeks or so, to be written in C, but the trouble is, I haven't written a line of C in nearly 2 years, so I am extremely rusty. Anyway, here is what I need help with:

I have data in a file in the following format (comma seperated)

Origin,Destination,Flight No.,Aircraft type,Days of week,Departure time,Arrival time,Begin Date,End Date

Essentially, what I have to do is read the data contained in this file into an array in order to run a search function on it.

The file is absolutely huge, but we were told that if we can get it to work on about 10 lines of data, there is no reason why it shouldn't work with a larger file.

I have absolutely no idea how to go about reading this data into an array. I have a C book which briefly covers opening files, but it doesn't mention comma-seperated data in files.

Any help on this really would be appreciated, as I have absolutely no idea how to do this. Once I have the data in an array, I should be able to muddle through it, but getting started is the problem.

Thanks in advance for your help.

-Vormav-
10-27-2005, 11:46 PM
See if this page helps you any:
http://www.eskimo.com/~scs/cclass/notes/sx6c.html

The basic idea is that getchar() reads in from the file one character at a time. So, if you wanted to break things apart into different array elements and have these be separated by a comma within your file, you could modify the getchar() loop such that it starts adding the data to a new array element whenever it sees a comma.
In the example from the link at the top, they're doing something similar, by checking to see when they're at the end of a line (the char would be /n).

Of course, if you wanted to be able to read in any amount of data, you'd have to look into dynamic memory allocation as well... For that, check out this page: http://www.cs.cf.ac.uk/Dave/C/node11.html

Hope that helps.

fred_lemaster
10-28-2005, 06:01 AM
Check out the strtok() function from the string.h header.

kennez
10-28-2005, 08:05 AM
Thanks for the links, Vormav.

fred_lemaster: Do you have any links that describe how to use strtok(), or any code samples? I've never come across that function before, and, as I said, my C is quite rusty, having not touched it for nearly two years.

Thanks.

fred_lemaster
10-28-2005, 08:22 AM
Unix manpage:
http://www.opengroup.org/onlinepubs/007908799/xsh/strtok.html

Example using strtok()
http://codenewbie.com/forum/showthread.php?t=3740&highlight=strtok: (http://codenewbie.com/forum/showthread.php?t=3740&highlight=strtok)
EDIT: This example is in C++, but the use of strtok is consistent with it's use in C, you would just have to aquire the string in C fashion rather than using C++ iostreams.
This post linked to from that one is a pure C example:
http://codenewbie.com/forum/showthread.php?t=3103&highlight=strtok

Hope that helps.

CGTalk Moderation
10-28-2005, 08:22 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.