Thread: fputc(int, FILE *) problem

  1. #1
    Young C n00b
    Join Date
    Jul 2006
    Posts
    59

    fputc(int, FILE *) problem

    I'm trying to write a program that inserts a number line into a text file.
    For some reason, fputc(int, FILE *) doesn't print out the int to stdout.

    Code:
    #include <stdio.h>
    
    int main(int argc, char **argv)
    {
            FILE *ifp = fopen(argv[1], "r");
            char c;
    
            int lineNumber = 0;
            while((c = getc(ifp)) != EOF)
            {
                    fputc(c, stdout); //print char to screen
                    if(c == '\n')
                    {
                            fputc(lineNumber, stdout);
                            lineNumber++;
                    }
            }
            printf("\nputc() ran %i times.\n\n", lineNumber);
    
    
    
            fclose(ifp);
    
            return 0;
    }
    input file
    Code:
    a
    b
    c
    d
    output
    Code:
    a
    b
    c
    d
    
    putc() ran 4 times.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    stdout is line-buffered. meaning characters arent guaranteed to be printed even after a print statement. to ensure things are printed you need to force it to print, for example printing a '\n' newline character after your fputc statements.
    Last edited by nadroj; 12-14-2007 at 08:22 PM.

  3. #3
    Young C n00b
    Join Date
    Jul 2006
    Posts
    59
    I'm not sure what you mean. I updated my code to:

    Code:
            int lineNumber = 0;
            while((c = getc(ifp)) != EOF)
            {
                    fputc(c, stdout); //print char to screen
                    fputc('\n', stdout);
                    if(c == '\n')
                    {
                            fputc(lineNumber, stdout);
                            fputc('\n', stdout);
                            lineNumber++;
                    }
            }
            printf("\nputc() ran %i times.\n\n", lineNumber);
    New Output:
    Code:
    [fl76a06@cisweb ex04]$ ./ex04 test
    a
    
    
    
    b
    
    
    
    c
    
    
    
    d
    
    
    
    
    putc() ran 4 times.
    It prints the newlines just fine, but no ints. I suspected this was some sort of buffer problem. Am I putting the calls to putc('\n', stdout) in the correct places?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
    fputc(lineNumber, stdout)
    And you expect ASCII character 1 to be printable, why? (Although if you had went to g, you should have gotten a beep for your troubles.) If you want to print a number, print a number.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    when i saw your output it didnt look like anything else was being printed which is why i suggested about the buffering or printing a newline character.

    when i tried your first code snippet it was printing something (garbled ASCII). so it doesnt look like its any buffer-mode problem.

    notice that your trying to call fputc on an integer, lineNumber. fputc is to print a single character, that is, 1byte. an integer is not 1 byte--its usually 4. so it was printing your 'integer', but only the 1st byte of it, so its not really recognizable.

    you can either leave your lineNumber as an int and initialize it to '0' (as opposed to just 0). when you print it in the last printf statement youll have to print it as a &#37;c, not as an integer.

    setting the int to '0' will set it to the ASCII value of the zero character. you can leave the ++ statement there because it will simply increase it to the next ASCII character, '1'. however, if you loop more than 10 times, your integer wont represent a number, itll represent the next ASCII value after the character '9'. an easier solution is to simply use printf or fputs/fprint.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You're trying to print 0x00 to 0x03 which aren't printable ASCII characters.
    If the value of the int isn't a printable ASCII character you'll get all kinds of crap on the screen. Maybe you want to convert the int to a char first?
    or you could just use:
    Code:
    printf( "%d", lineNumber );

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by nadroj View Post
    notice that your trying to call fputc on an integer, lineNumber. fputc is to print a single character, that is, 1byte. an integer is not 1 byte--its usually 4. so it was printing your 'integer', but only the 1st byte of it, so its not really recognizable.

    you can either leave your lineNumber as an int and initialize it to '0' (as opposed to just 0). when you print it in the last printf statement youll have to print it as a %c, not as an integer.

    setting the int to '0' will set it to the ASCII value of the zero character. you can leave the ++ statement there because it will simply increase it to the next ASCII character, '1'. however, if you loop more than 10 times, your integer wont represent a number, itll represent the next ASCII value after the character '9'. an easier solution is to simply use printf or fputs/fprint.
    Of course it's recognizable, just not the way the OP intended. putc() takes an int parameter, but only uses the lower 8 bits, so if the int was over 255 it wouldn't see the whole thing.

    Initializing the int to '0' won't help unless the file doesn't have more than 10 lines.

    As for the printf(), you should NOT use %c since that'll just have the same problem as the putc().

  8. #8
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Of course it's recognizable, just not the way the OP intended.
    well no, its not recognizable. because printing the LSB of the integer seems random--ie not recognizable.

    Initializing the int to '0' won't help unless the file doesn't have more than 10 lines.
    thats what i mentioned above--it will work if its in the range 0-9 inclusive.

    As for the printf(), you should NOT use &#37;c since that'll just have the same problem as the putc().
    using %c will work, when simply printing to the screen as the poster was doing. it will print the value of lineNumber, which is an integer representation of the single character 0-9.

  9. #9
    Young C n00b
    Join Date
    Jul 2006
    Posts
    59
    Thanks guys.

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Yes, but what I'm saying is, why limit your program to only 10 lines when you could easily use printf( "%d", lineNumber ); and won't have a problem unless your file has over 4294967295 lines?

  11. #11
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    yes im aware. i agree that using what the OP propsed above is not the best way, but i only focused on the problem given, as times there are requirements to meet, or just someone wants to try something different. above i also suggested to use a different print method that allows to print the actual integer.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by cpjust View Post
    Yes, but what I'm saying is, why limit your program to only 10 lines when you could easily use printf( "%d", lineNumber ); and won't have a problem unless your file has over 4294967295 lines?
    Actually, 2147483647 lines -- assuming a signed int.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read from file problem
    By Martin Kovac in forum C Programming
    Replies: 1
    Last Post: 04-13-2009, 08:33 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. File I/O problem
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 12
    Last Post: 09-03-2005, 12:14 PM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. Rename file problem
    By Emporio in forum C Programming
    Replies: 2
    Last Post: 06-05-2002, 09:36 AM