473,597 Members | 2,342 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1961
"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 | Interdisciplina ry 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)cybers pace.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.intercul t.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 | Interdisciplina ry cultural research, Stockholm University
http://www.intercult.su.se/~stefano
Nov 14 '05 #7
In article <87************ @jesus.intercul t.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**********@p c18.math.umbc.e du> ro****@pc18.mat h.umbc.edu (Rouben Rostamian) writes:
In article <87************ @jesus.intercul t.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

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

Similar topics

2
6773
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
3572
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
21913
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 C# equivalent. Any ideas? -- ----------------------------------- Ken Varn Senior Software Engineer Diebold Inc. varnk@diebold.com
9
4400
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" traceMode="SortByTime" localOnly="false" />
14
2916
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 LONG time to locate them. Time after time. The application DOES run, so it is ultimately finding what it needs. How can we tell "it" where these are located (should we even have to?) so it can look directly there instead of searching? ...
7
11021
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 display the progress of a text file that is being read into a DataGridView. I would like the progressbar to progress as the lines of data are being read. But to set the maximum value for the progress bar I would need to know the
6
24142
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 like to split it and create a new file at every lets say 20000 lines... so, the directory output would have to be something like this:
1
8035
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
8258
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6688
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
5847
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
5431
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
3927
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2404
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
1
1494
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1238
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.