fgetc putchar

This is a discussion on fgetc putchar within the C Programming forums, part of the General Programming Boards category; What would be the best way to implement a program in conjunction with this code, to print a line number ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    70

    fgetc putchar

    What would be the best way to implement a program in conjunction with this code, to print a line number at the beginning of each line.

    Code:
    int c;
    while((c = fgetc(fp)) != EOF){
      printf("%c", c);		  
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Use a counter, prior to the loop:
    Code:
    int ln = 1;
    printf("%d: ",ln);
    At the end of the loop, increment ln and print the line number after each '\n':
    Code:
    if (c == '\n') printf("%d: ",++ln);
    You may end up with an extra blank numbered line, but there's a general strategy.
    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

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    70
    That would just print a line number at the END of each line.
    Last edited by erasm; 08-08-2009 at 01:59 PM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,826
    Quote Originally Posted by erasm
    That would just print a line number at the END of each line.
    No, it would print a line number at the beginning of the next line. A modification of that idea that will allow you to print a line number at the beginning of each line would be to use a flag variable that is set to 1 before the loop. At the start of the loop, if the flag variable is non-zero, print the line number, increment the line number, and then set the flag variable to 0. Towards the end of the loop, if the character read is a newline, set the flag variable to 1.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by erasm View Post
    That would just print a line number at the END of each line.
    By "end of the loop" I meant after printf("%c", c). So a newline has just been printed, meaning you are at the beginning of a line and the if condition will be met, printing a fresh line number. Hence, if the last character from fp is a newline, you will get that extra line number at the end.

    It that is undesirable, Laserlight's method with a flag would be better.
    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

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    70
    Is this what you mean?

    Code:
    while((c = fgetc(fp)) != EOF){
      if(c == '\n'){
        printf("%d: ", ++lineCount);
      }
    printf("%c", c);
    }
    Disregard this
    Last edited by erasm; 08-08-2009 at 02:28 PM. Reason: Just saw previous post

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by erasm View Post
    Disregard this

    I never disregard a happy face!
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  2. fgets not working after fgetc
    By 1978Corvette in forum C Programming
    Replies: 3
    Last Post: 01-22-2006, 05:33 PM
  3. array help
    By 1rwhites in forum C Programming
    Replies: 17
    Last Post: 11-09-2005, 03:10 PM
  4. crazy triangles
    By markg in forum C Programming
    Replies: 3
    Last Post: 10-24-2005, 12:50 PM
  5. Text based frame in win32 console
    By GaPe in forum C Programming
    Replies: 8
    Last Post: 04-16-2002, 07:01 AM

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