C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-29-2009, 02:17 AM   #1
Registered User
 
Join Date: Sep 2009
Posts: 7
How would I go about counting the lines in a text file?

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 is offline   Reply With Quote
Old 09-29-2009, 02:24 AM   #2
Registered User
 
Join Date: Sep 2009
Posts: 7
here is what I have so far...

Code:
//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 is offline   Reply With Quote
Old 09-29-2009, 02:47 AM   #3
Registered User
 
Join Date: Sep 2009
Posts: 7
Ok worked the last one out, now I am trying to convert to a function, what is wrong with this?


Code:
//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
mickpc is offline   Reply With Quote
Old 09-29-2009, 03:38 AM   #4
Dae
Deprecated
 
Dae's Avatar
 
Join Date: Oct 2004
Location: Canada
Posts: 944
Why would you call it like this:

Code:
linecount(int argc,char *argv[]);
Shouldn't it be this?

Code:
linecount(argc, argv);
Shouldnt this:

Code:
void linecount(int,char);
Be this?

Code:
void linecount(int, char**);
Your compiler should probably be making these errors obvious.
__________________
Warning: Have doubt in anything I post.

GCC 4.5.0 (lambda branch), Boost 1.40.0, Code::Blocks 8.02, Ubuntu 9.04 010001000110000101100101
Dae is offline   Reply With Quote
Old 09-29-2009, 04:05 AM   #5
Registered User
 
Join Date: Sep 2009
Posts: 7
Code:
//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!!
mickpc is offline   Reply With Quote
Old 09-29-2009, 04:16 AM   #6
Dae
Deprecated
 
Dae's Avatar
 
Join Date: Oct 2004
Location: Canada
Posts: 944
- 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.
__________________
Warning: Have doubt in anything I post.

GCC 4.5.0 (lambda branch), Boost 1.40.0, Code::Blocks 8.02, Ubuntu 9.04 010001000110000101100101

Last edited by Dae; 09-29-2009 at 03:22 PM. Reason: edit: c-1999
Dae is offline   Reply With Quote
Old 09-29-2009, 07:33 AM   #7
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
Quote:
Originally Posted by mickpc View Post
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.

Code:
#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;
}
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is online now   Reply With Quote
Old 10-07-2009, 07:21 AM   #8
Registered User
 
Join Date: Oct 2009
Location: While(1)
Posts: 316
Code:
#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;
}
RockyMarrone is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Memory Address kevinawad C++ Programming 18 10-19-2008 10:27 AM
Formatting a text file... dagorsul C Programming 12 05-02-2008 03:53 AM
Formatting the contents of a text file dagorsul C++ Programming 2 04-29-2008 12:36 PM
Basic text file encoder Abda92 C Programming 15 05-22-2007 01:19 PM
Batch file programming year2038bug Tech Board 10 09-05-2005 03:30 PM


All times are GMT -6. The time now is 07:25 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22