473,406 Members | 2,345 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

fscanf question

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
3 7223
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Cal Lidderdale | last post by:
My input line is i1,i2,i3,i4,i5,i6,i7,i8^,...i596,597, ... 14101,14102...NL/CR very long line of data - I only want the first 8 items and the delimiter between 8 & 9 is a carrot "^". The line...
1
by: siliconwafer | last post by:
Hi All, here is one code: int main() { FILE*fp; unsigned long a; fp = fopen("my_file.txt","w+"); a = 24; fprintf(fp,"%ld",a); while(fscanf(fp,"%ld",&a) == 1) {
2
by: Gary Baydo | last post by:
The following program illustrates a question I have about fscanf() behavior: #include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { FILE *testfile; char s1 =...
7
by: blacksoil | last post by:
Hi, I have a question regarding reading data from a file and put it to a member of a class. I use fscanf. The class looks like this class myclass { double a;
7
by: ehui928 | last post by:
I use the following program to read some strings from an inupt file, and print them on the standard output. But the last string in the input file always printed twice, what is the reason and how...
3
by: totoro2468 | last post by:
Hi, I'm a complete NOOB. How do I get fscanf to copy into an array? I also need to use malloc, but where do I put it in my file? _______________________________________________________ FILE...
6
by: haloman | last post by:
Why does: char *myString = (char *) malloc(WORDLIMIT*sizeof(char)); fscanf(openFile, "%s", myString) != EOF; work yet char *myString = (char *) malloc(WORDLIMIT*sizeof(char));...
10
by: Roman Zeilinger | last post by:
Hi I have a beginner question concerning fscanf. First I had a text file which just contained some hex numbers: 0C100012 0C100012 ....
59
by: David Mathog | last post by:
Apologies if this is in the FAQ. I looked, but didn't find it. In a particular program the input read from a file is supposed to be: + 100 200 name1 - 101 201 name2 It is parsed by reading...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.