Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > fscanf question

Reply
Thread Tools

fscanf question

 
 
Fallon
Guest
Posts: n/a
 
      07-12-2003
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-?

Much appreciated,
Jessica
 
Reply With Quote
 
 
 
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      07-12-2003
In 'comp.lang.c', http://www.velocityreviews.com/forums/(E-Mail Removed) (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-?


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- (E-Mail Removed) [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/
 
Reply With Quote
 
 
 
 
Jason
Guest
Posts: n/a
 
      07-12-2003
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.



 
Reply With Quote
 
Giuseppe
Guest
Posts: n/a
 
      07-13-2003
On 12 Jul 2003 09:33:58 -0700, (E-Mail Removed) (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-?


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

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Question about fscanf() behavior Gary Baydo C Programming 2 11-06-2005 11:31 PM
question abt fscanf siliconwafer C Programming 1 09-02-2005 12:48 AM
fscanf question - most of line -> null Cal Lidderdale C Programming 4 12-28-2004 08:43 AM
Question about fscanf .. Marc Reclaire C++ 2 12-18-2003 12:51 PM
Re: Simple fscanf Question Barry Schwarz C Programming 2 08-02-2003 08:42 AM



Advertisments