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

Not reading data

P: n/a

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
--
Posted via http://dbforums.com
Nov 13 '05 #1
Share this Question
Share on Google+
15 Replies


P: n/a
djj858 <me*********@dbforums.com> wrote:
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
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #2

P: n/a
djj858 <me*********@dbforums.com> scribbled the following:
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 (pa*****@cc.helsinki.fi) ------------- 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
Nov 13 '05 #3

P: n/a

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!
--
Posted via http://dbforums.com
Nov 13 '05 #4

P: n/a
djj858 <me*********@dbforums.com> scribbled the following:
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 */
}

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Hasta la Vista, Abie!"
- Bart Simpson
Nov 13 '05 #5

P: n/a
djj858 wrote:
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[i].a, &atom[i].b, &atom[i].c))
i++;
} while (!feof(f));

Jirka

Nov 13 '05 #6

P: n/a
In <sp********************************@4ax.com> Irrwahn Grausewitz <ir*******@freenet.de> writes:
djj858 <me*********@dbforums.com> wrote:
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #7

P: n/a
Da*****@cern.ch (Dan Pop) wrote:
In <sp********************************@4ax.com> Irrwahn Grausewitz <ir*******@freenet.de> writes:
[Assuming you're talking about text files]


How do you define a line in a binary file?


In a file format description, which may vary from that of C text
streams.
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #8

P: n/a

"Joona I Palaste" <pa*****@cc.helsinki.fi> schrieb im Newsbeitrag
news:bn**********@oravannahka.helsinki.fi...
djj858 <me*********@dbforums.com> 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
Nov 13 '05 #9

P: n/a
In <35****************@dbforums.com> djj858 <me*********@dbforums.com> writes:
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #10

P: n/a
I wrote:
do {
if (3 == fscanf(f, "ATOM%d%d%d ", &atom[i].a, &atom[i].b, &atom[i].c))
i++;
else fscanf(f, "%*[^\n] ");
} while (!feof(f));


Jirka

Nov 13 '05 #11

P: n/a
In <bn**********@mamenchi.zrz.TU-Berlin.DE> Jirka Klaue <jk****@ee.tu-berlin.de> writes:
I wrote:
do {
if (3 == fscanf(f, "ATOM%d%d%d ", &atom[i].a, &atom[i].b, &atom[i].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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #12

P: n/a
In <pb********************************@4ax.com> Irrwahn Grausewitz <ir*******@freenet.de> writes:
Da*****@cern.ch (Dan Pop) wrote:
In <sp********************************@4ax.com> Irrwahn Grausewitz <ir*******@freenet.de> writes:
[Assuming you're talking about text files]


How do you define a line in a binary file?


In a file format description, which may vary from that of C text
streams.


Most binary file format descriptions I'm familiar with don't define lines
at all.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #13

P: n/a
Da*****@cern.ch (Dan Pop) wrote:
In <pb********************************@4ax.com> Irrwahn Grausewitz <ir*******@freenet.de> writes:
Da*****@cern.ch (Dan Pop) wrote:
In <sp********************************@4ax.com> Irrwahn Grausewitz <ir*******@freenet.de> writes:

[Assuming you're talking about text files]

How do you define a line in a binary file?


In a file format description, which may vary from that of C text
streams.


Most binary file format descriptions I'm familiar with don't define lines
at all.


That wouldn't keep me from providing one. Laziness OTOH does. :)
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #14

P: n/a
Jirka Klaue <jk****@ee.tu-berlin.de> wrote in message news:<bn**********@mamenchi.zrz.TU-Berlin.DE>...
djj858 wrote:
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[i].a, &atom[i].b, &atom[i].c))
i++;
} while (!feof(f));

Jirka


that is incorrect usage of feof
- nethlek
Nov 13 '05 #15

P: n/a
ne*****@tokyo.com (Mantorok Redgormor) wrote:
Jirka Klaue <jk****@ee.tu-berlin.de> wrote in message news:<bn**********@mamenchi.zrz.TU-Berlin.DE>...
struct atom {
int a, b, c;
} atom[42];

int i = 0;

do {
if (3 == fscanf(f, "ATOM%d%d%d ", &atom[i].a, &atom[i].b, &atom[i].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
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.