【问题标题】:File Handling and export into text file文件处理和导出到文本文件
【发布时间】:2017-09-06 09:41:05
【问题描述】:

我需要你的帮助。我想将我的代码输出导出到文本文件中。我真的不知道如何处理它。有人可以帮我解决这个问题。非常感谢

这是我的代码:

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

struct node {
int data;
struct node *next;
};
struct node *top = NULL;

struct node* createNode(int data){
struct node *p = (struct node *) malloc(sizeof (struct node));
p->data = data;
p->next = NULL;
}

void push (int data){
    struct node *ptr = createNode(data);
    if (top == NULL){
        top = ptr;
    return;
    }
    ptr->next = top;
    top = ptr;
}

int pop(){
    int data;
    struct node *temp;
    if (top == NULL)
        return -1;
    data = top->data;
    temp = top;
    top =top->next;
    free(temp);
return (data);
}

int main(){
        char str[100];
        int i = 0, data = -1, operand1, operand2, result;
        printf("Expression in postfix format: ");
        fgets(str, 99, stdin);
            for (; i < strlen(str); i++){
                if (isdigit(str[i])){
                data = (data == -1) ? 0 : data;
                data = (data * 10) + (str[i] - 48);
                continue;
            }
            if (data != -1){
            push(data);
            }
            if (str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/'){
            operand2 = pop();
            operand1 = pop();
                if (operand1 == -1 || operand2 == -1)
                    break;

        switch (str[i]){
            case '+':
                    result = operand1 + operand2;
                    push(result);
                    break;
            case '-':
                    result = operand1 - operand2;
                    push(result);
                    break;
            case '*':
                    result = operand1 * operand2;
                    push(result);
                    break;
            case '/':
                    result = operand1 / operand2;
                    push(result);
                    break;
                }
            }
                    data = -1;
        }
                    if (top != NULL && top->next == NULL)
                        printf("Postfix Evaluation: %d\n", top->data);
                    else
                        printf("Invalid Expression!\n");
        return 0;

}

【问题讨论】:

  • 您需要将 printfs 替换为 fprintfs
  • prog &gt; output.txt
  • 或使用freopen
  • 贴出的代码编译不干净!。除其他外,它缺少以下声明:#include &lt;ctype.h&gt; for the isdigit()
  • 函数:createNode() 签名表示它返回指向struct node 的指针。但是,该函数中不存在这样的 return 语句。

标签: c stack postfix


【解决方案1】:

您必须在 main 中打开文件:

FILE *fp;
fp = fopen ("whatever.txt", "w+"); // w+ mean that you open the file 
                                   // write and read, but if it
                                   // not exist, will be made.

并将printf 更改为fprintf

fprintf(fp, "%d\n", i); // you must add the name of the file where to
                        // print your stuff (fp)

main结束,记得关闭文件连接:

fclose (fp);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-16
    • 1970-01-01
    • 1970-01-01
    • 2017-06-18
    • 2020-06-21
    • 1970-01-01
    • 2017-08-25
    • 2014-06-23
    相关资源
    最近更新 更多