Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to get the total row number of a text file

Reply
Thread Tools

How to get the total row number of a text file

 
 
Wei Su
Guest
Posts: n/a
 
      08-11-2005
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.

 
Reply With Quote
 
 
 
 
castreuil.anthony@student.ha.be
Guest
Posts: n/a
 
      08-11-2005

Wei Su schreef:

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


You could count the numbers of linebreaks.

 
Reply With Quote
 
 
 
 
ThunderBird
Guest
Posts: n/a
 
      08-11-2005
please show some details. thanks

 
Reply With Quote
 
Robert Gamble
Guest
Posts: n/a
 
      08-11-2005
ThunderBird wrote:
> please show some details. thanks


Please show some context so people have a clue as to what you are
balking about.

Robert Gamble.

 
Reply With Quote
 
ThunderBird
Guest
Posts: n/a
 
      08-11-2005
it mentioned "You could count the numbers of linebreaks. ". My question
is how to count the numbers of linebreaks?
TB

 
Reply With Quote
 
Charles M. Reinke
Guest
Posts: n/a
 
      08-11-2005
----- Original Message -----
From: "ThunderBird" <>
Newsgroups: comp.lang.c
Sent: Thursday, August 11, 2005 1:14 PM
Subject: Re: How to get the total row number of a text file


> it mentioned "You could count the numbers of linebreaks. ". My question
> is how to count the numbers of linebreaks?
> TB
>


This works for me...

cmreinke@hologram>cat lines.c
/* lines.c */
#include <stdio.h>

int main(int argc, char **argv) {
long lines=0;
FILE *fp;

if(argc>1) {
if((fp=fopen(argv[1], "r"))) {
while(fscanf(fp, "%*[^\n]\n")!=EOF) lines++;
printf("Number of lines in file \"%s\": %ld\n", argv[1], lines);
} /* if */
else printf("ERROR: could not open file \"%s\"\n", argv[1]);
} /* if */
else printf("ERROR: no input file specified\n");

return 0;
} /* main */
/* lines.c */

cmreinke@hologram>gcc -Wall -ansi -pedantic -o lines.exe lines.c
cmreinke@hologram>lines.exe output2
Number of lines in file "output2": 270

-Charles


 
Reply With Quote
 
moxm
Guest
Posts: n/a
 
      08-11-2005
This is a kind of tricky question, different os use different
combinations to get the newline.
Windows: \r\n
Unix: \n
Mac: \r . it said to be, I have never used a Mac machine. However, the
text file transfered between M$'s os and Unix, I dit get some trouble
with the carriage return and line feed.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

static const char *prog_name="wcn";

int
main(int argc, char *argv[]) {
FILE *fp;
int i;

if (argc == 1) {
printf("Usage: %s [files]\n", prog_name);
exit(EXIT_FAILURE);
}

for (i=1; i<argc; ++i) {
unsigned int r_c=0, n_c=0, rn_c=0;
int c, last=0;
if (! (fp=fopen(argv[i], "rb"))) {
printf("%s: %s: %s\n", prog_name, argv[i], strerror(errno));
continue;
}


while ((c=fgetc(fp)) != EOF) {
if (c == '\n') {
++n_c;
if (last == '\r')
++rn_c;
} else if (last == '\r')
++r_c;
last = c;
}
r_c += last=='\r'; /* In case the last char is '\r' */

if (ferror(fp)) {
printf("%s: %s: %s\n", prog_name, argv[i], strerror(errno));
if (fclose(fp) == EOF)
perror(prog_name);
continue;
}
if (fclose(fp) == EOF)
printf("%s: %s: %s\n", prog_name, argv[i], strerror(errno));
printf("%s:\t \\n: %u\t \\r: %u\t \\r\\n: %u\n", argv[i], n_c, r_c,
rn_c);
}
exit(EXIT_SUCCESS);
}

 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      08-11-2005
In article < .com>,
moxm <> wrote:
>This is a kind of tricky question, different os use different
>combinations to get the newline.
>Windows: \r\n
>Unix: \n
>Mac: \r . it said to be, I have never used a Mac machine. However, the
>text file transfered between M$'s os and Unix, I dit get some trouble
>with the carriage return and line feed.


> if (! (fp=fopen(argv[i], "rb"))) {


If you do not open in binary mode, then the local OS's internal convention
will be transformed into \n when the data is read.

If you are trying to deal with files that have been copied from other
OS's then the first line of defence is to copy them in text mode instead
of binary mode.
--
"No one has the right to destroy another person's belief by
demanding empirical evidence." -- Ann Landers
 
Reply With Quote
 
moxm
Guest
Posts: n/a
 
      08-11-2005
Charles' code is very clean and elegant, I think it works well in
windows and unix , but what if the newline character is just '\r' at a
Mac machine.

 
Reply With Quote
 
moxm
Guest
Posts: n/a
 
      08-11-2005
Sorry! I am idiot, just open the file in text mode is okay like Walter
Roberson said.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
BT Total Broadband vs BT Business Total John Y UK VOIP 2 02-02-2007 09:27 AM
How do I get the row number off of a Excel Row object? wlaver@gmail.com Ruby 1 08-22-2006 08:48 PM
How to get the total number of lines of a text file Mullin Java 4 06-12-2005 09:15 PM
ok I can do a totals row but how about a percentage row after each data row D ASP .Net Datagrid Control 0 05-23-2005 04:10 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57