Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › Copy a text file to a structure?
New Posts  All Forums:Forum Nav:

Copy a text file to a structure?

post #1 of 9
Thread Starter 
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.
Edited by Vagrant Storm - 6/11/08 at 3:10pm
My System
(16 items)
 
  
CPUMotherboardGraphicsGraphics
2600K @ 4.7GHz Asus P8P67 B3 GTX 580 EVGA Hydro Copper 2 GTX 580 EVGA  
RAMHard DriveOptical DriveOS
4x4GB G.Skill Ripjaws bunch of 'em Blu-Ray For movies Windows 8.1 
MonitorMonitorKeyboardPower
24.4" Hans G HH251 X2 Yamakasi DS270  Blah 1000watt Super Flower 
CaseMouseMouse PadAudio
Built into Desk Microsoft SideWinder X8 Comfy one... Creative Extreme Gamer 
  hide details  
Reply
My System
(16 items)
 
  
CPUMotherboardGraphicsGraphics
2600K @ 4.7GHz Asus P8P67 B3 GTX 580 EVGA Hydro Copper 2 GTX 580 EVGA  
RAMHard DriveOptical DriveOS
4x4GB G.Skill Ripjaws bunch of 'em Blu-Ray For movies Windows 8.1 
MonitorMonitorKeyboardPower
24.4" Hans G HH251 X2 Yamakasi DS270  Blah 1000watt Super Flower 
CaseMouseMouse PadAudio
Built into Desk Microsoft SideWinder X8 Comfy one... Creative Extreme Gamer 
  hide details  
Reply
post #2 of 9
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
}

Edited by FearMeansControl - 6/11/08 at 12:45pm
Jesus PC v5.3
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7-950 Gigabyte X58A-UD3R 5850 GSkill 1600 MHz 4x4GB 
Hard DriveOptical DriveOSMonitor
3x1200.12 500GB RAID 0, 2x2TB Deskstar RAID 1 Samsung Writemaster SATA Gentoo, Windows 7 x64 3x 21.5" Samsungs 
PowerCaseMouse
Thermaltake 750W RocketPwn G500 
  hide details  
Reply
Jesus PC v5.3
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7-950 Gigabyte X58A-UD3R 5850 GSkill 1600 MHz 4x4GB 
Hard DriveOptical DriveOSMonitor
3x1200.12 500GB RAID 0, 2x2TB Deskstar RAID 1 Samsung Writemaster SATA Gentoo, Windows 7 x64 3x 21.5" Samsungs 
PowerCaseMouse
Thermaltake 750W RocketPwn G500 
  hide details  
Reply
post #3 of 9
here

fgets works fine
Edited by Indian_Engineer - 6/11/08 at 12:44pm
p
(13 items)
 
  
CPUMotherboardGraphicsRAM
Athlon X2 5000+ BE @ 3.0GHz BIOSTAR TForce TF570SLI 2 X PNY 9800GX2 1GB 512-bit 8GB OCZ Gold DDR2 800 
Hard DriveOptical DriveOSMonitor
36GB Raptor X 4 in RAID10 20x DVD+RW Slackware 12.1 Dual Samsung 24" WS LCD 
KeyboardPowerCaseMouse
IBM Model M PC P&C 610 Silencer (12V@49A) Ultra Aluminus Full Tower Logitech G5 
Mouse Pad
Ratpadz GS 
  hide details  
Reply
p
(13 items)
 
  
CPUMotherboardGraphicsRAM
Athlon X2 5000+ BE @ 3.0GHz BIOSTAR TForce TF570SLI 2 X PNY 9800GX2 1GB 512-bit 8GB OCZ Gold DDR2 800 
Hard DriveOptical DriveOSMonitor
36GB Raptor X 4 in RAID10 20x DVD+RW Slackware 12.1 Dual Samsung 24" WS LCD 
KeyboardPowerCaseMouse
IBM Model M PC P&C 610 Silencer (12V@49A) Ultra Aluminus Full Tower Logitech G5 
Mouse Pad
Ratpadz GS 
  hide details  
Reply
post #4 of 9
Thread Starter 
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.
My System
(16 items)
 
  
CPUMotherboardGraphicsGraphics
2600K @ 4.7GHz Asus P8P67 B3 GTX 580 EVGA Hydro Copper 2 GTX 580 EVGA  
RAMHard DriveOptical DriveOS
4x4GB G.Skill Ripjaws bunch of 'em Blu-Ray For movies Windows 8.1 
MonitorMonitorKeyboardPower
24.4" Hans G HH251 X2 Yamakasi DS270  Blah 1000watt Super Flower 
CaseMouseMouse PadAudio
Built into Desk Microsoft SideWinder X8 Comfy one... Creative Extreme Gamer 
  hide details  
Reply
My System
(16 items)
 
  
CPUMotherboardGraphicsGraphics
2600K @ 4.7GHz Asus P8P67 B3 GTX 580 EVGA Hydro Copper 2 GTX 580 EVGA  
RAMHard DriveOptical DriveOS
4x4GB G.Skill Ripjaws bunch of 'em Blu-Ray For movies Windows 8.1 
MonitorMonitorKeyboardPower
24.4" Hans G HH251 X2 Yamakasi DS270  Blah 1000watt Super Flower 
CaseMouseMouse PadAudio
Built into Desk Microsoft SideWinder X8 Comfy one... Creative Extreme Gamer 
  hide details  
Reply
post #5 of 9
Thread Starter 
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.
My System
(16 items)
 
  
CPUMotherboardGraphicsGraphics
2600K @ 4.7GHz Asus P8P67 B3 GTX 580 EVGA Hydro Copper 2 GTX 580 EVGA  
RAMHard DriveOptical DriveOS
4x4GB G.Skill Ripjaws bunch of 'em Blu-Ray For movies Windows 8.1 
MonitorMonitorKeyboardPower
24.4" Hans G HH251 X2 Yamakasi DS270  Blah 1000watt Super Flower 
CaseMouseMouse PadAudio
Built into Desk Microsoft SideWinder X8 Comfy one... Creative Extreme Gamer 
  hide details  
Reply
My System
(16 items)
 
  
CPUMotherboardGraphicsGraphics
2600K @ 4.7GHz Asus P8P67 B3 GTX 580 EVGA Hydro Copper 2 GTX 580 EVGA  
RAMHard DriveOptical DriveOS
4x4GB G.Skill Ripjaws bunch of 'em Blu-Ray For movies Windows 8.1 
MonitorMonitorKeyboardPower
24.4" Hans G HH251 X2 Yamakasi DS270  Blah 1000watt Super Flower 
CaseMouseMouse PadAudio
Built into Desk Microsoft SideWinder X8 Comfy one... Creative Extreme Gamer 
  hide details  
Reply
post #6 of 9
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
Edited by rabidgnome229 - 6/11/08 at 4:04pm
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
post #7 of 9
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);
}
If count will always go 1-> some number the code can change a bit
Edited by rabidgnome229 - 6/11/08 at 4:17pm
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
post #8 of 9
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.
Edited by Stormwolf - 6/12/08 at 1:56am
post #9 of 9
Quote:
Originally Posted by Stormwolf View Post
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.
Very true. My code assumes that the number of inputs is well known which may or may not be the case.
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › Copy a text file to a structure?