Writing Output to a File

This is a discussion on Writing Output to a File within the C Programming forums, part of the General Programming Boards category; I am new to C programming. I just started reading through the K&R section on character input and output, but ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    21,734
    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.
    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
    - - - - - - - - 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
    21,734
    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?
    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

  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
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129

  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

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