Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming

Reply
 
LinkBack Thread Tools
Old 06-11-08   #1 (permalink)
New to Overclock.net
 
Vagrant Storm's Avatar
 
intel nvidia

Join Date: Nov 2005
Location: Rochester, MN
Posts: 1,837

Rep: 85 Vagrant Storm is acknowledged by some
Unique Rep: 81
Folding Team Rank: 403
Trader Rating: 2
Default Copy a text file to a structure?

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!!!

System: My System
CPU
E8500 @ 4GHz
Motherboard
DFI X48 T2RS
Memory
4 Gigs Gskill DDR2 800
Graphics Card
8800GTS 320MB
Hard Drive
3x250GB WD in RAID 5
Sound Card
Xtreme Gamer
Power Supply
750 watt Silverstone
Case
Gigabyte Aurora
CPU cooling
Appogee
GPU cooling
Stock
OS
Vista 64 Ultimate
Monitor
19" Hans G wide screen

Last edited by Vagrant Storm : 06-11-08 at 06:10 PM.
Vagrant Storm is offline I fold for Overclock.net   Reply With Quote
Old 06-11-08   #2 (permalink)
Linux Lobbyist
 
FearMeansControl's Avatar
 
intel nvidia

Join Date: Aug 2007
Location: Wrong Island, NY
Posts: 951

Rep: 68 FearMeansControl is acknowledged by some
Unique Rep: 59
Folding Team Rank: 103
Trader Rating: 2
Default

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:
Originally Posted by ENTERPRISE View Post
I would reccomend opening task manager and closing the process ''IntelFanboy.exe''
Q6600
(450x8) (417x9) (425x9)

System: Jesus PC v5.3
CPU
Q6600
Motherboard
Gigabyte X38-DS4
Memory
OCZ PC-8000 2x2 GB
Graphics Card
Quadro FX-4600 & FX-1400
Hard Drive
3x2500JS RAID 0, 2x7200.11 500GB RAID 1
Sound Card
Presonus Firepod + Yamaha HS-5M's
Power Supply
OCZ StealthxStream 600W
Case
RocketPwn
CPU cooling
Fuzion v2, mcw-30, GTX 360, D5, CD Waterfall Res.
GPU cooling
Stock, re-applied with AS5
OS
Gentoo, XP x64
Monitor
Samsung Syncmaster 2253LW + 2x17" LCD's

Last edited by FearMeansControl : 06-11-08 at 03:45 PM.
FearMeansControl is offline I fold for Overclock.net   Reply With Quote
Old 06-11-08   #3 (permalink)
New to Overclock.net
 
amd nvidia

Join Date: May 2008
Posts: 164

Rep: 7 Indian_Engineer Unknown
Unique Rep: 6
Trader Rating: 0
Default

here

fgets works fine
__________________
System: p
CPU
Athlon X2 5000+ BE @ 3.0GHz
Motherboard
BIOSTAR TForce TF570SLI
Memory
8GB OCZ Gold DDR2 800
Graphics Card
2 X PNY 9800GX2 1GB 512-bit
Hard Drive
36GB Raptor X 4 in RAID10
Sound Card
DiamondTek XtremeSound 7.1
Power Supply
PC P&C 610 Silencer (12V@49A)
Case
Ultra Aluminus Full Tower
CPU cooling
Stock
GPU cooling
Stock
OS
Slackware 12.1
Monitor
Dual Samsung 24" WS LCD

Last edited by Indian_Engineer : 06-11-08 at 03:44 PM.
Indian_Engineer is offline   Reply With Quote
Old 06-11-08   #4 (permalink)
New to Overclock.net
 
Vagrant Storm's Avatar
 
intel nvidia

Join Date: Nov 2005
Location: Rochester, MN
Posts: 1,837

Rep: 85 Vagrant Storm is acknowledged by some
Unique Rep: 81
Folding Team Rank: 403
Trader Rating: 2
Default

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!!!

System: My System
CPU
E8500 @ 4GHz
Motherboard
DFI X48 T2RS
Memory
4 Gigs Gskill DDR2 800
Graphics Card
8800GTS 320MB
Hard Drive
3x250GB WD in RAID 5
Sound Card
Xtreme Gamer
Power Supply
750 watt Silverstone
Case
Gigabyte Aurora
CPU cooling
Appogee
GPU cooling
Stock
OS
Vista 64 Ultimate
Monitor
19" Hans G wide screen
Vagrant Storm is offline I fold for Overclock.net   Reply With Quote
Old 06-11-08   #5 (permalink)
New to Overclock.net
 
Vagrant Storm's Avatar
 
