473,703 Members | 3,018 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how do i read odd numbered lines from a file

hi there,
can anyone tell me how to lines from a file which are odd numbered i.e.
1st,3rd,5th...l ines.

i tried incrementing file pointer by 2 (fp=fp+2)
but it does'nt work
Can someone give me the code please.

Jun 4 '06 #1
24 6142
ru******@gmail. com wrote:
hi there,
can anyone tell me how to lines from a file which are odd numbered i.e.
1st,3rd,5th...l ines.

i tried incrementing file pointer by 2 (fp=fp+2)
but it does'nt work
Can someone give me the code please.


You could just read in two lines to the same char-array, thereby
discarding the one you don't want. And the filepointer is incremented
the correct number of bytes each time.

So the problem with fp=fp+2 is that line lengths can vary and you don't
know each linelength on beforehand...
Best regards
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Jun 4 '06 #2
Martin Jørgensen said:
ru******@gmail. com wrote:
hi there,
can anyone tell me how to lines from a file which are odd numbered i.e.
1st,3rd,5th...l ines.

i tried incrementing file pointer by 2 (fp=fp+2)
but it does'nt work
Can someone give me the code please.
You could just read in two lines to the same char-array, thereby
discarding the one you don't want. And the filepointer is incremented
the correct number of bytes each time.


No, it isn't!

The file pointer is NOT incremented, because the file pointer does not point
to the file! I know it sounds like it does, but it really, really doesn't.
What it points to is some internal doodad that contains information about
the file. One of the items of information that internal doodad stores is a
"file position indicator" - and /that/ is incremented.
So the problem with fp=fp+2 is that line lengths can vary and you don't
know each linelength on beforehand...


No, the problem with fp=fp+2 is that it turns a valid pointer into an
invalid one for no gain. Please don't give advice unless you are absolutely
100% sure it's correct - and even then, please check in K&R or the Standard
to ensure that you are right to be sure.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 4 '06 #3
ru******@gmail. com wrote:
can anyone tell me how to lines from a file which are odd numbered i.e.
1st,3rd,5th... lines.

i tried incrementing file pointer by 2 (fp=fp+2)
but it does'nt work
Can someone give me the code please.


If by "incrementi ng the file pointer" you mean you did this:

#include <stdio.h>

...
FILE *fp = fopen(...);
...
fp = fp + 2;
...

That does not do what you want; Surprisingly, it increments the file
pointer, it does not read from the file, or modify the current
position in the file stream in any way.
After the increment fp will have a value 2*sizeof(FILE) bytes larger
than before, pointing to a memory area that most likely is not a FILE
structure and definitively is not the FILE structure you opened.
Attempting to read via fp will cause undefined behavior. (Because it
will modify random memory locations)

Hint: If you had in front of you a document (printed single side) and
you wanted to read only the odd numbered pages. How would you do it?
Jun 4 '06 #4
ru******@gmail. com wrote:
hi there,
can anyone tell me how to lines from a file which are odd numbered i.e.
1st,3rd,5th...l ines.
A line is defined in C as a sequence of characters terminated by a
newline character. While the actual end-of-line sequence varies between
operating systems, the C standard library ensures conversion to the
newline character.

So to read odd numbered lines, you'll have to read from the beginning
of the file and keep track of each line. You can store the odd lines
into your buffer while even lines can simply be read and discarded.
i tried incrementing file pointer by 2 (fp=fp+2) but it does'nt work
A pointer to type FILE is not a "file pointer" in the sense you seem to
understand. It points to an internal, implementation defined, data
structure which hold a lot of "meta" information about the file to
which it is attached. Merely changing this pointer's value will give
you pointer pointing to an unknown area of memory, which will
eventually cause undefined behaviour.
Can someone give me the code please.


This group helps those who make a sincere attempt, not those who simply
want to pass the course, by hook or by crook, even if it's only for a
mind-numbing BPO job.

So, try your best attempt, and if you encounter problems, post your
question and your code.

Jun 4 '06 #5
ru******@gmail. com wrote:
hi there,
can anyone tell me how to lines from a file which are odd numbered i.e.
1st,3rd,5th...l ines.
Others have given you some good advice already. Here is some pseudo-code:

