how to read integers from a text file

R

rohit

Hi All,
I am new to C language.I want to read integers from a text file and
want to do some operation in the main program.To be more specific I
need to multiply each of these integers with another set of integers
stored in an array.It would be a great help if you could provide some
code for it.I tried the function fscanf but by that I am able to read
only the first integer of the text file.Please help me.
 
J

Jens Thoms Toerring

nembo kid said:
rohit ha scritto:
Place each integer in a line and popolate an array v[] with these

For fscanf() to work they don't have to be on separate lines.
fscanf() skips all white-space characters, i.e. spaces, tabs
and line end characters (or character combinations) etc.
values. Could be as follows:
/* ... */
#define DIM 100 /* max 100 integers */
int v[DIM];
/* 'n' is the number of integers (lines) max DIM values */
/* It is specified in the first line */
/* fpin is the pointer to FILE */
for (j=0;j<n;j++)
{
if(fscanf(fpin, "%d\n", &v[j])!=1)
{
fprintf(stderr, "\nData corrupted");
return 1;
}
}

Or, if you don't know how many integers there are in the file
(but also assuming that you know an upper limit 'DIM'):

#include <stdio.h>
#include <stdlib.h>

(...)

int count = 0;

(...)

while ( count < DIM && fscanf( fpin, "%d", v + count ) == 1 )
count++;

if ( ferror( fpin ) )
{
fprintf( stderr, "Read failure\n" );
exit( EXIT_FAILURE );
}

But it would be much better if you would show what you did
try yourself. In that case you would get an explanation of
why it didn't work and how to improve it. And that, in the
long run, will help you much more than just blindly using
other peoples code...
Regards, Jens
 
K

Kenny McCormack

Hi All,
I am new to C language.I want to read integers from a text file and
want to do some operation in the main program.To be more specific I
need to multiply each of these integers with another set of integers
stored in an array.It would be a great help if you could provide some
code for it.I tried the function fscanf but by that I am able to read
only the first integer of the text file.Please help me.

<OT>I think you'll find this a lot easier to do it in AWK, than
bothering with C.

It sounds to me like what you are doing is a perfect fit for AWK.
 
B

Barry Schwarz

Hi All,
I am new to C language.I want to read integers from a text file and
want to do some operation in the main program.To be more specific I
need to multiply each of these integers with another set of integers
stored in an array.It would be a great help if you could provide some
code for it.I tried the function fscanf but by that I am able to read
only the first integer of the text file.Please help me.


Remove del for email
 
S

santosh

rohit said:
Hi All,
I am new to C language.I want to read integers from a text file and
want to do some operation in the main program.To be more specific I
need to multiply each of these integers with another set of integers
stored in an array.It would be a great help if you could provide some
code for it.I tried the function fscanf but by that I am able to read
only the first integer of the text file.Please help me.

Since the integer values are represented in text you need to, in
essence, read each value as a sequence of characters and then convert
the sequence to the value it represents, before storing away your
value. You will need to do this for every integer represented in your
file.

The fscanf function will do the reading and the conversion in one call
for you, but can be a bit difficult to use. In particular the sequence
of characters that fscanf reads from the file must match what it is
expecting from it's format string. The function can fail if it
encounters unexpected characters (like say an alphabet). You can (and
should) check the return value of fscanf after every call to it, before
proceeding.

Another method is to read in a line of text (one or more characters
terminated by a '\n' character) with the fgets function and then supply
the line (or a part of that line if multiple numbers are represented in
each line) to a conversion function like strtol. If you use atoi be
aware that it produces undefined behaviour with certain values and does
not indicate failure. The functions strtol and strtoul are really much
more robust.

Also you could read the file character by character with a function like
getc and do the conversion to a int value yourself, but it's usually
better to use standardised functions unless you're learning or have
special requirements.

Please provide us with the code for your attempted solution and an exact
description of the format of your text file to help you further.
 
B

Barry Schwarz

Hi All,
I am new to C language.I want to read integers from a text file and
want to do some operation in the main program.To be more specific I
need to multiply each of these integers with another set of integers
stored in an array.It would be a great help if you could provide some
code for it.I tried the function fscanf but by that I am able to read
only the first integer of the text file.Please help me.

Show your code.


Remove del for email
 
R

rahul

If the integers are stored as text in the given file, you can read
them in a buffer and parse the individual items using sscanf. If you
want to directly go for fread, then it more or less requires that the
data be written in the same format by fwrite or something ( owing to
the endian-ness and the variable size of data types ).

No one likes to write code to explain something to someone. It would
be far easier for everyone if you post your own code snippets.
 
K

Keith Thompson

rio said:
Jens Thoms Toerring said:
nembo kid said:
rohit ha scritto:
I tried the function fscanf but by that I am able to read
only the first integer of the text file.
Place each integer in a line and popolate an array v[] with these

For fscanf() to work they don't have to be on separate lines.
fscanf() skips all white-space characters, i.e. spaces, tabs
and line end characters (or character combinations) etc.
values. Could be as follows:

but fscanf can catch the overflow or a wrong input?
something like:

2222222222222222222222222222222222222222222222222222222222

No. C99 7.19.6.2p10:

If this object does not have an appropriate type, or if the result
of the conversion cannot be represented in the object, the
behavior is undefined.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top