Jump to content

  • Log In with Google      Sign In   
  • Create Account

[C] Safe way to read integers from file?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 3Dgonewild   Members   -  Reputation: 174

Like
0Likes
Like

Posted 19 April 2007 - 07:46 AM

Hello again. Can someone tell me how to read a whole line of integers(255 chars in length) in C? Thanks.

Sponsor:

#2 cresty   Members   -  Reputation: 122

Like
0Likes
Like

Posted 19 April 2007 - 08:02 AM

If you are referring to an int which contains 255 chararacters i cannto help you.

However when reading an int from a file it will read every character that is an number until it encounters one that isnt.

for example scanning an int from a file with the string "823a237846" would get the value of 823.

EDIT: I think it would also stop if the int in question could not hold anymore numbers (like 255 numbers long).

EDIT2: Maybe you are meaning you want to read 255 different integers each containing one digit?

#3 3Dgonewild   Members   -  Reputation: 174

Like
0Likes
Like

Posted 19 April 2007 - 09:48 AM

Quote:
Original post by cresty
If you are referring to an int which contains 255 chararacters i cannto help you.

However when reading an int from a file it will read every character that is an number until it encounters one that isnt.

for example scanning an int from a file with the string "823a237846" would get the value of 823.

EDIT: I think it would also stop if the int in question could not hold anymore numbers (like 255 numbers long).

EDIT2: Maybe you are meaning you want to read 255 different integers each containing one digit?



255 is a random length. i just need to read a hole number. for example :

My file.txt has the following text :

1200000

I want to read that number..

#4 MrBob   Members   -  Reputation: 143

Like
0Likes
Like

Posted 19 April 2007 - 10:00 AM

Void Extract_integers(char *file)
{
File *ptrFile;
char buffer[255];

ptrFile=fopen(file,"r");
fgets(buffer,sizeof(buffer),ptrFile);
fscanf(ptrFile,"%d",//your integer variable here);
}

this code will read all integers (from one line of a file) into your variable of choosing until it hits a non number character.

you may need to debug the code a bit i just tossed it together, but this is the basics of what u need. another fscanf will read the next line of the file.

Enjoy


#5 3Dgonewild   Members   -  Reputation: 174

Like
0Likes
Like

Posted 19 April 2007 - 12:28 PM

Thanks but its not working.

Here's the code:

int readLINE(char *file)
{
FILE *F;
char buffer[255];
int r=0;
F=fopen(file,"r");
fgets(buffer,sizeof(buffer),F);
fscanf(F,"%d",r);
return r;
}
and in main :

int main()
{
int t=readLINE("file2.txt");
//t returns 0 :/
}




..By the way , how can i write a line ?

#6 JohnBolton   Members   -  Reputation: 1372

Like
0Likes
Like

Posted 19 April 2007 - 12:59 PM

Quote:
Original post by 3Dgonewild
Thanks but its not working.

Here's the code:
*** Source Snippet Removed ***

Try making these changes:
    int readLINE(char *file)
{
FILE *F;
char buffer[255];
int r=0;
F=fopen(file,"r");
if ( F != NULL )
{
fgets(buffer,sizeof(buffer),F);
sscanf(buffer,"%d",&r);
fclose(f);
}
return r;
}
There are many ways to do it depending on exactly what you want to do. You could also write it like this (though it wouldn't read an entire line):
    int readLINE(char *file)
{
FILE *F;
int r=0;
F=fopen(file,"r");
if ( F != NULL )
{
fscanf(F,"%d",&r);
fclose(f);
}
return r;
}
There are many ways to do it depending on exactly what you want to. Use fprintf() to write to a file.


John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!

#7 MrBob   Members   -  Reputation: 143

Like
0Likes
Like

Posted 19 April 2007 - 01:03 PM

the code i gave you was good just get rid of this line

fgets(buffer,sizeof(buffer),F);

this is for putting one line of your file into an array for example...

in file2.txt the first line of the file is "hello my name is bob"
if you look in the buffer array you make you would see
buffer[0]='h'
buffer[1]='e'
"
"
"
buffer[n]='b'

hope this helps

#8 3Dgonewild   Members   -  Reputation: 174

Like
0Likes
Like

Posted 19 April 2007 - 01:17 PM

Thanks Bob & John! , it works now!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS