473,231 Members | 1,878 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,231 software developers and data experts.

Not reading data


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
15 2155
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
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

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
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
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
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
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

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

Similar topics

2
by: Dariusz | last post by:
Below is part of a code I have for a database. While the database table is created correctly (if it doesn't exist), and data is input correctly into the database when executed, I have a problem...
0
by: Andy | last post by:
Hi, In the code below (not pretty I know but it's an early version :-P) I'm having problems reading the data object back in. If I move the reading code to immediately after the section where it...
1
by: Magnus | last post by:
allrite folks, got some questions here... 1) LAY-OUT OF REPORTS How is it possible to fundamentaly change the lay-out/form of a report in access? I dont really know it that "difficult", but...
6
by: KevinD | last post by:
assumption: I am new to C and old to COBOL I have been reading a lot (self teaching) but something is not sinking in with respect to reading a simple file - one record at a time. Using C, I am...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
10
by: Tyler | last post by:
Hello All: After trying to find an open source alternative to Matlab (or IDL), I am currently getting acquainted with Python and, in particular SciPy, NumPy, and Matplotlib. While I await the...
5
blazedaces
by: blazedaces | last post by:
Ok, so you know my problem, java is running out of memory reading with SAX, the event-based xml parser intended more-so than DOM for extremely large files. I'll try to explain what I've been doing...
4
by: Shark | last post by:
Hi, I need a help. My application reads data from COM port, this data is then parsed and displyed on: 1. two plotters 2. text box. I'm using Invoke method to update UI when new data is...
13
by: swetha | last post by:
HI Every1, I have a problem in reading a binary file. Actually i want a C program which reads in the data from a file which is in binary format and i want to update values in it. The file...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.