Jump to content
  • Advertisement
Sign in to follow this  
3Dgonewild

[C] Safe way to read integers from file?

This topic is 4084 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
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?

Share this post


Link to post
Share on other sites
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..

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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 ?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!