|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming | |
Copy a text file to a structure?
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | |||||||||||||
|
New to Overclock.net
|
I am trying to make a program in C that will take a text file and load it into some structured variables.
So like: struct { int count[25]; char workday[25]; int month[25]; int day[25] } and then I'll have a .txt file with data like 1 Monday 6 10 2 Tuesday 6 11 3 Wednesday 6 12 ...ect how can I go about combining the two? fgets maybe? but that has trouble when the integer come first... I am sure I'll figure it out, but I any pointers would be great.
__________________
If it ain't broke...MAKE IT GO FASTER!!!
Last edited by Vagrant Storm : 06-11-08 at 06:10 PM. |
|||||||||||||
|
|
|
|
#2 (permalink) | ||||||||||||||
|
Linux Lobbyist
|
are you trying to get specific lines or get it in order?
EDIT: Btw, fgets should be fine with integers... Code:
while (fgets(intVariable, numberOfInts, fileVariable) != NULL)
{
your function
}
__________________
Quote:
(450x8) (417x9) (425x9)![]()
Last edited by FearMeansControl : 06-11-08 at 03:45 PM. |
||||||||||||||
|
|
|
|
#3 (permalink) | |||||||||||||
|
New to Overclock.net
|
__________________
Last edited by Indian_Engineer : 06-11-08 at 03:44 PM. |
|||||||||||||
|
|
|
|
|
#4 (permalink) | |||||||||||||
|
New to Overclock.net
|
oh really? hmm I thought it fgets gets messed up if it reads an integer and then reads an empty space...oh well thanks for setting me straight there.
and yes I only need to get it in order. Once it is loaded I will make changes, sort it, and then over write the old file.
__________________
If it ain't broke...MAKE IT GO FASTER!!!
|
|||||||||||||
|
|
|
|
#5 (permalink) | |||||||||||||
|
New to Overclock.net
|
Hmmm...
after thinking about this, fgets won't help unless I made each line in the .txt file a string, but I need to get an integer, then a string, and then two more integers. I don't think fgets will help. Will I have to parse it all in there some how? uff this is getting to be a bigger pain the more I think about it.
__________________
If it ain't broke...MAKE IT GO FASTER!!!
|
|||||||||||||
|
|
|
|
#6 (permalink) | |||||||||||||
|
Apple Doesn't Love You
|
A text file is one giant string. Use fgets to read each line into a buffer, then tokenize it with strtok and parse the ints with atoi
Last edited by rabidgnome229 : 06-11-08 at 07:04 PM. |
|||||||||||||
|
|
|
|
#7 (permalink) | |||||||||||||
|
Apple Doesn't Love You
|
two second code (may not work)
Assume WHATEVER has been defined somewhere, and the buffers can get smaller if you choose Code:
typedef struct {
int count[25];
char workday[25];
int month[25];
int day[25]
} thingy;
thingy t_array[WHATEVER];
FILE *infile;
char buffer[1024], token[32];
int i;
//Open file etc
for(i=0; fgets(buffer, 1024, infile); ++i){
token = strtok(buffer, " ");
t_array[i].count = atoi(token);
t_array[i].workday = strtok(NULL, " ");
token = strtok(NULL, " ");
t_array[i].month = atoi(token);
token = strtok(NULL, " ");
t_array[i].day = atoi(token);
}
Last edited by rabidgnome229 : 06-11-08 at 07:17 PM. |
|||||||||||||
|
|
|
|
#8 (permalink) |
|
New to Overclock.net
|
If you do it the above way, you need to change the loop test condition to include a check on "i < WHATEVER" as well or you may end up writing past the end of your array space.
The problem with reading from a file is you never know in advance exactly how many elements you're going to have to deal with... so reading into a static array is either wasteful (i.e. you allocate much more space than you require) or dangerous (you don't allocate enough and blow off the stack). You could decide to include the number of elements as a part of the file header protocol (i.e. the first line of the file is an integer specifying the number of records) and then dynamically allocate the memory for the array (just don't forget to free it again when you're done) Also you need to tell the compiler what the max stack size will be - the compiler default stack size is usually 1MB. If the array size exceeds this limit then the program will throw a stack overflow error... and 1MB aint that much (a 640 x 480 32bit 'bitmap' image will exceed 1MB). If you use a dynamic array then you need to do the same for the heap. Another way of doing it is to read into a 'temp' copy of the structure, then add (copy) it to a linked list. The list should take care of the memory allocation / deallocation for you. Last edited by Stormwolf : 06-12-08 at 04:56 AM. |
|
|
|
|
|
#9 (permalink) | ||||||||||||||
|
Apple Doesn't Love You
|
Quote:
|
||||||||||||||
|
|
![]() |
| Tags |
| c programming, read from file |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|