Followers 0

[C] Safe way to read integers from file?

7 posts in this topic

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

Share on other sites
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?
0

Share on other sites
Quote:
 Original post by crestyIf 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..
0

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
0

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

Share on other sites
Quote:
 Original post by 3DgonewildThanks 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.
0

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
0

Share on other sites
Thanks Bob & John! , it works now!
0

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account