我可以在二进制中打开的文件中使用fput和fgets吗
文件中的数据是随机的,字母和数字。
/**** Program to APPEND one file after another file ****/
#include <stdio.h>
#include <fcntl.h>
int main()
{
FILE *fp, *fc;
char data[200];
fp = fopen("students.DAT", "ab");
if (fp == NULL)
{
printf("Can't open file");
exit(1);
}
fc = fopen("pr1.DAT", "rb");
if (fc == NULL)
{
printf("Can't open file");
exit(2);
}
fseek(fp, 0, SEEK_END);
while (fgets(data, 199, fc) != NULL)
{
fputs(data, fp);
}
fputs("\n", fp);
return 0;
}
在此程序中使用fputs
和fgets
是否使用? 如果不善待使用什么,为什么fgets
/puts
不对?
The data in file is random and both alphabet and numbers.
/**** Program to APPEND one file after another file ****/
#include <stdio.h>
#include <fcntl.h>
int main()
{
FILE *fp, *fc;
char data[200];
fp = fopen("students.DAT", "ab");
if (fp == NULL)
{
printf("Can't open file");
exit(1);
}
fc = fopen("pr1.DAT", "rb");
if (fc == NULL)
{
printf("Can't open file");
exit(2);
}
fseek(fp, 0, SEEK_END);
while (fgets(data, 199, fc) != NULL)
{
fputs(data, fp);
}
fputs("\n", fp);
return 0;
}
Is the use of fputs
and fgets
in this program OK?
If not kindly tell the what to use and why fgets
/puts
is not ok?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在二进制模式下,您应该使用
fread
而不是fgets
和fwrite
而不是fputs
。不同之处在于,如果您使用fwrite
函数,则应提供作为参数:In binary mode you should use
fread
instead offgets
andfwrite
instead offputs
. The difference is if you usefwrite
function you should provide as arguments: