【问题标题】:C logging input to file?C记录输入到文件?
【发布时间】:2011-01-17 19:18:27
【问题描述】:

我正在开发一个将输入记录到文件的程序。这是我当前的代码:

#include <stdio.h>
#include <curses.h>
#include <signal.h>
#define WAIT 3
#define INCORRECT "Incorrect input\n"
#define FILENAME ".xintrc"

int stop();

int main()
{
    char first[10], last[10];
    int i;
    FILE *fp, *fopen()
    initscr();
    scanf("%[^\n]", first);
    getchar();
    noecho();
    scanf("%[^\n]", last);
    printf("\n");
    getchar();
    echo();
    sleep(WAIT);
    if((fp = fopen(FILENAME, "a")) != NULL){
    fprintf(fp, "First:   %s Last: %s\n", first, last);
    fclose(fp);
  }

    printf(INCORRECT);
    endwin();
 }
    stop()
 {
    endwin();
    exit(0);
 }

当我编译时,我得到这个错误:

input1.c: In function ‘main’:
input1.c:15: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘initscr’
input1.c: In function ‘stop’:
input1.c:35: warning: incompatible implicit declaration of built-in function ‘exit’

【问题讨论】:

    标签: c file input


    【解决方案1】:

    嗯,你需要一个分号在结尾:

    FILE *fp, *fopen(); /* ; here! */
    

    你可能还想停止匹配它的原型:

    /* includes */
    int stop();
    
    /* main etc */
    int main(int argc, char** argv)
    {
        /* main code */
    }
    
    int stop()
    {
        endwin();
        exit(0);
    }
    

    对于exit(),您需要#include &lt;stdlib.h&gt;

    哦,编译这个,假设它被称为test.c,然后使用gcc -lcurses test.c -o test。这告诉 gcc 你想与 libcurses 链接。

    【讨论】:

    • 现在我得到:input1.c:37:错误:重新定义'stop' input1.c:9:注意:之前的'stop'定义在这里
    • 您的第一个停止声明很好 int stop(); 以上主要。在它下面,你想要实现它但前面有 int 的函数。我将编辑我的答案。
    • 这样更有意义吗?我猜你编辑的是顶部的“停止”而不是底部?
    • 当这种情况发生时,你几乎肯定有不平衡的括号、大括号或某处缺少分号。仔细检查,逐行适当缩进等。它让生活更轻松!
    • 我把它放在外面,就像它在编辑中一样,但我仍然得到同样的错误:/它显然讨厌我。编辑:哦,我在编译器中添加了 -lcurses 并且它起作用了。谢谢
    【解决方案2】:

    您的代码有很多问题。请参阅我的嵌入式 cmets 以获取可以编译并可能以更理智的方式执行您想要的操作的版本。

    #include <stdio.h>
    #include <stdlib.h> /* for _Exit() */
    #include <string.h> /* for strcspn() */
    /* #include <curses.h> you don't need curses for such a simple program */
    /* #include <signal.h> this header is not needed */
    
    #define WAIT 3
    #define INCORRECT "Incorrect input\n"
    #define FILENAME "testfile"
    
    void stop(void); /* match the new prototype */
    
    int main(void) /* main() always returns int, use void if not using argc/argv */
    {
      char first[10], last[10];
      /* int i;  You never use this */
      FILE *fp; /* You don't need *fopen() here */
    
      /* initscr(); */
      /* noecho(); */
    
      printf("Enter first name: "); /* Added by me */
      fgets(first, sizeof(first), stdin); /* Don't use scanf, fgets prevents overflows */
      first[strcspn(first,"\n")] = '\0'; /* chomp the newline if it exists */
    
      printf("Enter last name: "); /* Added by me */
      fgets(last, sizeof(last), stdin); /* Don't use scanf, fgets prevents overflows */
      last[strcspn(last,"\n")] = '\0'; /* chomp the newline if it exists */
    
      /* echo(); */
      sleep(WAIT);
    
      if((fp = fopen(FILENAME, "a")) != NULL){
        fprintf(fp, "First:   %s Last: %s\n", first, last);
        fclose(fp);
        stop(); /* You never call this, i'm guessing you want it here */
      }
    
      printf(INCORRECT); /* only called if fopen() fails */
    
      /* endwin(); */
    
      return 0; /* mandatory return for main() */
    }
    
    void stop(void) /* Use 'void' if the func takes no params and returns nothing */
    {
      /* endwin(); */
      _Exit(0); /* _Exit is apart of C99 */
    }
    

    【讨论】:

    • 啊,谢谢,效果很好:)失败的原因是因为我使用了与 80 年代不同的代码来帮助我。
    • @AustinM 还有几个 cmets。 1)sleep()有点别扭,真的需要吗? 2) 使用_Exit() 停止程序通常不是解决此问题的正确方法。如果fopen() 成功,而不是调用stop(),只需使用else 子句调用printf(INCORRRECT)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-12
    • 1970-01-01
    • 2022-10-17
    • 2013-08-03
    相关资源
    最近更新 更多