当fput直接用于在文件中写入字符数组时,会存储不同格式的文本(When fputs is used directly for writing character array in a file, a

编程入门 行业动态 更新时间:2024-10-25 16:24:28
当fput直接用于在文件中写入字符数组时,会存储不同格式的文本(When fputs is used directly for writing character array in a file, a different format of text is stored)

当我使用fputs()直接在文件中存储字符数组时,它将其存储在文件中:

ÈLwìþ(

那是为什么?

#include <stdio.h> int main() { FILE *p; p=fopen("pa.txt","w+"); char name[100]; printf("Enter a string :"); fputs(name,p); fclose(p); getchar(); return 0; }

当我使用scanf()或gets()名称输入时, gets()存储正确的文本,但是当使用fputs() ,它会以不寻常的格式存储。 为什么会这样?

When I used fputs() to directly store a character array in a file, it stored this in the file:

ÈLwìþ(

Why was that?

#include <stdio.h> int main() { FILE *p; p=fopen("pa.txt","w+"); char name[100]; printf("Enter a string :"); fputs(name,p); fclose(p); getchar(); return 0; }

When I take input in name using scanf() or gets(), the correct text is stored but when directly use fputs() is used it stored in an unusual format. Why does this happen?

最满意答案

当我使用scanf()或gets()名称输入时, gets()存储正确的文本,但是当直接使用fputs() ,它会以不寻常的格式存储。 为什么会这样?

在使用fputs写出之前,您还没有从stdin读取数据。

使用:

fgets(name, sizeof(name), stdin);

然后:

fputs(name, p);

When I take input in name using scanf() or gets() correct text is stored but when directly fputs() is used it stored in unusual format. Why this happens ?

You haven't read the data from stdin before writing it out using fputs.

Use:

fgets(name, sizeof(name), stdin);

and then:

fputs(name, p);

更多推荐

本文发布于:2023-08-03 18:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1396287.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   字符   文本   格式   文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!