当 input.txt 文件传递​​给它时,简单的 C 代码无法正常工作

Simple C code not working properly when input.txt file is passed to it

所以我写了一个简单的 C 代码,它生成 N 个斐波那契数列,然后将每个成员写入链表节点,然后将其全部写出。

我在 Geany 的 windows 中编写代码,但我已经测试了 ubuntu 中的代码,因为 gcc 在 windows 中工作很奇怪。我已经编写了代码,它编译构建和 运行s,当我输入一切时它运行良好,一切都很好。 例如输入是 1 5 2 3。 1是进入N个成员的菜单选择, 5是用户选择的成员数, 2用于生成节点并将其写入节点和 3 用于全部打印出来。 一切都很好,但问题是,当我拿一个写有 1 5 2 3 的 in.txt 文件并将它传递给代码时 猫 in.txt | ./fibonacci > Ubuntu 中的 out.txt 命令,我的 out.txt 文件很大,包含大量重复输出。如果我直接输入数字 3,我会收到一条消息说 "The list is empty",这很好。 但是,当我在 in.txt 文件和 运行 命令中仅输入数字 3 时,我在 out.txt 中获得了 150 MB 的 "the list is empty"。所以当我把我的作业变成某种自动作业检查器时,我们在我的大学里发生了同样的事情,作业给了我一个大红色的不可以。我真的不明白问题是什么,为什么我有一个完美运行的程序,当它直接 运行 但是当一个输入文件用一个数字 3 传递给它时,我得到无限的 "the list is empty" 消息作为我在 out.txt 文件中的输出。

这是代码

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

#ifndef DEBUG
#define DEBUG(...) printf(__VA_ARGS__)
#endif

typedef struct Node {
    struct Node *next;
    int val;
} node;

int entry()
{
    int n;
    scanf("%d", &n);
    if(n <= 0) return -1;

    return n;

}
int make_list ( int val, node **head )
{
    node *new, *current;
    new=(node*) malloc(sizeof(node));
    if(!new) return -1;

    new->val = val;
    new->next=NULL;

    if(!*head){
        *head = new;
    }else {
        for(current=*head; current->next != NULL; current= current->next);
        current->next = new;
    }

return 0;
}

int fibonacci ( int n, node **head ){
    int i, f1=0, f2=1, next=0;
    if ( n <= 0 ) return -1;
    for(i=1; i<n; i++){
    if(i==1){
        make_list(f1, head);
        continue;
    }
    if(i==2){
        make_list(f2, head);
        continue;
    }
    next = f1+f2;
    f1=f2;
    f2=next;
    make_list(next, head);
}
return 1;
}

int print( node *head){
node *current;

if(!head) return -1;

for(current=head; current!=NULL; current=current->next){
    printf("%d ", current->val);
}
return 0;
}


int main(int argc, char **argv)
{
node *head=NULL;
char menu_choice;
int retval, n;

do {
    DEBUG("\n(1) Enter number of Fibonacci members \n(2) Generate N numbers             of Fibonacci \n(3) Print out list \n(e) Exit\n");

    scanf(" %c", &menu_choice);
    switch (menu_choice) {
        case '1':
            retval = entry();
            n = retval;
            if (retval == -1) printf("You entered negative number\n");
            break;
        case '2':
            retval = fibonacci(n+1, &head);
            if (retval==1) printf("Numbers are generated\n");
            if (retval==-1) printf("Not possible to generate numbers\n");
            break;
        case '3':
            retval = print(head);
            if (retval==-1) printf("List is empty\n");
            break;
    }
} while(menu_choice!='e');





return 0;

}

您似乎没有在所有情况下都初始化n

如果你先写 2 n 未初始化

node *head=NULL;
char menu_choice;
int retval, n;        <----

do {
    DEBUG("\n(1) Enter number of Fibonacci members \n(2) Generate N numbers             of Fibonacci \n(3) Print out list \n(e) Exit\n");

    scanf(" %c", &menu_choice);
    switch (menu_choice) {
    case '1':
        retval = entry();
        n = retval;
        if (retval == -1) printf("You entered negative number\n");
        break;
    case '2':
        retval = fibonacci(n+1, &head); <-- n defined only if case 1: first
        if (retval==1) printf("Numbers are generated\n");
        if (retval==-1) printf("Not possible to generate numbers\n");
        break;

您的输入不包含程序终止命令'e'。此外,您没有检查 scanf 的结果。因此,这两个结合使您的程序无限循环 menu_choice 中的陈旧值,在您的情况下为 '3',将相应的输出写入文件。