English 中文(简体)
C号不同产出内容文件
原标题:Different output content file copy in C

Hello i had a simple copy file program in C but i cant explain why i get different output in the destination file when i use the 2nd method. The correct output with for loop:

I am the worst programmer in the world!
:D
 And this is bla bla bla bla
 more bla bla bla...

BUT, with while loop a arbitrary char is produced in EOF:

I am the worst programmer in the world!
:D
 And this is bla bla bla bla
 more bla bla bla...


法典

int main()
{
int i;
char ch;
create_files();
FILE *src = fopen("best.txt", "r");
FILE *dst = fopen("copied.txt", "w");
for(i=getc(src); i!=EOF; i=getc(src))  //correct copy
    {
        putc(i, dst);
    }

/* while(!feof(src))                  //woot?
    {
        ch=fgetc(src);
        fputc(ch,dst);
    }*/

fclose(dst);
fclose(src);
return 0;
}

void create_files()
{
    FILE *fp;
    fp = fopen("best.txt","w");
    fprintf(fp,"I am the worst programmer in the world!
:D
 And this is bla bla bla bla
 more bla bla bla...
");
    fclose(fp);
}

i 炉 used既使用烟.,又使用 put和植物,也使用同样方法。 是否忘记了什么?

最佳回答

什么

while (!feof(src)) {
    ch=fgetc(src);
    fputc(ch,dst);
}

确实:

  1. check for EOF
  2. read a character, possibly causing EOF
  3. output the character just read, without checking for EOF.

在《观察员部队》出现时,(3) 在检查之前,在下一次检查时,仍在(1)处执行。 特别价值EOF改为char和产出。

正确的住所是

while ((ch = fgetc(src)) != EOF)
    fputc(ch, dst);

www.un.org/spanish/ecosoc <代码>int,因为char/code> 页: 1 请注意检查中的派任;一些方案人员将告诉你,这很奇怪,但很多人使用检查方法,你也可以使用。 页: 1 休闲也是正确的。

(Aside 1: fputc is equivalent to putc and fgetc to getc, as long as you don t try to use them in a function pointer context.)

(单位:while) 查阅EOF 回返也赶上了。

问题回答

第一种假体的特性(可能发现EOF),然后检查发现低地轨道F的价值,并可能执行书写特征的碎片(直到所有特性都读)。

第二次随机检查,看看是否发现了低地轨道定位系统,发现其特性(可能发现低地轨道定位系统),书写特征(不考虑可能发生的情况),并可能继续具有下一个特征(如果发现低地轨道定位系统)。

在第二处休息室,如果是现场观察组,请在检查前写字。

轮机给你随机,因为《观察员部队》在阅读失败之前实际上没有旗帜。 因此,你在休息时发生的情况是:fgetc失败,建立了低温和低温潜能值,然后又回到你身上,然后由你印刷。

较好的办法是:

ch=fgetc(src);
while (!feof(src)) {
   fputc(ch,dst);
   ch=fgetc(src);
}

页: 1 它读写了一种额外性,即:红心代码(<>>>><>>>/代码(>)来自何处,你在产出档案中用<代码>fgetc()写出了地球静止轨道价值。

Edit

Looks like this isn t the actual problem, but I ll keep the answer as an advice.


The only thing I noticed is:

char ch;

根据您的制度,<代码>char 可以签署或签署,但以签署或签署方式签署或签署,但以签署或签署方式签署或签署。 页: 1

使用<代码>int,取而代之。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签