我曾经学习过C语言,可是真是三脚猫的功夫,加上好久不用,现在已经生疏的不行了,今天我想写一个简单的C程序,重拾过去学习C的那痛苦的记忆,果然木有让我失望,几行的代码,报错不少,下面是代码:
#include
#include
void main(){
char a[30];
printf("Enter the file name(include path):");
scanf("%s",a);
if(access(a,0) != -1){
printf("File already exits,open it now....\n");
FILE *fp = fopen(a,"a+");
char line[100];
/*while(fscanf(fp,"%s",line) != -1){
printf("%s\n",line);
}*/
while(fgets(line,100,fp) ){
printf("%s",line);
}
while(1){
fseek(stdin,0,SEEK_END);
printf("Append?");
scanf("%c",line);
if(line[0] == 'n' || line[0] == 'N'){
break;
}else{
printf("*******************************Append Begin**********************************\n");
printf("INPUT:");
//scanf("%s",line);
fseek(stdin,0,SEEK_END);
fgets(line,100,stdin);
//fprintf(fp,"%s\n",line);
fputs(line,fp);
printf("*******************************Append End**********************************\n");
}
}
fclose(fp);
}else{
printf("File not exist,create it.....\n");
FILE *fp = fopen(a,"w");
char content[100];
while(1){
fseek(stdin,0,SEEK_END);
printf("Enter your text(@ at the beginning to end):");
gets(content);
if(content[0] == '@'){
break;
}else{
fprintf(fp,"%s\n",content);
}
}
fclose(fp);
}
}
整个程序写的乱七八糟,有时间再调试吧,改改,先这样,现在基本能实现,新建文件和在append文件,以后可以尝试添加其他功能。。。。
阅读(1192) | 评论(0) | 转发(0) |