首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >检查文本文件中的新行

检查文本文件中的新行
EN

Stack Overflow用户
提问于 2015-03-22 08:46:02
回答 1查看 26关注 0票数 0

我正在试着写一段代码,它能从文件中读取数据,并能处理所读取的字符。要点是它必须纠正它读取的文件中存在的大小写错误。

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

代码语言:javascript
复制
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++;

}

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

EN

回答 1

Stack Overflow用户

发布于 2015-03-22 08:54:10

代码语言:javascript
复制
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.
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29189766

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档