By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,079 Members | 880 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,079 IT Pros & Developers. It's quick & easy.

fscanf question

P: n/a
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
Nov 13 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
In 'comp.lang.c', fa********@hotmail.com (Fallon) wrote:
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;
}
}

--
-ed- em**********@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 13 '05 #2

P: n/a
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.

Nov 13 '05 #3

P: n/a
On 12 Jul 2003 09:33:58 -0700, fa********@hotmail.com (Fallon) wrote:
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
Nov 13 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.