473,406 Members | 2,352 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,406 software developers and data experts.

How do i found out the number of lines in a file

Presently I define the number of lines to be expected in a file when
defining the array size and the initialisation of this array. This works
fine for now, but i'm sure that in the future this could change. So rather
than explicitly define the number expected, the file is read and the number
defined that way.

So I have a csv file that I can read in, how can I work out the number of
lines in the file? Is there a function that can do this, or do I need to
code this myself?

Vas.
Nov 14 '05 #1
18 1928
"Vasilis Serghi" <vs*****@jaguar.com> writes:
So I have a csv file that I can read in, how can I work out the
number of lines in the file? Is there a function that can do this,
or do I need to code this myself?


Just count the number of \n characters in the file (or whatever
character combination the system uses for "end of line").

From what you say it seems you already know the maximum length of a
line. If not, you can add a counter to keep track of how long the
longest line is, to be sure to allocate enough memory.

--
Stefano | Department of Psychology, University of Bologna
Ghirlanda | Interdisciplinary cultural research, Stockholm University
http://www.intercult.su.se/~stefano
Nov 14 '05 #2
On Thu, 19 Feb 2004 10:24:55 -0000
"Vasilis Serghi" <vs*****@jaguar.com> wrote:
Presently I define the number of lines to be expected in a file when
defining the array size and the initialisation of this array. This
works fine for now, but i'm sure that in the future this could change.
If you were sure that it would not change then you can be certain some
abstrad would change it :-)
So rather
than explicitly define the number expected, the file is read and the
number defined that way.

So I have a csv file that I can read in, how can I work out the number
of lines in the file? Is there a function that can do this, or do I
need to code this myself?


You will have to work it out yourself by counting the lines as you read
them in. If you are trying to load all of the data in to memory then you
will need to malloc the space then realloc as necessary.
--
Flash Gordon
Paid to be a Geek & a Senior Software Developer
Although my email address says spam, it is real and I read it.
Nov 14 '05 #3
Stefano Ghirlanda <st***************@unibo.it> spoke thus:
From what you say it seems you already know the maximum length of a
line. If not, you can add a counter to keep track of how long the
longest line is, to be sure to allocate enough memory.


Who needs to allocate memory? fgetc() can handle this job quite well.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #4


Vasilis Serghi wrote:
Presently I define the number of lines to be expected in a file when
defining the array size and the initialisation of this array. This works
fine for now, but i'm sure that in the future this could change. So rather
than explicitly define the number expected, the file is read and the number
defined that way.

So I have a csv file that I can read in, how can I work out the number of
lines in the file? Is there a function that can do this, or do I need to
code this myself?


You would code it yourself using some functions from the Standard C library.

You could experiment with dynamically allocated storage for the array as you
read the data file. And, as you do this keep track of the number of elements
allocated. How this is done depends on the array data type and the file format.

You could put this all in a struct data type.
For example, if the data was type int:

typedef struct DATA
{
int *array; /* holds the data */
size_t size; /* keep track of the number of elements */
}DATA;

Write a function to add data to the array.

int *AddDATA(DATA *p, int data)
{
int *tmp;
size_t cnt = p->size;

if((tmp = realloc(p->array, (cnt+1)*sizeof *tmp)) == NULL) return NULL;
p->array = tmp;
p->array[p->size++] = data;
return &p->array[cnt];
}

In main you would declare the struct:
DATA myData = {NULL};

open the file and as you read an item of data, call the function and store the
data dyamically in
the array.

Beware that even dynamic allocations are not limitless. Available memory space
may be
exhausted to the point where no more storage can be made available for the
array.The
function return of NULL would indicated the failure of allocation.

Nov 14 '05 #5
In <87************@jesus.intercult.su.se> Stefano Ghirlanda <st***************@unibo.it> writes:
"Vasilis Serghi" <vs*****@jaguar.com> writes:
So I have a csv file that I can read in, how can I work out the
number of lines in the file? Is there a function that can do this,
or do I need to code this myself?


