How would I go about counting the lines in a text file?

This is a discussion on How would I go about counting the lines in a text file? within the Linux Programming forums, part of the Platform Specific Boards category; I am refreshing my C knowledge, I am wondering how I would count the number of lines in a text ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    16

    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

  2. #2
    Registered User
    Join Date
    Sep 2009
    Posts
    16
    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);
    }

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    16
    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

  4. #4
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    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, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    16
    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!!

  6. #6
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    - 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.
    Last edited by Dae; 09-29-2009 at 04:22 PM. Reason: edit: c-1999
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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;
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 04:53 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 02:19 PM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 04:30 PM

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