how do I count the lines in a text file?

This is a discussion on how do I count the lines in a text file? within the C Programming forums, part of the General Programming Boards category; Hi folks, how would I count the lines in a file? I want to get a new unique ID# for ...

  1. #1
    Registered User
    Join Date
    Feb 2003

    how do I count the lines in a text file?

    Hi folks,
    how would I count the lines in a file?
    I want to get a new unique ID# for a simple DB by counting the records and assigning this number + 1 for each new record.
    The lines in the .txt file are as follows


    I have tried this but it doesn't work

    FILE *r ;
    r = fopen(datafile,"a"); // I'll error check later if
    //I get this bit to work first

    if(inchar == '\n')
    linecount++ ;

    linecount remains at 0 (the swine)

    The function which printed the file is


    so there is a \n in there to be counted.
    I have tried numerous other ways from the C board archives
    but nothing works!
    what am I doing wrong?
    any help would be greatly appreciated.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    A couple of things:
    - use code tags
    - try opening with "a+"
    - read more about fopen() here.


  3. #3
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    You can try something like that:
    #include <stdio.h>
    #include <stdlib.h>
    #define PATH "C:\\test.txt"
    int main(void) {
    	FILE *fpIn;
    	int lines = 0;
    	char buf[BUFSIZ];
    	if ((fpIn = fopen(PATH,"r")) == NULL) {
    		perror ("Could not open file: ");
    		return EXIT_FAILURE;
    	while (fgets(buf,sizeof(buf),fpIn) != NULL)
    	printf("This file contains %d lines\n",lines);
    	return 0;

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    while (fgets(buf,sizeof(buf),fpIn) != NULL)
    This code could catch you out, as fgets() isn't guaranteed to get you a line of data. You'd need to check the last character of the buffer to be sure. But if you do this, be mindful of what happens at the end of a file that doesn't end with a newline character.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Feb 2003
    well I never, a+ did the trick!
    I spent hours trying get it to work and I knew I must be close.
    I settled on the following bit of code since it's nice and simple and seem's to do the trick ie prints out the HTML table with a unique ID.
    sorry I forgot code tags , didn't think that my ity bity snitch of code deserved code tags!

    file mode rt also works!

    thanks for all the help. Here's the piece of code I'm using.
    (with code tags...I hope) It takes input from a form and enters it into a HTML table. It's very simple but I can work it up to what I really need with time and effort.

    void process2(char *s)
    int ID=0;
    int header_print =0;
    char full_entry1[1000];
    char name_first[300];
    char name_last[300];
    char test1_score[300];
    char test2_score[300];
    char test3_score[300];
    char test4_score[300];
    int linecount =0 ;
    int inchar;
    FILE *r ;
    r = fopen(datafile,"rt");
     if(r == NULL)
     printf("%s%c%c\n",  "Content-Type:text/html;charset=iso-8859-1",13,10);
     printf("<P><EM>Unable to open data file</EM>");
     if(inchar == '\n')
     ID = linecount;
       char b[] = "<TABLE> <TABLE BORDER >";
       char d[] = "<TH> ID#";
       char f[] = "<TH> LAST NAME";
       char g[] = "<TH> FIRST NAME";
       char h[] = "<TH> TEST 1 SCORE";
       char i[] = "<TH> TEST 2 SCORE";
       char c[] = "<TH> TEST 3 SCORE";
       char j[] = "<TH> TEST 4 SCORE<TR>";
       fprintf(r,"\n%s%s%s%s%s%s%s%s",b,d,g,f,h,i,c,j);  // prints table headers
    GetStringComponent("name_first", s, name_first);
    GetStringComponent("name_last", s, name_last);
    GetStringComponent("test1_score", s, test1_score);
     //must execute once from here to print headers, ok then
    fprintf(r, "\n<tr><td align=""center"" >%d<td>%s<td>%s<td><td>%s<tr>",ID,name_first,name_last,test1_score  );
                           // doesn't work

    hey, the tags worked.

    Thanks all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  4. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM

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