intel nvidia

Join Date: Nov 2005
Location: Rochester, MN
Posts: 1,837

Rep: 85 Vagrant Storm is acknowledged by some
Unique Rep: 81
Folding Team Rank: 403
Trader Rating: 2
Default

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!!!

System: My System
CPU
E8500 @ 4GHz
Motherboard
DFI X48 T2RS
Memory
4 Gigs Gskill DDR2 800
Graphics Card
8800GTS 320MB
Hard Drive
3x250GB WD in RAID 5
Sound Card
Xtreme Gamer
Power Supply
750 watt Silverstone
Case
Gigabyte Aurora
CPU cooling
Appogee
GPU cooling
Stock
OS
Vista 64 Ultimate
Monitor
19" Hans G wide screen
Vagrant Storm is offline I fold for Overclock.net   Reply With Quote
Old 06-11-08   #6 (permalink)
Kernel Sanders
 
rabidgnome229's Avatar
 
intel nvidia

Join Date: Feb 2006
Location: Pittsburgh
Posts: 4,900
Blog Entries: 1

Rep: 549 rabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famous
Unique Rep: 327
FAQs Submitted: 6
Trader Rating: 5
Default

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
__________________
BIG BROTHER
I put on my robe and wizard hat...

IS WATCHING

System: It goes to eleven
CPU
E6300
Motherboard
DS3
Memory
2GB XMS2 DDR2-800
Graphics Card
EVGA 8600GTS
Hard Drive
1.294 TB
Sound Card
Audigy 2 ZS
Power Supply
Corsair 520HX
Case
Lian-Li v1000B Plus
CPU cooling
TTBT
GPU cooling
Thermalright V2
OS
Arch Linux/XP
Monitor
Samsung 226bw

Last edited by rabidgnome229 : 06-11-08 at 07:04 PM.
rabidgnome229 is online now Overclocked Account   Reply With Quote
Old 06-11-08   #7 (permalink)
Kernel Sanders
 
rabidgnome229's Avatar
 
intel nvidia

Join Date: Feb 2006
Location: Pittsburgh
Posts: 4,900
Blog Entries: 1

Rep: 549 rabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famous
Unique Rep: 327
FAQs Submitted: 6
Trader Rating: 5
Default

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
__________________
BIG BROTHER
I put on my robe and wizard hat...

IS WATCHING

System: It goes to eleven
CPU
E6300
Motherboard
DS3
Memory
2GB XMS2 DDR2-800
Graphics Card
EVGA 8600GTS
Hard Drive
1.294 TB
Sound Card
Audigy 2 ZS
Power Supply
Corsair 520HX
Case
Lian-Li v1000B Plus
CPU cooling
TTBT
GPU cooling
Thermalright V2
OS
Arch Linux/XP
Monitor
Samsung 226bw

Last edited by rabidgnome229 : 06-11-08 at 07:17 PM.
rabidgnome229 is online now Overclocked Account   Reply With Quote
Old 06-12-08   #8 (permalink)
New to Overclock.net
 
Join Date: May 2008
Location: Nottingham, UK
Posts: 24

Rep: 7 Stormwolf Unknown
Unique Rep: 5
Trader Rating: 0
Default

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.
Stormwolf is offline   Reply With Quote
Old 06-12-08   #9 (permalink)
Kernel Sanders
 
rabidgnome229's Avatar
 
intel nvidia

Join Date: Feb 2006
Location: Pittsburgh
Posts: 4,900
Blog Entries: 1

Rep: 549 rabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famous
Unique Rep: 327
FAQs Submitted: 6
Trader Rating: 5
Default

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.
__________________
BIG BROTHER
I put on my robe and wizard hat...

IS WATCHING

System: It goes to eleven
CPU
E6300
Motherboard
DS3
Memory
2GB XMS2 DDR2-800
Graphics Card
EVGA 8600GTS
Hard Drive
1.294 TB
Sound Card
Audigy 2 ZS
Power Supply
Corsair 520HX
Case
Lian-Li v1000B Plus
CPU cooling
TTBT
GPU cooling
Thermalright V2
OS
Arch Linux/XP
Monitor
Samsung 226bw
rabidgnome229 is online now Overclocked Account   Reply With Quote
Reply

Tags
c programming, read from file



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -4. The time now is 02:39 PM.


Overclock.net is a Carbon Neutral Site Creative Commons License Internet Security By ControlScan

Terms of Service / Forum Rules | Privacy Policy | Advertising | Become an Official Vendor
Copyright © 2008 Shogun Interactive Development. Most rights reserved.
Page generated in 1.61597 seconds with 9 queries