Questions

T

Tinker

Does anyone know how to read a txt file which is created before and
inside the files only hv numbers and did not structured.
how can i only read the secound integer such as the patten inside the
txt file is 11 12 13 and i only want the programme to display the 12
on the screen?
Thanks a lot!Please help!
 
J

Jens.Toerring

Tinker said:
Does anyone know how to read a txt file which is created before and
inside the files only hv numbers and did not structured.
how can i only read the secound integer such as the patten inside the
txt file is 11 12 13 and i only want the programme to display the 12
on the screen?

FILE *fp;
int number;
int count = 0;

if ( ( fp = fopen( "file.dat", "r" ) ) == NULL )
exit( EXIT_FAILURE );

while ( fscanf( "%d", &number ) == 1 && ++count < 2 )
/* empty */ ;

if ( count != 2 )
exit( EXIT_FAILURE );

printf( "The number is %d\n", number );

Take care, this will also pick up numbers embedded in text like
"abc234xyz".
Regards, Jens
 
J

j

FILE *fp;
int number;
int count = 0;

if ( ( fp = fopen( "file.dat", "r" ) ) == NULL )
exit( EXIT_FAILURE );

while ( fscanf( "%d", &number ) == 1 && ++count < 2 )
/* empty */ ;

Or simply eliminate the use of ``count''
and use ``*'' along with a conversion
specifier that is followed by the apprioriate
conversion specifier for the value that he wants.
 
C

CBFalconer

Tinker said:
Does anyone know how to read a txt file which is created before
and inside the files only hv numbers and did not structured. how
can i only read the secound integer such as the patten inside the
txt file is 11 12 13 and i only want the programme to display the
12 on the screen?

What are hv numbers? High voltage?
 
A

Arthur J. O'Dwyer

What are hv numbers? High voltage?

No, no, the word "hv" is clearly being used as a verb in that sentence!
I respectfully submit that it is intended as an abbreviation of "hover"
--- i.e., the numbers are actually hovering inside the files. (The OP
is obviously going to be interested in the 'float' datatype.)

-Arthur
 
T

Tinker

j said:
Or simply eliminate the use of ``count''
and use ``*'' along with a conversion
specifier that is followed by the apprioriate
conversion specifier for the value that he wants.


Thanks
But how about if i want to read the secound number of each line?
Please help
 
J

j

Tinker said:
"j" <[email protected]> wrote in message


Thanks
But how about if i want to read the secound number of each line?
Please help

Then you would want to exclude everything that follows
the second number up to a new line.

so you can use,

#include <stdio.h>

int main(void)
{
int num;

while((fscanf(stdin, "%*s %d %*[^\n]", &num)) == 1)
printf("Number: %d\n", num);

return 0;
}

or with a different variation of the format string:
fscanf(stdin, "%*d %d %*[^\n]", &num);
fscanf(stdin, "%*[^ ] %d %*[^\n]", &num);

You need to take into account your programs behaviour
if your file will have spurious data. e.g., ``aaaaa 12 1000''
or whitespace that proceeds the second number.
Then construct the appropriate format string
to attain the desired behaviour that you want for
your program.
 

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,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top