FILE *fp = fopen(filename, "r");
if(fp)
{
char buf[1024];
while(fgets(buf , sizeof buf, fp))
{
/* discard the line we just read and read another */
if(!fgets(buf, sizeof buf, fp)) break;

do_something_wi th(buf);
}
}
i tried incrementing file pointer by 2 (fp=fp+2)
but it does'nt work


Sorry but this just made me laugh out loud!

The file pointer does not point into an array of lines in the file. It
points to some data structure that, in a system-specific way, holds
whatever state information is required about an open file, the current
position, and the buffering. A FILE* object should only ever hold either
a null pointer or the result of an fopen function.

There is a way to seek to a particular location in a file if you know
its byte offset. Unfortunately, there's no simple way to determine where
each line starts and ends, without reading all the characters in and
comparing them to '\n'. That's what the fgets function that I used above
does.

--
Simon.
Jun 4 '06 #6
On 2006-06-04, Simon Biber <ne**@ralmin.cc > wrote:
ru******@gmail. com wrote:
hi there,
can anyone tell me how to lines from a file which are odd numbered i.e.
1st,3rd,5th...l ines.
Others have given you some good advice already. Here is some pseudo-code:

That's some mighty C-looking psuedo-code. ;-)
FILE *fp = fopen(filename, "r");
if(fp)
{
char buf[1024];
while(fgets(buf , sizeof buf, fp))
{
/* discard the line we just read and read another */
if(!fgets(buf, sizeof buf, fp)) break;

do_something_wi th(buf);
}
}
i tried incrementing file pointer by 2 (fp=fp+2)
but it does'nt work


Sorry but this just made me laugh out loud!

It made me reread the original post a few times. Then I laughed.

From your code I read "sizeof buf", which is one of those obvious things
I never thought of. I would have always written 1024, and then changed
the number in two places every time I needed to. The unnecessary pains
I go through because I don't think...

--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
If we would just let the poachers into the zoo, we'd
have less squashed people and more fancy pianos!
Jun 4 '06 #7
Simon Biber wrote:
ru******@gmail. com wrote:
can anyone tell me how to lines from a file which are odd
numbered i.e. 1st,3rd,5th...l ines.


Others have given you some good advice already. Here is some
pseudo-code:

FILE *fp = fopen(filename, "r");
if(fp)
{
char buf[1024];
while(fgets(buf , sizeof buf, fp))
{
/* discard the line we just read and read another */
if(!fgets(buf, sizeof buf, fp)) break;

do_something_wi th(buf);
}
}


That has the problem of coping with the EOL actions of fgets. I
suggest ggets (see below for availability) is more suitable.

char *buffer;
FILE *fp;

