tags:

views:

100

answers:

2

I am using a basic C code to print to a text file:

FILE *file;
file = fopen("zach.txt", "a+"); //add text to file if exists, create file if file does not exist

fprintf(file, "%s", "This is just an example :)\n"); //writes to file
fclose(file); //close file after writing

printf("File has been written. Please review. \n");

My question is regarding the above code: I have multiple lines I have printed that I would like to be saved to the text document. How can I easily include multiple lines of code to be printed in my file using the above code?

+2  A: 

Move file writing into a procedure:

void write_lines (FILE *fp) {
    fprintf (file, "%s\n", "Line 1");
    fprintf (file, "%s %d\n", "Line", 2);
    fprintf (file, "Multiple\nlines\n%s", "in one call\n");
}

int main () {
    FILE *file = fopen ("zach.txt", "a+");
    assert (file != NULL); // Basic error checking
    write_lines (file);
    fclose (file);
    printf ("File has been written. Please review. \n");
    return 0;
}
John Millikin
Why not use `fputs()` and avoid the dangers and overhead of the format string? It's what you're practically doing anyway.
Chris Lutz
You can also avoid having to call fprintf or fputs over and over.#define my_string "line1\nline2\nline3"fputs(my_string, file);
KFro
@KFro - Note that `fputs()` will _not_ append a newline, unlike regular `puts()` (standards error mutch?). However, I prefer to split it up into multiple different function calls, or else use the automatic concatenation of string literals to put lines of the string on their own lines. Don't need to hide that with a macro.
Chris Lutz
@Chris, @KFro: I've updated the listing to have more complex examples. `fprintf()` is used to match the original code, to avoid confusing the OP.
John Millikin
Ah that makes sense. So you are making the long list of print statements into a function? What are you actually doing there? Sorry, I am trying to learn instead of just get the answer :)Thank you for adding the error handling. I was going to search for that :)
HollerTrain
@Chris: The missing newline was a mistype. Note that you "can" do these things, not you "must". Obviously there are lots of ways to skin that cat, esp. for something as open-ended as the original question, as John has demonstrated :)
KFro
+1  A: 

There are lots of ways to do this, here's one:

#include<stdio.h>
#include<string.h>

int appendToFile(char *text, char *fileName) {

    FILE *file;

    //no need to continue if the file can't be opened.
    if( ! (file = fopen(fileName, "a+"))) return 0;

    fprintf(file, "%s", text);
    fclose(file);

    //returning 1 rather than 0 makes the if statement in
    //main make more sense.
    return 1;

}

int main() {

    char someText[256];

    //could use snprintf for formatted output, but we don't
    //really need that here. Note that strncpy is used first
    //and strncat used for the rest of the lines. This part
    //could just be one big string constant or it could be
    //abstracted to yet another function if you wanted.
    strncpy(someText, "Here is some text!\n", 256);
    strncat(someText, "It is on multiple lines.\n", 256);
    strncat(someText, "Hooray!\n", 256);

    if(appendToFile(someText, "zach.txt")) {
        printf("Text file ./zach.txt has been written to.");
    } else {
        printf("Could not write to ./zach.txt.");
    }

    return 0;

}

notice the strncpy and strncat functions since you aren't really utilizing the formatted input that comes with the xprintf functions.

Carson Myers