Afdrukken naar een bestand in C

Hoe druk ik af naar een leeg .txt filedat ik al heb gemaakt?

Ik print de resultaten al naar de console en nu wil ik afdrukken naar een bestand met de naam "Output.txt". Ik heb een aantal dingen geprobeerd die niet hebben gewerkt, maar ik denk dat het gemakkelijker was om een duplicaat printDictionary()te maken, speciaal voor het afdrukken naar een bestand met de naam printDictionaryToFile(). Ik ben echter een beetje de weg kwijt over hoe ik het moet doen. Kan iemand mij corrigeren waar ik fout ben gegaan? Ik heb al een extra type FILEmet de naam *outtoegevoegd voor mijn uitvoer naar een bestand.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stddef.h>
#define PUNC    " \t\n\r,;.:!()[]{}?'\""
typedef struct node node;
typedef struct node {
    char *word;
    int count;
    node *left;
    node *right;
} node;
void insert(node ** dictionary, char * word) {
    int result;
    node * entry;
    if (word == NULL || dictionary == NULL)
        return;
    if (*dictionary == NULL) {
        entry= (node *) malloc(sizeof(node));
        strcpy( entry->word= (char *) malloc(strlen(word) + 1), word);
        entry->left= entry->right= NULL;
        entry->count= 1;
        *dictionary= entry;
        return;
    }
    result = strcmp(word, (*dictionary)->word);
    if ( result < 0 )
        insert(&(*dictionary)->left, word);
    else if (result > 0)
        insert(&(*dictionary)->right, word);
    else
        ++(*dictionary)->count;
    return;
}
void printDictionary(node * dictionary) {
    if (dictionary == NULL)
        return;
    printDictionary(dictionary->left);
    printf( "%s = %d\n", dictionary->word, dictionary->count);
    printDictionary(dictionary->right);
    return;
}
void printDictionaryToFile( node * dictionary ) {
    if (dictionary == NULL)
        return;
    printDictionaryToFile(dictionary->left);
    fprintf(out, "%s = %d\n", dictionary->word, dictionary->count);
    printDictionaryToFile(dictionary->right);
    return;
}
void freeDictionary( node ** dictionary ) {
    if (dictionary == NULL || *dictionary == NULL)
        return;
    freeDictionary(&(*dictionary)->left);
    freeDictionary(&(*dictionary)->right);
    free((*dictionary)->word);
    free(*dictionary);
    *dictionary= NULL;
    return;
}
int main( int argc, char *argv[] ) {
    FILE *fp, *out;
    out = fopen("Output.txt", "w");
    char b[1000], *s;
    node *dictionary= NULL;
    int i;
    for (i= 1; i < argc; ++i) {
        if ((fp = fopen(argv[i], "r")) == NULL) {
            fprintf(stderr, "File %s can not be opened.\n", argv[i]);
            continue;
        }
        for (s = fgets(b, sizeof(b), fp); s != NULL; s = fgets(b, sizeof(b), fp)) {
            char *word;
            for (word= strtok(b, PUNC); word != NULL; word = strtok(NULL, PUNC))
                insert(&dictionary, strlwr(word));
        }
        fclose(fp);
    }
    printDictionaryToFile(dictionary);
    printDictionary(dictionary);
    freeDictionary(&dictionary);
    return 0;
}

Antwoord 1, autoriteit 100%

U kunt de fprintf()functie, die vrij gelijkaardig is aan printf()in de manier waarop het werkt.

Hier is een voorbeeld:

FILE *fp;
int myInt = 5;
fp = fopen("Output.txt", "w");// "w" means that we are going to write on this file
fprintf(fp, "This is being written in the file. This is an int variable: %d", myInt);
fclose(fp); //Don't forget to close the file when finished

De uitvoer op uw bestand zou dit zijn:

Dit wordt in het bestand geschreven. Dit is een int-variabele: 5

De moeite waard om te vermelden dat het openen van het bestand met wals parameter de inhoud van het bestand vernietigt telkens wanneer u het opent.

Other episodes