Like Tree2Likes
  • 2 Post By quzah

Printing contents of a text file

This is a discussion on Printing contents of a text file within the C Programming forums, part of the General Programming Boards category; I'm simply trying to print out the contents of a text file to make sure I am in fact opening ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    27

    Printing contents of a text file

    I'm simply trying to print out the contents of a text file to make sure I am in fact opening and reading a text file correctly. I also want to store all the characters in an array. Here's my code so far:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <fcntl.h>
    #include <sys/stat.h>
    
    int main(int argc, char** argv) {
    
        FILE * textfile;
        int max_chars = 1000;
        char contents[max_chars];
    
        textfile = fopen("file.txt", "r");
    
        fgets(contents,max_chars,textfile);
        fputs("%s",contents);
    
        return (EXIT_SUCCESS);
    }
    I tried fgets() to store the characters of the text file in the contents[] array.

    Then I tried fputs() to print out the text file.

    Am I using the right approach using fgets() and fputs() or should I be doing something else?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    fputs() should probably be just puts(contents); you aren't sending it to a file...
    fread() is a better tool if you're loading the whole file...

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,639
    fgets is great for reading a line of text from a file. As Tater pointed out, your usage of fputs is wrong. Check your docs or Google for correct usage. You could simply use puts, but puts adds a new line, so you may end up with the new line you read from the file plus an extra from puts, making the file come out double spaced (assuming you eventually read multiple lines from the file). Your usage of "%s" also makes me think you're mixing it up with printf/fprintf. That's for doing all kinds of special formatting. Again, check your docs or Google for details. Last two things, you need to check textfile for NULL before you try to read from it, in case the fopen failed, and fclose the file when you're done.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    27
    Thanks, puts() and fread() is what I was looking for. And yeah, I'll have to close the file when I'm done. Thanks for the help.

    EDIT: Oops, I meant puts().
    Last edited by never_lose; 04-27-2011 at 06:38 PM. Reason: fputs() should be puts().

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    No... you want puts() and fread()...
    As already pointed out fputs writes to a file, not the screen.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,639
    fputs will happily write to the screen if you use stdout as the file descriptor, and it will avoid the potential double newline issue.

    EDIT: Print to stdout, not stdin!
    Last edited by anduril462; 04-28-2011 at 12:01 AM. Reason: fixed wording

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by anduril462 View Post
    fputs will happily write to the screen if you use stdin as the file descriptor, and it will avoid the potential double newline issue.
    Ok... never tried that (I spend most of my actual programming time working in Windows GUI/API mode).

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by anduril462 View Post
    fputs will happily write to the screen if you use stdin as the file descriptor, and it will avoid the potential double newline issue.
    Are you sure about that?


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,639
    Yeah, stdout...good catch.

  10. #10
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    I always use printf(). Probably never needed puts in 40 years of programming.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nonoob View Post
    I always use printf(). Probably never needed puts in 40 years of programming.
    None the less, it IS there when you're ready...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. printing output from prog
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 09:50 PM
  2. How do I make a text box??
    By BubbleMan in forum Windows Programming
    Replies: 12
    Last Post: 08-24-2001, 03:31 AM
  3. dos printing
    By cozman in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-11-2001, 08:55 AM
  4. printing stream in C++?
    By cozman in forum C++ Programming
    Replies: 10
    Last Post: 08-10-2001, 10:26 PM

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