[C] Safe way to read integers from file?

Started by
6 comments, last by 3Dgonewild 17 years ago
Hello again. Can someone tell me how to read a whole line of integers(255 chars in length) in C? Thanks.
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?
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..
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
A priest, a rabbi, and a monkey walk into a bar. The bartender says," hey whats going on here, is this some kinda joke?"
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 ?
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!
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
A priest, a rabbi, and a monkey walk into a bar. The bartender says," hey whats going on here, is this some kinda joke?"
Thanks Bob & John! , it works now!

This topic is closed to new replies.

Advertisement