这是两个单独的C程序,一个通过写入内容到.TXT,另一个读取.TXT文件并显示,我想在linux下通过编译两个.c文件,生成一个可执行程序
写入:
#include
#include
#include
#include
int main()
{
FILE *fp;
char ch[20];
if ((fp = fopen("E:\student.txt", "w")) == NULL)
{
printf("can not open this file\n");
exit(1);
}
else
while(1)
{
fgets(ch, sizeof(ch), stdin);
if (ch[0] == 'q')
break;
fprintf(fp, "%s", ch);
}
fclose(fp);
return 0;
}
读取:
int main(void)
{
FILE *fp;
int ch;
fp = fopen("E:\\student.txt", "r");
if (NULL == fp)
{
printf( "File\n") ;
exit(1) ;
}
ch = fgetc(fp);
while (ch)
{
putchar(ch);
ch = fgetc(fp);
if(feof(fp))
{
break;
}
}
printf("\n");
fclose(fp);
return 0;
}
保留一个文件中的main函数,另一个文件中的maon函数换成其它有代表性的名字。之后写Makefile,语句示例如下:
gcc write.c read.c -O xxx