Not reading data

D

djj858

Another newbie question:



How do I begin reading data, but starting from the xth line down a
list? In other words, how do I skip the first lines and not read in
those values?



Thanks
 
I

Irrwahn Grausewitz

djj858 said:
How do I begin reading data, but starting from the xth line down a
list? In other words, how do I skip the first lines and not read in
those values?

[Assuming you're talking about text files]

Unless you're already maintaining an index to your file, the following
applies:

To skip lines you must count them. To count lines you have to count
line delimiters ('newlines'). To count newlines, you have to read the
data, e.g:

/* Untested code! */

#include <stdio.h>

int SkipLines( FILE *fp, unsigned int lines )
{
int c = 0;
while ( lines && (c = fgetc( fp )) != EOF )
if ( c == '\n' )
lines--;
return c == EOF ? 0 : 1;
}

Regards
 
J

Joona I Palaste

djj858 said:
Another newbie question:
How do I begin reading data, but starting from the xth line down a
list? In other words, how do I skip the first lines and not read in
those values?

Usually, you can't. You have to fake it by not caring at the first (x-1)
lines you read. I could be of more help if I knew how you were reading
the data, and from what.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"'So called' means: 'There is a long explanation for this, but I have no
time to explain it here.'"
- JIPsoft
 
D

djj858

Some clarification:



eg of file being read:



REMARK These are some lines of rubbish

REMARK that continue for a while

ATOM 123 123 2312

ATOM 123 123 2312



I want to skip the REMARKs at the beginning and read in the ATOM values.
The way I am doing it at the moment is to manually delete the REMARKs
from the file beforehand and then feeding it into the program using
fscanf. There has to be a better way!
 
J

Joona I Palaste

djj858 said:
Some clarification:
eg of file being read:
REMARK These are some lines of rubbish
REMARK that continue for a while
ATOM 123 123 2312
ATOM 123 123 2312
I want to skip the REMARKs at the beginning and read in the ATOM values.
The way I am doing it at the moment is to manually delete the REMARKs
from the file beforehand and then feeding it into the program using
fscanf. There has to be a better way!

There's gotta be a better way, sang Frankie Goes To Hollywood. And there
is. When you have fscanfed from the file to a string, call it s, just
use this kind of mechanism:

if (strncmp(s, "REMARK", 6) == 0) {
/* skip the line */
}
else {
/* actually do stuff with the line */
}
 
J

Jirka Klaue

djj858 said:
Some clarification:

eg of file being read:

REMARK These are some lines of rubbish
REMARK that continue for a while
ATOM 123 123 2312
ATOM 123 123 2312

I want to skip the REMARKs at the beginning and read in the ATOM values.
The way I am doing it at the moment is to manually delete the REMARKs
from the file beforehand and then feeding it into the program using
fscanf. There has to be a better way!

struct atom {
int a, b, c;
} atom[42];

int i = 0;

do {
if (3 == fscanf(f, "ATOM%d%d%d ", &atom.a, &atom.b, &atom.c))
i++;
} while (!feof(f));

Jirka
 
D

Dan Pop

In said:
djj858 said:
How do I begin reading data, but starting from the xth line down a
list? In other words, how do I skip the first lines and not read in
those values?

[Assuming you're talking about text files]

How do you define a line in a binary file?

Dan
 
D

Dan Pop

In said:
How do I begin reading data, but starting from the xth line down a
list? In other words, how do I skip the first lines and not read in
those values?

You can't entirely ignore these lines, you must scan the input file for
newline characters, even if you don't attempt to interpret the contents
of the lines in any other way.

The alternative is to have an accompanying index file, containing the
offset of each line of text in the main file, as returned by ftell (or
fgetpos if you have to support very large files). The index file can be
a binary file, so you can immediately compute the offset corresponding
to the information about a certain line number in the main file.

It is very easy, and an excellent exercise for a beginner, to create
and use such an index file, in order to allow the random access to
any line in a plain text file.

Dan
 
R

Robert Stankowic

Joona I Palaste said:
djj858 <[email protected]> scribbled the following: [....]

There's gotta be a better way, sang Frankie Goes To Hollywood. And there
is. When you have fscanfed from the file to a string, call it s, just
^^^^^^^^

Joona, that's great. Seriously
:)))
Robert
 
D

Dan Pop

In said:
I said:
do {
if (3 == fscanf(f, "ATOM%d%d%d ", &atom.a, &atom.b, &atom.c))
i++;


else fscanf(f, "%*[^\n] ");
} while (!feof(f));


Still broken, if the input is not *exactly* as expected. Consider the
behaviour on the following input:

ATOM 1 3
5 ATOM 1
2 3

Most of the time, a trailing space in a scanf format is NOT what you want.
And fscanf is (almost) as difficult to use in a bullet-proof manner
as scanf itself.

Dan
 
M

Mantorok Redgormor

Jirka Klaue said:
djj858 said:
Some clarification:

eg of file being read:

REMARK These are some lines of rubbish
REMARK that continue for a while
ATOM 123 123 2312
ATOM 123 123 2312

I want to skip the REMARKs at the beginning and read in the ATOM values.
The way I am doing it at the moment is to manually delete the REMARKs
from the file beforehand and then feeding it into the program using
fscanf. There has to be a better way!

struct atom {
int a, b, c;
} atom[42];

int i = 0;

do {
if (3 == fscanf(f, "ATOM%d%d%d ", &atom.a, &atom.b, &atom.c))
i++;
} while (!feof(f));

Jirka


that is incorrect usage of feof


- nethlek
 
I

Irrwahn Grausewitz

Jirka Klaue said:
struct atom {
int a, b, c;
} atom[42];

int i = 0;

do {
if (3 == fscanf(f, "ATOM%d%d%d ", &atom.a, &atom.b, &atom.c))
i++;
} while (!feof(f));

Jirka


that is incorrect usage of feof


Jirka's code has problems, one of which he already corrected in a reply
to his own post, the other being a more general one with malformed
input, as Dan Pop pointed out.

The usage of feof is not a problem. What made you think so?
Did you try the code?

Regards
 

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
474,263
Messages
2,571,064
Members
48,769
Latest member
Clifft

Latest Threads

Top