473,394 Members | 1,794 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,394 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 2164
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.