69,382
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 10
int main(void)
{
FILE *in, *out;
int ch;
char source[LEN];
char cpy[LEN];
int count = 0;
printf("请输入文件名:\n");
scanf("%s",source);
if ((in = fopen(source, "r")) == NULL)
{
fprintf(stderr, "I couldn't open the file \"%s\"\n",
source);
exit(2);
}
strncpy(cpy,source, LEN - 5);
//cpy[LEN - 5] = '\0';
/*就是这个注释,正确的时候应该不注释它。当我注释后运行时,最多只能输入4个字符的文件名,可以创建成功,再多
就会说fprintf里的"Can't create output file."。我想道字符串没有添加\0为知什么还能在不多于4个字符的文件名的
时候创建文件,多于4个就出错了,这是为什么呢?如果没有添加\0,他应该多少字符都出错,为什么只是在4个字符以上才
出错?*/
strcat(cpy,".txt");
if ((out = fopen(cpy, "w")) == NULL)
{
fprintf(stderr,"Can't create output 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;
}
char source[LEN] = {0};
char cpy[LEN] = {0};
char source[LEN];
char cpy[LEN];
memset(source, 0, sizeof(source));
memset(cpy, 0, sizeof(cpy));