Thread: adding line numbers to C file but skipping the empty lines, second try

  1. #1
    Registered User
    Join Date
    Mar 2017
    Posts
    2

    adding line numbers to C file but skipping the empty lines, second try

    Hi guys,

    something went wrong the first time.. so second try

    the following program i made reads a given file and writes the content of that given file to a new file. But it adds line numbers for each line it crosses. But the program needs to exclude the lines that have no text. the program now gives every line a number. Could you guys help me find the missing part so i can give a new file the line numbers only for the the lines with text?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
       FILE *fp;
        FILE *fp2;
        char ch;
         char fnamer[100];
        char fnamer2[100];    //Storing File Path/Name of Image to Display
        printf("\n\nPlease Enter the Full Path of the Image file you want to view: \n");
        scanf("%s",&fnamer);
        fp=fopen(fnamer,"r");
                if(fp==NULL)
            {
                    printf("Error!");
                    exit(1);
            }
              printf("\n\nPlease Enter the Full Path of the Image file you want to write to: \n");
        scanf("%s",&fnamer2);
        fp2=fopen(fnamer2,"w");
        if(fp2==NULL)
        {
            perror(fnamer2);
            exit(1);
        }
        else{
            printf("test\n");
        }
    
    
    
    int line_number = 0;
        fprintf(fp2, "%d: ", ++line_number);  /* put line number in output file */
        printf("%d: ", line_number);
    
    
        while((ch = fgetc(fp)) != EOF )
        {
            //printf("test2");
            fputc(ch,fp2);
            printf("%c", ch);
                if (ch == '\n'){
                         fprintf(fp2, "%d: ", ++line_number);  /* put line number in output file */
                         printf("%d: ", line_number);
                         }
    
    }
    
    fclose;
    fclose(fp);
    // fclose(fp2);
       // getch();
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Nothing went wrong the first time, your post was put into the moderation queue.

    The easy way would be to use fgets() to read a whole line, then you decide whether it's a blank line or not, and only then do you decide whether to print a newline or not.

    But at the moment, you need to have a variable which counts the number of characters printed on the line so far, so you can do things like
    Code:
    if ( ch != '\n' && charsOnLine == 0 ) {
      // print a line number
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2017
    Posts
    2
    thanks, I have tryed a couple of things with the following code

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
       FILE *fp;
        FILE *fp2;
        char ch;
         char fnamer[100];
        char fnamer2[100];    //Storing File Path/Name of Image to Display
        printf("\n\nPlease Enter the Full Path of the Image file you want to view: \n");
        scanf("%s",&fnamer);
        fp=fopen(fnamer,"r");
                if(fp==NULL)
            {
                    printf("Error!");
                    exit(1);
            }
              printf("\n\nPlease Enter the Full Path of the Image file you want to write to: \n");
        scanf("%s",&fnamer2);
        fp2=fopen(fnamer2,"w");
        if(fp2==NULL)
        {
             printf("Error!");
            exit(1);
        }
        else{
           // printf("test\n");
        }
    
    
    
    int line_number = 0;
    int charsOnLine = 0;
    
        fprintf(fp2, "%d: ", ++line_number);  /* put line number in output file */
        printf("%d: ", line_number);
    
    
        while((ch = fgetc(fp)) != EOF )
        {
            //printf("test2");
            fputc(ch,fp2);
            printf("%c", ch);
               if ( ch != '\n' && charsOnLine ==0 ) {
                    fprintf(fp2, "%d:", ++line_number );  /* put line number in output file */
                     printf("%d: ", line_number);
                         }
        //        if (ch != '\n' && charsOnLine 0 ){
          //          fprintf(fp2, "%c", ch);
            //        printf("%d", ch);
              //  }
    }
    
    fclose;
    fclose(fp);
    // fclose(fp2);
       // getch();
    }
    it almost works. but it gives now to every char a number.

    Ive tryed to add the code that is now hidden with //. but that did not work. What am i doing wrong?


    again thank you for your help!

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well at some point, you need to reset charsOnLine back to zero.

    And you also need to increment charsOnLine whenever you print something.

    Also, you shouldn't have lines 36,7 since you don't know yet if the file begins with a blank line.

    > scanf("%s",&fnamer);
    You don't need the & when using %s with a char array (and especially when using a char pointer).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    An alternative concept is to save the previous character. If both the current and previous characters are '\n' then the current line is blank. You would need to initialize prev_ch to '\n'.

    Of course, it depends on what you mean by blank. I'm assuming you mean that it consists only of a newline character as opposed to consisting of only whitespace E.g., what about a line that consists of five spaces? Is it "blank"?. If you want to deal with that type of blank line then using fgets with a big buffer and reading the in the entire line at a time is probably best.

    Also, you haven't made it clear whether you want to print out the blank lines (but without line numbers) or skip them completely.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 11-27-2013, 12:24 PM
  2. reading a file with getline and skipping lines..
    By kocmohabt33 in forum C++ Programming
    Replies: 2
    Last Post: 01-29-2011, 12:37 AM
  3. Adding line numbers to file
    By getout in forum C Programming
    Replies: 6
    Last Post: 01-04-2007, 12:31 PM
  4. skipping lines with an input text file
    By kwikness in forum C++ Programming
    Replies: 7
    Last Post: 12-12-2006, 09:11 AM

Tags for this Thread