关于文件操作。。
#include<stdio.h>#include<stdlib.h>
#include<string.h>
#define LEN 40
int main(int argc,char *argv[])
{
FILE *in,*out;
int ch;
char name[LEN];
int count=0;
if(argc<2)
{
fprintf(stderr,"Usage: %s filename\n",argv[0]);
exit(1);
}
if((in=fopen(argv[1],"r"))==NULL)
{
fprintf(stderr,"I couldn't open the file \"%s\"\n",
argv[1]);
exit(2);
}
strcpy(name,argv[1]);
strcat(name,".red");
if((out=fopen(name,"w"))==NULL)
{
fprintf(stderr,"Can't creat puput file.\n");
exit(3);
}
while((ch=getc(in))!=EOF)
if(count++%3==0)
putc(ch,out);
if(fclose(in)!=0||fclose(out)!=0)
fprintf(stderr,"Error in closing files.\n");
return 0;
}
代码如上,生成.exe为peoject8.exe 用命令行操作时,我把peoject8.exe复制到了lenovo文件夹下,并创建了tet.txt文件,
运行后提示I couldn't open the file "tet"
因为是刚接触文件操作,所以不知道哪里错了,希望大家可以指导下