打印到C中的文件 [英] Printing to a file in C

查看:117
本文介绍了打印到C中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何打印到已经创建的空白.txt file?

How do I print to an empty .txt file I already have created?

我已经将结果打印到控制台,现在我想打印到名为"Output.txt"的文件.我已经尝试了一些没有用的方法,但是我认为创建重复的printDictionary()专门用于打印到名为printDictionaryToFile()的文件会更容易.不过,我对如何执行操作有些迷失.谁能纠正我在哪里出了问题?我已经为我的输出添加了一个名为*out的额外FILE类型到文件.

I already print the results to the console, and now I want to print to a file named "Output.txt". I've tried a couple of things that haven't worked, but I think it was easier to create a duplicate printDictionary() specifically for printing to a file called printDictionaryToFile(). I'm a little lost on how to do it though. Can anyone correct me on where I went wrong? I already added an extra FILE type called *out for my output to a 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() a>运作方式.

You can use the fprintf() function, which is quite similar to printf() in the way it works.

这里是一个例子:

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

文件上的输出为:

这正在写入文件中.这是一个int变量:5

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

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

Worth to mention that opening the file using w as parameter will destroy the file's content every time you open it.

这篇关于打印到C中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