c - 打印到 C 中的文件

标签 c text-files output binary-search-tree

如何打印到我已经创建的空 .txt 文件

我已经将结果打印到控制台,现在我想打印到名为 "Output.txt" 的文件。我尝试了一些没有奏效的方法,但我认为创建一个专门用于打印到名为 printDictionaryToFile() 的文件的副本 printDictionary() 会更容易>。不过,我对如何去做有点迷茫。谁能纠正我哪里出错了?我已经为我的文件输出添加了一个名为 *out 的额外 FILE 类型。

#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;
}

最佳答案

您可以使用 fprintf()功能,与printf()非常相似以它的工作方式。

这是一个例子:

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

你的文件的输出是这样的:

This is being written in the file. This is an int variable: 5

值得一提的是,使用 w 作为参数打开文件会在每次打开文件时破坏文件内容。

关于c - 打印到 C 中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18950835/

相关文章:

c - 如何识别traceroute包?

excel - VBA二进制文件-cnc程序

c++ - 在一行代码中输入 2 个十六进制数字和一个字符串

php - 只上传txt文件

python - 使用python在文本文件中搜索行

python 根据二叉树检查值,空输出

C++输出二维 vector

c - 如何在没有 FPU 的机器上计算 float

c - 如何在 C 中一次原子地读取多个变量?

c 将可变长度 int 连接到字符串而不打印它