stdout重定向更改输出

如何解决stdout重定向更改输出

| 我有一个名为
abc
的程序。 当我运行以下命令时:
$ ./abc < infile
我得到以下输出:
ijklm
但是,当我运行时:
$ ./abc < infile > outfile
$ cat outfile
我得到这个输出:
ijkoo
现在,我假设这是我程序的错误。但是,无论我的程序在做什么,我都不知道这是怎么可能的。 编辑: 现在,我知道这是可能的,我很好奇程序中导致这种情况的原因。 我的程序中的循环内有一个块,其中包含:
byte = ascii_to_byte(asciibyte);
putchar(byte);
字节的类型为“ 6”。 现在,如果将
putchar(byte)
更改为
printf(\"%c\",byte)
,所有输出将保持不变。 但是,如果将其更改为
printf(\"%d\",byte)
,则
$ ./abc < infile
输出:
105106107111111
这是
outfile
中那些ASCII字符的十进制表示形式。但这不是字符的十进制表示,因为它们刚被发送到stdout时实际出现。我不明白为什么会有这种差异。 编辑#2: 如果将打印行更改为
printf(\"%c\\n\",byte)
,则
$ ./abc < infile
输出:
i
j
k
o
o
这与输出文件中的内容一致。同样,不知道有什么区别。 编辑#3 我刚刚在32位计算机上对此进行了测试,该程序可以运行:
outputfile
包含
ijklm
。怪异的 编辑#4 这是主要功能:
int main()
{
    char asciibyte[8];
    char byte;

    int c; //Using int to avoid the EOF pitfall.
    long charcount = 0;

    while((c = getchar()) != EOF){
        if(c != \'0\' && c != \'1\'){
            continue;
        }
        asciibyte[charcount % 8] = c;
        if(charcount % 8 == 7){
            /*Testing revealed that at this point asciibyte does contain
            what it should contain,eight ASCII ones and zeros representing
            a byte read in from stdin*/
            byte = ascii_to_byte(asciibyte);
            /*Print statements such as:
                printf(\"%d\",byte);
                printf(\"%c\\n\",byte);
            reveal that the ascii_to_byte function works incorrectly on my
            64 bit machine. However these statements:
                putchar(byte);
                printf(\"%c\",byte);
            make it appear as though the function operates as it should.
            EXCEPT if i redirect that output to a file.*/
            putchar(byte);
        }
        charcount++;
    }
    return 0;
}
这是ascii_to_byte函数:
char ascii_to_byte(char *asciibyte){
    char byte;
    int i;
    for(i = 0; i < 8; ++i){
        if(asciibyte[7-i] == \'1\'){
            byte = byte | (1 << i);
        }
    }
    return byte;
}
最后编辑 我注意到我应该将字节初始化为0x00。问题解决了。我为什么这么弱智?我将给可以具体解释造成问题的原因的人提供答案。     

解决方法

这种看似无关的更改来来去去,这种奇怪的行为可能表明您的程序不应该从程序中读取或写入内存,并且随着代码其他部分对堆栈的不同使用,行为也会发生变化。和/或堆。 我会仔细检查您的代码是否有缺陷,例如缓冲区溢出,返回指向堆栈上变量的指针的函数等。 使用调试器单步执行代码可能会很有效率(或者,如果您不走运,它可能会再次更改行为!)。 您已经看到了一些有趣的事情:
stdout
的重定向如何影响任何事情?也许是因为它导致C库的行为有所不同:流使用不同的缓冲模式,具体取决于流是否连接到终端设备(请参阅GNU libc文档,或C99§7.9.13段。 7)。 当
printf(\"%d\",byte)
printf(\"%c\\n\",byte)
都改变了行为时,为什么将
putchar(byte)
更改为
printf(\"%c\",byte)
并没有任何改变?也许是因为编译器会自动将
printf(\"%c\",byte)
改成效率更高的
putchar(byte)
-隐含地GCC的最新版本通常会这样做,即使未启用优化也是如此-而
printf(\"%d\",byte)
实际上将被编译为对
printf()
的调用。     ,这当然是可能的-程序可以检查它是否正在写入终端,并写入与写入管道时不同的内容。     ,正如您所说,,30ѭ尚未初始化,因此可能会发生任何事情。 可能发生的事情之一是
byte
\“ starts \”的值为0,并使其在函数调用之间保持其值(就像它被声明为
static
)。 在二进制...    字节| c(bin)|字节| C ----------- + -------------- + --------------  00000000 |我(01101001)| 01101001(i)  01101001 | j(01101010)| 01101011(k)*很奇怪,您得到\'j \',但任何事情都可能发生:)  01101011 | k(01101011)| 01101011(k)  01101011 | l(01101100)| 01101111(o)  01101111 |米(01101101)| 01101111(o)     ,尼尔·巴特沃思(Neil Butterworth)说的话。该功能称为“ 33”。
if (isatty(STDOUT)) printf(\"I am printing to the terminal!\\n\");
另外,在测试内容时,您可能已经做过:
$ ./abc < infile > infile
意外地。因此,您可能需要快速检查一下,“ѭ36”确实包含相同的数据。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-