Felipe Boszczowski
Felipe Boszczowski

Reputation: 67

Save to a text file what was wrote at the console. C

I wrote some code to edit text files. While the program is executed, the console write informations to the user knows what happened. My question is: How do I save all information that was wrote by the user and the program at the console to a text file?

Thank you

Upvotes: 0

Views: 85

Answers (2)

aerojun
aerojun

Reputation: 1256

I've not used C++ in years but this is a quick example of using fopen() and fclose() to write and read files. Do a little research of this functions.

#include <stdio.h>

int main (void) {

    char userInput[100];
    int res;

    FILE *userFile; //Pointer to FILE struct

    //Open file
    if ( ( userFile = fopen("userFile.txt", "r+") ) != NULL ) {

        //Read user Input
        gets( userInput );
        //Writes userInput content to File
        fputs( userInput, userFile );
        //Closes file
        fclose(userFile);
    }
    else
        printf ("Can't open file");

    return 0;
}

Upvotes: 1

Drew McGowen
Drew McGowen

Reputation: 11706

In the general case, you can redirect stdout to a file with the > character:

your_program_name whatever arguments here > target.file
                                          ^^^^^^^^^^^^^

Upvotes: 2

Related Questions