Just count the number of \n characters in the file (or whatever
character combination the system uses for "end of line").


If he opens the file in text mode, as he is supposed to do, it's '\n'
*regardless* of the local system conventions. It's the job of the C
runtime support to transparently handle the conversions between '\n'
and whatever the local system uses, on text streams (but not on binary
streams).

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #6
Da*****@cern.ch (Dan Pop) writes:
If he opens the file in text mode, as he is supposed to do, it's
'\n' *regardless* of the local system conventions. It's the job of
the C runtime support to transparently handle the conversions
between '\n' and whatever the local system uses, on text streams
(but not on binary streams).


Even better. I was uncertain whether this was the case, so I threw in
a warning.

--
Stefano | Department of Psychology, University of Bologna
Ghirlanda | Interdisciplinary cultural research, Stockholm University
http://www.intercult.su.se/~stefano
Nov 14 '05 #7
In article <87************@jesus.intercult.su.se>,
Stefano Ghirlanda <st***************@unibo.it> wrote:
"Vasilis Serghi" <vs*****@jaguar.com> writes:
So I have a csv file that I can read in, how can I work out the
number of lines in the file? Is there a function that can do this,
or do I need to code this myself?


Just count the number of \n characters in the file (or whatever
character combination the system uses for "end of line").


But beware that the number lines may be 1 more than the number
of \n characters if the file does not end in \n.

The C standard assumes that a text stream is terminated by a
newline. Your CSV file, however, may have been created by
a utility which may not have heard of the C standard.

--
Rouben Rostamian
Nov 14 '05 #8
Lewis Bowers wrote:
You could experiment with dynamically allocated storage for the array as you
read the data file. And, as you do this keep track of the number of elements
allocated. How this is done depends on the array data type and the file format.

I would also strongly consider a linked list for storing the strings. If
your access to the strings once loaded is essentially sequential, then
that's a viable solution.
Brian Rodenborn
Nov 14 '05 #9
In <c1**********@pc18.math.umbc.edu> ro****@pc18.math.umbc.edu (Rouben Rostamian) writes:
In article <87************@jesus.intercult.su.se>,
Stefano Ghirlanda <st***************@unibo.it> wrote:
"Vasilis Serghi" <vs*****@jaguar.com> writes:
So I have a csv file that I can read in, how can I work out the
number of lines in the file? Is there a function that can do this,
or do I need to code this myself?


Just count the number of \n characters in the file (or whatever
character combination the system uses for "end of line").


But beware that the number lines may be 1 more than the number
of \n characters if the file does not end in \n.


Does that extra bunch of characters after the last newline qualify as a
line?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #10
"Vasilis Serghi" <vs*****@jaguar.com> wrote:
# Presently I define the number of lines to be expected in a file when
# defining the array size and the initialisation of this array. This works
# fine for now, but i'm sure that in the future this could change. So rather
# than explicitly define the number expected, the file is read and the number
# defined that way.
#
# So I have a csv file that I can read in, how can I work out the number of
# lines in the file? Is there a function that can do this, or do I need to
# code this myself?

Reallocate the array of lines as need.

char *a,**line = 0; int m = 0, n= 0;

a = next line of input;
if (n+1>=m) {m = 2*(n+1); line = realloc(line,sizeof(char*)*m);}
line[n++] = a;

--
Derk Gwen http://derkgwen.250free.com/html/index.html
A bunch of savages in this town.
Nov 14 '05 #11
nrk
Vasilis Serghi wrote:
Presently I define the number of lines to be expected in a file when
defining the array size and the initialisation of this array. This works
fine for now, but i'm sure that in the future this could change. So rather
than explicitly define the number expected, the file is read and the
number defined that way.

So I have a csv file that I can read in, how can I work out the number of
lines in the file? Is there a function that can do this, or do I need to
code this myself?

