鱼C论坛

 找回密码
 立即注册
查看: 3506|回复: 5

如题:puts(),'\0' 打印输出

[复制链接]
发表于 2015-6-29 12:56:45 | 显示全部楼层 |阅读模式
5鱼币
本帖最后由 lark 于 2015-6-29 12:56 编辑

这里有两个代码
问题是:这两个代码,代码1,直接输出,在没加'\0'的情况下,会继续输出乱码;代码2,以函数调用的方式,不管有没有加'\0',都不会输出乱码;
           所以,我想问:这是 编译自动处理 的结果 还是 函数调用 的亦或 是其他 或 C的原因?
/*这个代码数组都没有初始化;
*编译器:C-Free 5.0(未注册版);
*/



插入代码1:
(说明下:)
  1. #include <stdio.h>
  2. #include <conio.h>
  3. int main(void){
  4.         FILE *fp=fopen("F:\\MYTXT.TXT","w+");
  5.     int i;
  6.     char string[20];
  7.     for(i=0;i<10;i++)
  8.         string[i]='a';
  9.     puts(string);
  10.     fputs(string,fp);
  11.     fclose(fp);
  12.     getch();
  13.     return 0;
  14. }
复制代码

插入代码2:
  1. #include <stdio.h>
  2. #include <conio.h>
  3. /*语言:C 语言;
  4. * Novice need to pay attention(初学者要注意;
  5. */
  6. void NOT_0        (void);        //不加'\0'的输出;
  7. void HAVE_0 (void); //加'\0'的输出;

  8. void NOT_0        (void){
  9.         FILE *fp=fopen("F:\\NOT_0.txt","w+");
  10.     int i;
  11.     char string[20];
  12.     for(i=0;i<10;i++)
  13.         string[i]='a';
  14.                 fputs(string,fp);
  15.         fclose(fp);
  16.     puts(string);   
  17. }

  18. /*从此例中可看到puts输出字符串时要遇到'\0’也就是字符结束符才停止,
  19. *如上面的程序加上一句 string[10]='\0';
  20. */

  21. void HAVE_0 (void){
  22.         FILE *fp=fopen("F:\\HAVE_0.txt","w+");
  23.     int i;
  24.     char string[20];
  25.     for(i=0;i<10;i++)
  26.         string[i]='a';
  27.     string[10]='\0';
  28.     puts(string);
  29.         fputs(string,fp);
  30.         fclose(fp);
  31.     getch();
  32. }


  33. int main(void){
  34.         NOT_0();
  35.         HAVE_0();
  36.         //在此注意调用NOT_0()和HAVE_0()的区别;
  37.         return 0;
  38. }
复制代码


图片:

这是运行结果,上面的是代码1,下面的是代码2;的结果;

这是运行结果,上面的是代码1,下面的是代码2;的结果;

这是输出到时文件的结果,证明NOT_0确实是'\0'结束了,(代码2函数)

这是输出到时文件的结果,证明NOT_0确实是'\0'结束了,(代码2函数)

最佳答案

查看完整内容

附上MSDN对于两个函数的简单概述, 摘要如下 fputs: Each of these functions copies string to the output stream at the current position. fputws copies the wide-character argument string to stream as a multibyte-character string or a wide-character string according to whether stream is opened in text mode or binary mode, respectively. Neither function copies the terminating null character. puts: T ...
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-6-29 12:56:46 | 显示全部楼层
本帖最后由 ryxcaixia 于 2015-6-30 09:06 编辑

附上MSDN对于两个函数的简单概述, 摘要如下
fputs:
Each of these functions copies string to the output stream at the current position. fputws copies the wide-character argument string to stream as a multibyte-character string or a wide-character string according to whether stream is opened in text mode or binary mode, respectively. Neither function copies the terminating null character.

puts:
The puts function writes string to the standard output stream stdout, replacing the string’s terminating null character ('\0') with a newline character ('\n') in the output stream

简单来说 puts会自定替换第一个\0字符为一个换行符('\n')
fputs写入标准流时, 不会将'\0'字符一并写入(这是肯定的 影响美观)

但两者都不会对字符串作一个加上'\0'的操作
即原来的字符串长什么样子 就输出什么样子
原来的是乱码 就输出乱码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-6-29 17:05:39 | 显示全部楼层
表示只要不加 '\0' 都会乱码,因为字符串要以 '\0' 结尾,没有的话用字符串函数puts,fputs就会乱码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-6-30 08:58:47 | 显示全部楼层
楼主 我表示我这边输出的 NOT_0 也是乱码 并且 这应该是理想的结果
因为及时在NOT_0内  也是一堆乱码
楼主可以调试跟踪下程序 看看是不是编译器自动进行了栈内存清零操作

1.png
2.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2015-7-2 17:19:02 | 显示全部楼层
ryxcaixia 发表于 2015-6-30 08:58
楼主 我表示我这边输出的 NOT_0 也是乱码 并且 这应该是理想的结果
因为及时在NOT_0内  也是一堆乱码
楼 ...

嗯,谢谢了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2015-7-2 17:21:47 | 显示全部楼层
哥斯拉不说话 发表于 2015-6-29 17:05
表示只要不加 '\0' 都会乱码,因为字符串要以 '\0' 结尾,没有的话用字符串函数puts,fputs就会乱码

:lol:这个感觉看编译器了,就像有些编译器会自动加 “任意键结束一样”,而有些则不会
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-20 10:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表