fscanf question

F

Fallon

If someone could help it would be greatly appreciated.

I am trying to write a simple program to read in elements from an
input file formatted as follows:

3
4
81
5
6
45
54
7
..
..
..
(etc)

Now I'd like to be able to extract the first, third, or any ordered
element?
How would I do this, I've looked at all documentation for fscanf and
haven't been able to find the answer. I know I can read in the first
element using:

fscanf(fd, "%d", &x)


Or the entire list can be read in using a for loop of something like:

for(i=0; i < number; i++)
if (fscanf(fd, "%d", &x)==EOF)
break;


But how can I access specific ordered elements, or for that matter a
specific range of elements from the input file (e.g. elements 4-8)?

Much appreciated,
Jessica
 
E

Emmanuel Delahaye

In said:
I am trying to write a simple program to read in elements from an
input file formatted as follows:

3
4
81
5
6
45
54
7
.
.
.
(etc)

Now I'd like to be able to extract the first, third, or any ordered
element?
How would I do this, I've looked at all documentation for fscanf and
haven't been able to find the answer. I know I can read in the first
element using:

fscanf(fd, "%d", &x)


Or the entire list can be read in using a for loop of something like:

for(i=0; i < number; i++)
if (fscanf(fd, "%d", &x)==EOF)
break;


But how can I access specific ordered elements, or for that matter a
specific range of elements from the input file (e.g. elements 4-8)?

Use the i value to 'filter' the required actions

int x;
int i;

for (i = 0; i < number; i++)
{
if (fscanf(fd, "%d", &x) == 1)
{
/* filter */
switch (i + 1)
{
case 1:
/* 1st process */
break;

case 3:
/* 3rd process */
break;
}
default:
/* ignore */
;
}
else
{
break;
}
}
 
J

Jason

You could count each item after using fscanf, and determine which items you
want to keep.

If you really do need to access elements directly from a file, then you
could use fopen in binary mode ... fopen("file", "rb"); and use the fseek
function to get to the desired value. The file will not be in such a
favoriable format if you want to edit it in a text editor though.
 
G

Giuseppe

If someone could help it would be greatly appreciated.

I am trying to write a simple program to read in elements from an
input file formatted as follows:

3
4
81
5
6
45
54
7
.
.
.
(etc)
Now I'd like to be able to extract the first, third, or any ordered
element?
How would I do this, I've looked at all documentation for fscanf and
haven't been able to find the answer. I know I can read in the first
element using:

fscanf(fd, "%d", &x)
Or the entire list can be read in using a for loop of something like:

for(i=0; i < number; i++)
if (fscanf(fd, "%d", &x)==EOF)
break;
But how can I access specific ordered elements, or for that matter a
specific range of elements from the input file (e.g. elements 4-8)?

you could count '\n' in the file
_____
ABBASSO L'Europa e il governo globale del mondo
 

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,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top