C 将输入记录到文件?

发布于 2024-10-12 21:59:08 字数 954 浏览 1 评论 0原文

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

#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’

I'm working on a program that logs input to a file. This is my current code:

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

When I compile, I get this error:

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’

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寻找一个思念的角度 2024-10-19 21:59:08

好吧,您需要在末尾添加一个分号:

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

您可能还希望 stop 来匹配其原型:

/* includes */
int stop();

/* main etc */
int main(int argc, char** argv)
{
    /* main code */
}

int stop()
{
    endwin();
    exit(0);
}

对于 exit() 您需要 #include

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

Well, you need a semicolon on the end of:

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

You probably also want stop to match its prototype:

/* includes */
int stop();

/* main etc */
int main(int argc, char** argv)
{
    /* main code */
}

int stop()
{
    endwin();
    exit(0);
}

And for exit() you need #include <stdlib.h>

Oh and to compile this, let's say it's called test.c, then use gcc -lcurses test.c -o test. This tells gcc you want to link with libcurses.

浅浅 2024-10-19 21:59:08

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

#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 */
}

Your code has quite a few problems. See my embedded comments for a version that compiles and presumably does what you want in a more sane manner.

#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 */
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文