You'll have to code this yourself. However, if you don't mind traversing
the file twice, things are considerably simpler. Let's for the moment
assume that you have a fixed maximum length for your lines (this artificial
restriction can be removed as well). So, here's what you can do:

#include <stdio.h>
#include <assert.h>

int count_lines(FILE *fp, int *nlptr, int *mlptr) {
int nlines = 0, maxlen = 0;
int ch, len = 0, rval;
fpos_t pos;

rval = fgetpos(fp, &pos);
if ( rval )
return 1; /* didn't read fp at all */

maxlen = 0;
while ( (ch = fgetc(fp)) != EOF ) {
if ( ch == '\n' ) {
++nlines;
maxlen = (len > maxlen) ? len : maxlen;
len = 0;
}
else ++len;
}

if ( ferror(fp) )
return 2; /* read something */

if ( len ) {
++nlines;
maxlen = (len > maxlen) ? len : maxlen;
}

*nlptr = nlines;
*mlptr = maxlen;

rval = fsetpos(fp, &pos);
if ( rval )
return 3; /* read everything, but failed to return back */
return 0;
}

That should help you get both the line count and the length of the longest
line (assuming you didn't encounter any errors meantime), and returns the
file position indicator for the stream back to where it was before the call
was made. You can use the maximum length for allocation purposes (wasteful
maybe, but depends on your input space really) or to see if the file
doesn't meet your expectations for the maximum length.

-nrk.
Vas.


--
Remove devnull for email
Nov 14 '05 #12
In article <c1**********@sunnews.cern.ch>, Dan Pop <Da*****@cern.ch> wrote:
In <c1**********@pc18.math.umbc.edu> ro****@pc18.math.umbc.edu (Rouben Rostamian) writes:
In article <87************@jesus.intercult.su.se>,
Stefano Ghirlanda <st***************@unibo.it> wrote:
"Vasilis Serghi" <vs*****@jaguar.com> writes:

So I have a csv file that I can read in, how can I work out the
number of lines in the file? Is there a function that can do this,
or do I need to code this myself?

Just count the number of \n characters in the file (or whatever
character combination the system uses for "end of line").


But beware that the number lines may be 1 more than the number
of \n characters if the file does not end in \n.


Does that extra bunch of characters after the last newline qualify as a
line?


That depends on the file's creator's concept of a "line".
If the file was derived from a spreadsheet, that last
pseudo-line may contain the balance of your bank account.
You don't want to be too cavalier in discarding it.

--
Rouben Rostamian
Nov 14 '05 #13
Rouben Rostamian wrote:
Dan Pop <Da*****@cern.ch> wrote:
(Rouben Rostamian) writes:
In article <87************@jesus.intercult.su.se>,
Stefano Ghirlanda <st***************@unibo.it> wrote:
"Vasilis Serghi" <vs*****@jaguar.com> writes:

> So I have a csv file that I can read in, how can I work out the
> number of lines in the file? Is there a function that can do
> this, or do I need to code this myself?

Just count the number of \n characters in the file (or whatever
character combination the system uses for "end of line").

But beware that the number lines may be 1 more than the number
of \n characters if the file does not end in \n.


Does that extra bunch of characters after the last newline
qualify as a line?


That depends on the file's creator's concept of a "line".
If the file was derived from a spreadsheet, that last
pseudo-line may contain the balance of your bank account.
You don't want to be too cavalier in discarding it.


The simple answer is to use an input mechanism that defines the
action (as far as is feasible) on that possibly orphan last line:

#include <stdio.h>
#include <stdlib.h>
#include "ggets.h"

#define useup free

int main(void)
{
char *ln;
size_t lines;

lines = 0;
while (0 == ggets(&ln)) {
lines++;
useup(ln);
}
printf("%ul lines\n", (unsigned long)lines);
return 0;
} /* untested */

and you can get ggets.c and ggets.h (in standard C) at:

<http://cbfalconer.home.att.net/download/ggets.zip>

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #14
In <c1**********@pc18.math.umbc.edu> ro****@pc18.math.umbc.edu (Rouben Rostamian) writes:
In article <c1**********@sunnews.cern.ch>, Dan Pop <Da*****@cern.ch> wrote:
In <c1**********@pc18.math.umbc.edu> ro****@pc18.math.umbc.edu (Rouben Rostamian) writes:
In article <87************@jesus.intercult.su.se>,
Stefano Ghirlanda <st***************@unibo.it> wrote:
"Vasilis Serghi" <vs*****@jaguar.com> writes:

> So I have a csv file that I can read in, how can I work out the
> number of lines in the file? Is there a function that can do this,
> or do I need to code this myself?

Just count the number of \n characters in the file (or whatever
character combination the system uses for "end of line").

But beware that the number lines may be 1 more than the number
of \n characters if the file does not end in \n.


Does that extra bunch of characters after the last newline qualify as a
line?


That depends on the file's creator's concept of a "line".
If the file was derived from a spreadsheet, that last
pseudo-line may contain the balance of your bank account.
You don't want to be too cavalier in discarding it.


The implementation may discard it for you:

Whether the last line
requires a terminating new-line character is implementation-defined.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #15
In article <c1**********@sunnews.cern.ch>, Dan Pop <Da*****@cern.ch> wrote:
In <c1**********@pc18.math.umbc.edu> ro****@pc18.math.umbc.edu (Rouben Rostamian) writes:
In article <c1**********@sunnews.cern.ch>, Dan Pop <Da*****@cern.ch> wrote:
In <c1**********@pc18.math.umbc.edu> ro****@pc18.math.umbc.edu (Rouben Rostamian) writes:

But beware that the number lines may be 1 more than the number
of \n characters if the file does not end in \n.

Does that extra bunch of characters after the last newline qualify as a
line?


That depends on the file's creator's concept of a "line".
If the file was derived from a spreadsheet, that last
pseudo-line may contain the balance of your bank account.
You don't want to be too cavalier in discarding it.


The implementation may discard it for you:

Whether the last line
requires a terminating new-line character is implementation-defined.


You are right, the implementation may discard it for you.

I would consider an implementation that discards incoming data,
one of poor quality, if not dangerous to use.

--
Rouben Rostamian
Nov 14 '05 #16
Derk Gwen wrote:
"Vasilis Serghi" <vs*****@jaguar.com> wrote:
# Presently I define the number of lines to be expected in a file when
# defining the array size and the initialisation of this array. This works
# fine for now, but i'm sure that in the future this could change. So rather
# than explicitly define the number expected, the file is read and the number
# defined that way.
#
# So I have a csv file that I can read in, how can I work out the number of
# lines in the file? Is there a function that can do this, or do I need to
# code this myself?

Reallocate the array of lines as need.

char *a,**line = 0; int m = 0, n= 0;

a = next line of input;
if (n+1>=m) {m = 2*(n+1); line = realloc(line,sizeof(char*)*m);}
line[n++] = a;


Bad.

If realloc returns NULL, it means it couldn't allocate enough space for
the new copy of the buffer... but it does not mean it's freed the
original; that's still sitting there, pointed to by line. Except you
just lost that pointer.

Net result: a block of allocated memory you can't access, can't free,
can't do anything with. I'll assume that the lack of a check for NULL
was simply "condensing for posting", rather than simply bad habits. ;)
Nov 14 '05 #17

