void filecopy(FILE *ifp, FILE *ofp)
{
int c;
while((c = getc(ifp))!= EOF)
putc(c,ofp);
}
所以,我尝试了:
void filecopy(FILE *ifp, FILE *ofp)
{
int c;
int count = 0;
while((c = getc(ifp))!= EOF)
if(count == 50){
putc("n",ofp);//This didnt work
count = 0;
}
putc(c,ofp);
}
我应该使用某种类型的指针吗? 我不太擅长C指针,有人知道吗? 谢谢。
您的putc
正在尝试输出一个字符串,这实际上是一个指针。 putc
只是将最初的 8 位作为变量的字符,在这种情况下肯定不是n
。
您可能想要(注意单引号):
putc('n', ofp);
如果您使用的是 windows,则可能需要输出rn
才能获得所需的结果。
最后,您的循环不是每测试50个字符,而是在每次循环迭代时输出值。 我假设你已经这样做了作为测试。
几个问题:
- 您的
while
环需要牙套 -
'n'
不"n"
- 递增
count
最终代码应如下所示:
void filecopy(FILE *ifp, FILE *ofp)
{
int c;
int count = 0;
while((c = getc(ifp))!= EOF){
if(count == 50){
putc('n',ofp);//This didnt work
count = 0;
}
putc(c,ofp);
count++;
}
}
基于@Paul的正确答案,您可以使用模来决定何时输出换行符:
if(++count % 50 == 0){
putc('n', ofp);
}
答案是:
void filecopy(FILE *ifp, FILE *ofp)
{
int c;
int count = 0;
while((c = getc(ifp))!= EOF)
if(count == 50){
printf("n");
putc(c,ofp);
count = 0;
}
else
putc(c,ofp);
count++;
}