69,371
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
int filecopy();
int main()
{
int a;
a=filecopy();
if(a==0)
{
printf("ok\n");
}
else
{
printf("error\n");
}
system("pause");
return 0;
}
int filecopy()
{
char ch;
FILE *fin;
FILE *fout;
if((fin = fopen("in.txt", "rb+")) == NULL) //也可以以"rt"文本方式打开
{
printf("can't open the file/n");
return (-1);
}
if((fout = fopen("out.txt", "wb")) == NULL) //也可以以"rt"文本方式打开
{
printf("can't open the file/n");
return (-1);
}
while(!feof(fin))
{
ch = fgetc(fin);
if(ch != EOF) //在这里加一个条件判断便可
{
fputc(ch, fout);
}
}
fclose(fin);
fclose(fout);
//system("PAUSE");
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int rename(char* oldname,char* newname)
{
char cmd[128] = "";
sprintf(cmd,"ren %s %s",oldname,newname); //ren %s %s是dos下的重命名命令,第一个%s:原文件名,第二个%s:新文件名
if(system(cmd) != 0) //执行命令
{
printf("执行命令:%s 失败 !\n",cmd);
return -1;
}
return 0;
}
int main(int argc,char* argv[])
{
int count = 1;
//char filename[64] = "inputdate.txt";
char oldname[64] = ""; //原文件名
char newname[64] = ""; //新文件名
//判断参数是否正确
if(argc != 2)
{
//printf("The num of parameter wrong!\n");
printf("Usage:%s message\n",argv[0]); //执行程序时:程序名 文件名
return -1;
}
strcpy(oldname,argv[1]);
//strcpy(oldname,filename);
//重命名
for(count = 1;count<=100;count++)
{
sprintf(newname,"%d",count);
strcat(newname,".txt");
//if(strcmp(oldname,filename) == 0)
if(strcmp(oldname,argv[1]) == 0)
{
if(rename(oldname,newname) != 0)
{
printf("rename failed!\n");
return -1;
}
}
if(strstr(oldname,"00") != 0)
{
if(rename(oldname,newname) != 0)
{
printf("rename failed!\n");
return -1;
}
}
memset(oldname,0,sizeof(oldname));
//memset(newname,0,sizeof(newname));
strcpy(oldname,"00");
sprintf(oldname+2,"%d",count);
strcat(oldname,".txt");
if(rename(newname,oldname) != 0)
{
printf("rename failed 1!\n");
return -1;
}
memset(newname,0,sizeof(newname));
}
return 0;
}