if (fp = fopen(filename, "r") {
do {
if (0 != fggets(fp, &buffer) break;
free(buffer); /* discard odd numbered lines */
if (0 != fggets(fp, &buffer) break;
doSomethingWith (buffer);
free(buffer);
} while (1);
} /* untested */

See <http://cbfalconer.home .att.net/download/>

--
"Our enemies are innovative and resourceful, and so are we.
They never stop thinking about new ways to harm our country
and our people, and neither do we." -- G. W. Bush.
"The people can always be brought to the bidding of the
leaders. All you have to do is tell them they are being
attacked and denounce the pacifists for lack of patriotism
and exposing the country to danger. It works the same way
in any country." --Hermann Goering.

Jun 4 '06 #8
Richard Heathfield wrote:
Martin Jørgensen said:

ru******@gmai l.com wrote:
hi there,
can anyone tell me how to lines from a file which are odd numbered i.e.
1st,3rd,5th. ..lines.

i tried incrementing file pointer by 2 (fp=fp+2)
but it does'nt work
Can someone give me the code please.


You could just read in two lines to the same char-array, thereby
discarding the one you don't want. And the filepointer is incremented
the correct number of bytes each time.

No, it isn't!

The file pointer is NOT incremented, because the file pointer does not point
to the file! I know it sounds like it does, but it really, really doesn't.
What it points to is some internal doodad that contains information about
the file. One of the items of information that internal doodad stores is a
"file position indicator" - and /that/ is incremented.


I also meant that.
So the problem with fp=fp+2 is that line lengths can vary and you don't
know each linelength on beforehand...

No, the problem with fp=fp+2 is that it turns a valid pointer into an
invalid one for no gain. Please don't give advice unless you are absolutely
100% sure it's correct - and even then, please check in K&R or the Standard
to ensure that you are right to be sure.


That was my mistake... But please don't think that errors don't get
corrected by other people in this newsgroup and that it therefore is an
absolute catastropy to write comments to other peoples posts. This group
is large enough for both (or all three of us). I was thinking of some
FSEEK code I recently made where the file position can be incremented or
stored just as the OP had in mind and I don't see anything wrong in
telling him that the line lengths vary and therefore he can't do what he
probably thought he could do.
Best regards
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Jun 4 '06 #9
Simon Biber wrote:
ru******@gmail. com wrote:
hi there,
can anyone tell me how to lines from a file which are odd numbered i.e.
1st,3rd,5th...l ines.


Others have given you some good advice already. Here is some pseudo-code:

FILE *fp = fopen(filename, "r");
if(fp)
{
char buf[1024];
while(fgets(buf , sizeof buf, fp))
{
/* discard the line we just read and read another */
if(!fgets(buf, sizeof buf, fp)) break;

do_something_wi th(buf);
}
}


Note that this processes the 2nd, 4th, etc... lines, and at face value
it ignores
the possibility that a line may be longer than sizeof buf characters.

You can avoid storing the line unnecessarily by doing something like...

if (fscanf(fp, "%*[^\n]") != EOF)
fgetc(fp);

--
Peter

Jun 4 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3506
by: deko | last post by:
I have a text file ("eighty.txt") that looks like this: 83|84|85|86 I can read the file into an array like this: $numbers= file("eighty.txt"); But how do I key the array? I'd like to use strings as keys: three, four, five, six - do I add the key names in the file?
6
4294
by: Ruben | last post by:
Hello. I am trying to read a small text file using the readline statement. I can only read the first 2 records from the file. It stops at the blank lines or at lines with only spaces. I have a while statement checking for an empty string "" which I understand represents an EOF in Python. The text file has some blank lines with spaces and other with blanks. Thanks a lot.
2
6796
by: VM | last post by:
Is it possible to open an ascii file, read the first 499 lines, overwrite line 500 with the new string, and close the file? All the lines before and after line 500 will be untouched. Thanks.
0
406
by: rudranee | last post by:
hi there, can anyone tell me how to lines from a file which are odd numbered i.e. 1st,3rd,5th...lines. i tried incrementing file pointer by 2 (fp=fp+2) but it does'nt work Can someone give me the code please.
3
4217
by: Yaniv | last post by:
Hi I'm new in VB.NET. I wrote an application which opens a text file and read it all lines untill the EOF this file is open for read only and for sharing asllowed. every 5 seconds another applications write another line to this file at the end and I'm trying to read those new lines from my applications. the problem is that the EOF function returns TRUE even though a new lines added to this file.
6
3548
by: Kinbote | last post by:
Hi, I'm trying to make a function that opens a file, reads it in line by line, puts each line into an malloc'd array, and returns the array. I suspect I'm going about it in an atypical fashion, as I'm avoiding the use of fscanf and fgets to read in lines. I don't want to have to specify a temporary char* buffer to read in each line, and then have to concern myself with the (remote) possibility of overflows or with increasing the buffer...
9
7384
by: flebber | last post by:
I was working at creating a simple program that would read the content of a playlist file( in this case *.k3b") and write it out . the compressed "*.k3b" file has two file and the one I was trying to read was maindata.xml . I cannot however seem to use the gzip module correctly. Have tried the program 2 ways for no success, any ideas would be appreciated. Attempt 1 #!/usr/bin/python
5
1514
by: jgentes | last post by:
I really like the numbered code lines on this forum. I would like to incorporate something like this on my own site, and was wondering how exactly it was done? I am assuming that its creating some type of loop, delimiting by newline character, and inserting the n+1 where n is the number last new line character integer.
6
4526
by: bmerlover | last post by:
I'm trying to read each line, character by character, to determine if a line has a semicolon in it. I'm trying to count the number of lines that have a semicolon. In all of the files in one directory. I have 2 problems, one problem is it displays '.' and '..' and the subdirectory as files which I want it to avoid. I tried to use grep but I was unsuccessful. The second problem I'm having is that while it will read the amount of lines I have,...
0
8759
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8669
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9017
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6588
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5922
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4433
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4687
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3125
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.