"Vasilis Serghi" <vs*****@jaguar.com> wrote in message
news:c1*********@eccws12.dearborn.ford.com...
Presently I define the number of lines to be expected in a file when
defining the array size and the initialisation of this array. This works
fine for now, but i'm sure that in the future this could change. So rather
than explicitly define the number expected, the file is read and the number defined that way.

So I have a csv file that I can read in, how can I work out the number of
lines in the file? Is there a function that can do this, or do I need to
code this myself?

Vas.


Wow, thanks for the responses. This is what I did in the end. Don't flame me
if this is completely wrong but it seems to work ok.

while (!feof(errorFile))
{
/* Read each line of text and increment the counter. Provided the
last line is not a line feed */
if (fgets(lineText,100,errorFile) && (lineText[0] != '\r') &&
(lineText[1] != '\n'))
{
numOfRows++;
}
}

I noticed that after the last line, I saw a "\r\n", hence the check for
these characters. Stefano gave me the original idea. The csv file is
generated by me in Excel. What this won't work with is multiple \r\n after
the last line, but that is something I can deal with before the file is
read.
Nov 14 '05 #18
Vasilis Serghi wrote:

"Vasilis Serghi" <vs*****@jaguar.com> wrote in message
news:c1*********@eccws12.dearborn.ford.com...
Presently I define the number of lines to be expected in a file when
defining the array size and the initialisation of this array. This works
fine for now, but i'm sure that in the future this could change. So rather
than explicitly define the number expected, the file is read and the

