#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
FILE *fp;
char ch;
char buffer[80] ;
fp = fopen("c:\\Rasmi Personal\\hello.txt", "w");
if(fp == NULL)
{
printf("File not found");
exit(1);
}
else
{
while(1)
{
gets(buffer);
fwrite(buffer, strlen(buffer), 2, fp); /* I made, size_t nitems = 2 (third element/argument)*/
fwrite("\n", 1, 1, fp);
}
}
fclose(fp);
return 0;
}
输入:
Rasmi Ranjan Nayak
输出:
Rasmi Ranjan Nayak 0@ ÿ" 8ÿ"
为什么这个垃圾来了。
根据 fwrite() 函数。如果size_t nitems is more than 1
则输入的文本将被写入more than once
。但是这里为什么我会得到垃圾?