gpt4 book ai didi

检查文本文件中是否有新行

转载 作者:行者123 更新时间:2023-11-30 15:22:34 24 4
gpt4 key购买 nike

我正在尝试编写一个从文件读取并处理它读取的字符的代码。要点是它必须纠正它读取的文件中存在的大小写错误。

一个特殊的要求是我必须对每一行进行编号,因此我编写了一些代码来确定读取的每个字符是否是换行符。

int fix_caps(char* ch, int* char_in_word, int* line_num){

char a;
ch = &a;

if(a != '\n'){
return 0;
}else{
return 1;
}

if(a == ' ')
*char_in_word = 0;

if(*char_in_word == 1)
a = toupper(a);

if(*char_in_word > 1)
a = tolower(a);

char_in_word++;

}

然而,它所在的函数总是返回 0,而它应该在每行末尾返回 1。我做错了什么?

最佳答案

the execution will never get beyond this 'if control block:

char a;
ch = &a;

if(a != '\n'){
return 0;
}else{
return 1;
}

there is a few reasons it 'always' returns 0
1) 'a' is on the stack and could contain anything.
2) the chances of the 'trash' that is on the stack where 'a'
is located are 255:1 against the trash happening to contain
a new line character.

nothing beyond the 'if control block is ever executed because
an 'if' control block only has two execution paths
and both paths contain a return statement.

关于检查文本文件中是否有新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29189766/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com