C 将输入记录到文件?
我正在开发一个将输入记录到文件的程序。这是我当前的代码:
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,您需要在末尾添加一个分号:
您可能还希望 stop 来匹配其原型:
对于
exit()
您需要#include
哦,要编译它,假设它名为
test.c
,然后使用gcc -lcurses test.c -o test
。这告诉 gcc 您想要与 libcurses 链接。Well, you need a semicolon on the end of:
You probably also want stop to match its prototype:
And for
exit()
you need#include <stdlib.h>
Oh and to compile this, let's say it's called
test.c
, then usegcc -lcurses test.c -o test
. This tells gcc you want to link with libcurses.您的代码有很多问题。请参阅我嵌入的注释,了解可以编译并可能以更理智的方式执行您想要的操作的版本。
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.