number
defined that way.

So I have a csv file that I can read in, how can I work out the number of
lines in the file? Is there a function that can do this, or do I need to
code this myself?

Vas.


Wow, thanks for the responses. This is what I did in the end. Don't flame me
if this is completely wrong but it seems to work ok.

while (!feof(errorFile))
{
/* Read each line of text and increment the counter. Provided the
last line is not a line feed */
if (fgets(lineText,100,errorFile) && (lineText[0] != '\r') &&
(lineText[1] != '\n'))
{
numOfRows++;
}
}

I noticed that after the last line, I saw a "\r\n", hence the check for
these characters. Stefano gave me the original idea. The csv file is
generated by me in Excel. What this won't work with is multiple \r\n after
the last line, but that is something I can deal with before the file is
read.


You're doing it wrong. You want to read lines from a text file. You must
open the text file in text mode ("r"). In text mode you can't see a '\r'
character. The sequence '\r\n' that Windows makes is reduced to '\n' by
your C implementation in text mode. I've repeated 'text mode' here
several times to aid your eventual recollections. Also fgets() works on
text streams.

A line is a stream of zero or more characters ending with and including
'\n'. Whether the last line of a stream requires '\n' is 'implementation
defined'.

It is always(?) wrong to test feof() before doing something which may
cause it. Your loop above is more correctly written like..

while (fgets(lineText, 100, errorFile)) ++ numOfRows;

That loop will run and count lines as long as there are any. It will
also count the last line that doesn't have a '\n'. If you still want to
know why fgets() stopped, now is the time for feof(), ferror() or
whatever.
--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #19

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

Similar topics

2
by: Andrew Rich | last post by:
Howdy, I have a need to do this :- 1. find a match 2. go back three lines 3. read out lines 1 2 3 eg
36
by: Wei Su | last post by:
Hi, I have a text file abc.txt and it looks like: 12 34 56 23 45 56 33 56 78 ... .. .. ... .. .. I want to get how many rows totally in the text file, how to do this? Thanks.
11
by: Ken Varn | last post by:
I want to be able to determine my current line, file, and function in my C# application. I know that C++ has the __LINE__, __FUNCTION__, and __FILE___ macros for getting this, but I cannot find a...
9
by: Joe Rattz | last post by:
I can't seem to get to trace.axd. I have turned tracing on in web.config. Here is how I currently have i configured: <trace enabled="true" requestLimit="10" pageOutput="false"...
14
by: NormD | last post by:
We have a client-server app using Web Services on an IIS machine. The trace below shows that .NET is searching around for some things (e.g., SystemDrawing.DLL and System.Drawing.EXE) and taking a...
7
by: TheGanjaMan | last post by:
Hi people, I'm stuck on a problem and I was wondering if there was a way around: I'm trying to find the number of lines in a comma delimited text file. I have a progress bar that should...
6
by: ivan.perak | last post by:
Hello, im a beginner in VB.NET... The thing i would like to do is as it follows.... I have a text file (list of names, every name to the next line) which is about 350000 lines long. I would...
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?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
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...
0
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,...
0
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...

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.