PDA

View Full Version : How would I go about counting the lines in a text file?



mickpc
09-29-2009, 02:17 AM
I am refreshing my C knowledge, I am wondering how I would count the number of lines in a text file?

I have looked up the ascii code for a CR, this being 0x15, is it as simple as scanning the file for CR's?

This is just a practical exercise to get back into C.

Cheers Mick

mickpc
09-29-2009, 02:24 AM
here is what I have so far...


//name: linecount.c
//description: counts the number of lines in a text document
//

#include <stdio.h>
#include <stdlib.h> //need for file operations?
void main(int argc,char *argv[])
{
int count=0,c;
FILE *ifp,*ofp;


ifp = fopen(argv[1],"r");
while((c=getc(ifp))!=EOF)
if(c==\r)
count++;
printf("the charater count was:%5d\n",count);
}

mickpc
09-29-2009, 02:47 AM
Ok worked the last one out, now I am trying to convert to a function, what is wrong with this?



//name: linecount.c
//description: counts the number of lines in a text document
//

#include <stdio.h>
#include <stdlib.h> //need for file operations?
void linecount(int,char);

void main(int argc,char *argv[])
{
linecount(int argc,char *argv[]);
}

void linecount(int argc,char *argv[])
{
int count=0,c;
FILE *ifp,*ofp;


ifp = fopen(argv[1],"r");
while((c=getc(ifp))!=EOF)
if(c=='\n')
count++;
printf("the number of lines is:%5d\n",count);
}

Cheers Mick

Dae
09-29-2009, 03:38 AM
Why would you call it like this:


linecount(int argc,char *argv[]);

Shouldn't it be this?


linecount(argc, argv);

Shouldnt this:


void linecount(int,char);

Be this?


void linecount(int, char**);

Your compiler should probably be making these errors obvious.

mickpc
09-29-2009, 04:05 AM
//name: linecount.c
//description: counts the number of lines in a text document
//

#include <stdio.h>
#include <stdlib.h> //need for file operations?

void linecount(int, char**);

void main(int argc,char *argv[])
{

linecount(argc, argv);

}
void linecount(int, char**)


{
int count=0,c;
FILE *ifp,*ofp;


ifp = fopen(argv[1],"r");
while((c=getc(ifp))!=EOF)
if(c=='\n')
count++;
printf("the number of lines is:%5d\n",count);
}
This it? Still having trouble!!

Dae
09-29-2009, 04:16 AM
- main return type should be int.
- main should return 0 (not required - thanks laserlight).
- linecount's implementation arguments should have names (just like main).

You could really improve your formatting. You should probably change the linecount interface, instead of taking argc/argv, extract the filename from argc/argv and pass it as a c-string to linecount.

MK27
09-29-2009, 07:33 AM
This it? Still having trouble!!

Why on Earth would you want to pass all of argc and argv to linecount, esp. since you don't use it? If you are going to write a function, make it in some sense "functional" relative to the rest of the program, eg.



#include <stdio.h>

int linecount (char *file) {
int ch, total = 0;
FILE *fp = fopen(file,"r");
if (!fp) return -1;
while ((ch=fgetc(fp)) != EOF) if (ch==10) total++;
fclose(fp);
return total;
}

int main (int argc, char *argv[]) {
int num_of_lines;
if (argc<2) {
puts("Filename required!");
return 0;
}
num_of_lines = linecount(argv[1]);
printf("\n%d lines in \"%s\"\n",num_of_lines,argv[1]);
return 0;
}

RockyMarrone
10-07-2009, 07:21 AM
#include <stdio.h>

#define EOL '\n'
#define CAR_RETURN '\r'

int line_count(char* __str_file_name) {
FILE* fd;

if ((fd = fopen(__str_file_name, "r")) == NULL) {
printf("[Error] : While opening the file\n");
exit;
}

unsigned int line_count = 0;
while ( (ch = fgetc(fd)) != EOF)
if (ch == EOL || ch == CAR_RETURN)
++line_count;

if (fd) {
fclose(fd);
}

return line_count;
}


int main(const unsigned int argc, char* argv[] ) {
int status = 0;

if (argc < 2)
status = -1;
else
printf("Number of lines in file[%s] == [%d]\n", argv[1], line_count(argv[1]));

return status;
}