Thread: Writing Output to a File

  1. #1
    Registered User
    Join Date
    Jan 2008
    Location
    MA
    Posts
    5

    Writing Output to a File

    I am new to C programming. I just started reading through the K&R section on character input and output, but I am left wondering how a person would go about writing text to a file.

    I'm not looking to do anything elaborate with it just yet ... I would like to write output from the code from the exercises that I do to a file so that I can better view the results (the Windows display disappears almost instantly).

    Any suggestions?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Any of these will write output to a file.
    fputc()
    fputs()
    fprintf()


    Eg, these are synonymous
    Code:
    printf( "Hello world" );
    fprintf( stdout, "Hello world" );
    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
    Jan 2008
    Location
    MA
    Posts
    5
    So if I wanted to print "Hello World" to a file called test.txt, I would use those functions to do so?

    If so, how would the "Hello World" know where the file it's being written to is? Would I have to add test.txt as a parameter to the functions you mentioned?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If so, how would the "Hello World" know where the file it's being written to is?
    There is a function named fopen that opens a file (given the filename), returning a file pointer.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You use something called a file pointer (type FILE*) like this:
    Code:
    #include <stdio.h>
    
    #define MAXSTR 1000
    
    int main() {
        char str[ MAXSTR ];
        FILE* fp = fopen( "test.txt", "w" );  /* w = write */
        fprintf( fp, "Hello, world!\n" );
        fclose( fp );
    
        fp = fopen( "test.txt", "r" );  /* r = read */
        fgets( str, MAXSTR, fp );
        puts( str );
        fclose( fp );
    }

  6. #6
    Registered User alreadyinuse's Avatar
    Join Date
    Dec 2007
    Posts
    12
    Quote Originally Posted by portable View Post
    (the Windows display disappears almost instantly).

    Any suggestions?
    you can add system ("PAUSE") (if I remember well) to see what it is displaying
    Code:
    #include <stdlib.h>
    int main()
    {
        //code
    
        system("PAUSE"); 
        return 0;
    }

  7. #7
    Registered User
    Join Date
    Jan 2008
    Location
    MA
    Posts
    5
    I am able to follow the code for the file pointer, which was a big help. Thanks to both ooga and alreadyinuse

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Incidentally, system("PAUSE") may not be portable. For the sake of your username, you may wish to read the FAQ on How do I get my program to wait for a keypress?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jan 2008
    Location
    MA
    Posts
    5
    Thanks laser

    Is there a way to see a list of all of the commonly used functions, such as the ones mentioned in this thread, and what they do? I know a lot of the names are self explanatory, but it would be nice to see them laid out in a study-able format.

    Like I said in the original post, I'm new, and I want to learn as much as possible

  10. #10

  11. #11
    Registered User alreadyinuse's Avatar
    Join Date
    Dec 2007
    Posts
    12
    look at your PM

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM