简体   繁体   中英

Write an entire program to a file in C

I'm quite new to C programming, but I'm starting to get the hang of it. For a school assignment, I have written a code in C to play Blackjack. One of the requirements for the assignment, is that is has to have an option that saves the entire output (everything that's shown in cmd when the program is build and run). I can't find how to do this anywhere.

Is there a way to write the entire output into a file?

Thanks in advance!

There are 3 ways to achieve what you want.

  1. Use a file. This is the way I recommend. You will need the functions fopen to open a file and enter link description here to print the data in the file. I suggest you read the documentation of the functions in the links and look at the examples.

  2. Redirect stdout to a file using freopen . This basically puts everything that you see now in the console in a file, by adding just one line.

  3. Redirect the output of the program in a file. This is more a shell thing than a C programming technique, but I think it is worth mentioning. In an *NIX environment, the command ./a.out > file.txt will redirect the output of a.out to a file called file.txt in a similar manner freopen does.

You can pipe the stdout and stderr to a file when you build and run. For example, on Linux using the bash shell you can do try this (where the "build_script" is what you use to build and the "a.exe" is the program name):

$ ./build_script >& build_out.txt
$ ./a.exe >& run_out.txt &

The ">&" tells bash to pipe both stdout and stderr to the file. The final "&" in the second line tells bash to run the program in the background. It's hard to tell if this answer will suit your purposes since it's not clear exactly how the game is played from what you have posted. Eg, if you need to use stdin/stdout to play the game then maybe piping the "a.exe" stdout to a file might not work...

I'm assuming simple output redirection is not an option:

$ app > file.txt

Probably, you are using printf to print data to console. Instead of printf , you can use fprintf . fprintf can write data to an arbitrary file, just like printf does to the standard output (which is also a file), in this case the console.

You must first open the file where you will write the output. The command fopen will do this for you:

// this will open or create the file as text to write data.
FILE *f = fopen("my-file.txt", "w");

With the f variable (which you should check for NULL in case of error), you can pass it to fprintf to write data:

fprintf(f, "my super string: %s", string);

Note that despite the first argument being a FILE* , everything else behaves like printf . Actually you can think of printf as a wrapper where the first argument of the fprintf is always stdout .

Do not forget to close your file after you write data!

fclose(f);

This can be done once, after all the data is written to file.

Check the man pages for more info about these commands.

There are more complex (not that much actually) ways of accomplishing this, like using freopen , but I'm leaving this out of the answer. If you need more, update your answer.

EDIT

In your comment, you said you must save or not the output to a file at the end of the program. Well, the file management stuff above you still be usefull. The changes are the following:

You must store the output somewhere in order to decide whether to write to a file or not at the end of the program. Probably you are doing some data formatting with printf . You will have to change your calls from printf to snprintf . This command will write your data to a string, just as printf does prior to output it to the stdout , but it will skip the print-to-output- part.

Then, store the buffer at a list of strings and at the end of the program you write this list to the file or not.

This has some complications: you need a list of strings (a dynamically allocated array of arrays will be enough, actually); how big must your buffer be?

snprintf will return the size required to print the passed data to the buffer, no matter if the buffer is larger or smaller then the given one. But, if it is smaller, you will have to increase its size (reallocating it) and call again snprintf :

char *buffer = malloc(sizeof(char) * 41); // 40 chars + \0!
int size;
if ( size = snprintf(buffer, 41, "your format here", args) > 40 ) {
    buffer = realloc(buffer, sizeof(char) * (size + 1));
    snprintf(buffer, size + 1, "your format here", args);
}
// now your buffer have the correct data!
// print it to stdout!
puts(buffer);
// store it at the string list.
// execise!

Is left as an exercise wrapping this piece of code in a command to avoid repeat it everywhere you print anything. Also, it is an exercise to create the list of strings.

When you decide to write data to file, instead of use fprintf you can use fputs , as data is already formatted.

I don't know if there is an easier way. I think not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM