strrchr函数查找换行符一直返回NULL,但内存中是有换行符的

神-气 2012-10-23 08:45:24

if(fgets(line,max_line_len,input) == NULL)
return NULL;

while(strrchr(line,'\n') == NULL)
{
max_line_len *= 2;
line = (char *) realloc(line,max_line_len);
len = (int) strlen(line);
if(fgets(line+len,max_line_len-len,input) == NULL)
break;
}


input是打开一个文件,里面有很多行数据。
我发现这个strrchr始终找不到换行符,我查看了内存和watch窗口,很明显line中是存在换行符的:
line[4900]=10
max_line_len=8192
但是仍然在循环中转。
...全文
417 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2012-10-24
  • 打赏
  • 举报
回复
wchar_t *fgetws( wchar_t *string, int n, FILE *stream );

mujiok2003 2012-10-24
  • 打赏
  • 举报
回复
That makes sense:-)

[Quote=引用 12 楼 的回复:]
引用 9 楼 的回复:

你确定?

C/C++ code
char *fgets( char *str, int count, FILE *stream );

Reads at most count - 1 characters from the given file stream and stores them in str. The produced character st……
[/Quote]
I_ask_who 2012-10-24
  • 打赏
  • 举报
回复
wcsrchr,_getws可能更适用
神-气 2012-10-24
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 的回复:]

你确定?

C/C++ code
char *fgets( char *str, int count, FILE *stream );

Reads at most count - 1 characters from the given file stream and stores them in str. The produced character string is always ……
[/Quote]

我读的是unicode字符,确实很多0.
神-气 2012-10-24
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]

我用codeblock测试了这个程序,正常情况下是对的,txt文本为unicode big endian时内存溢出
[/Quote]

嗯,确实,我换成ANSI的好了。
神-气 2012-10-24
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 的回复:]

wchar_t *fgetws( wchar_t *string, int n, FILE *stream );
[/Quote]

嗯,Thx .
神-气 2012-10-24
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 的回复:]

wcsrchr,_getws可能更适用
[/Quote]

各个函数都有不同方面的应用吧,我把文件转成ANSI的了。
I_ask_who 2012-10-23
  • 打赏
  • 举报
回复
我用codeblock测试了这个程序,正常情况下是对的,txt文本为unicode big endian时内存溢出
mujiok2003 2012-10-23
  • 打赏
  • 举报
回复
你确定?

char *fgets( char *str, int count, FILE *stream );

Reads at most count - 1 characters from the given file stream and stores them in str. The produced character string is always NULL-terminated. Parsing stops if end-of-file occurs or a newline character is found, in which case str will contain that newline character.
可见str只有最后一个字符是'\0'

[Quote=引用 6 楼 的回复:]
我找到原因了,结贴。

原因是,在读取文件之后,fgets(line,max_line_len,input),使得line这块内存中有很多\0,而strrchr(line,'\n')这个函数应该是靠\0判断字符串是否结尾,然后再往左查找'\n'位置,因此strrchr总是找不到换行符,因为提前终止了。
[/Quote]
I_ask_who 2012-10-23
  • 打赏
  • 举报
回复
是不是编码不同,文件换成ANSI看看
swordtan 2012-10-23
  • 打赏
  • 举报
回复
二进制读取文件,在做字符处理时,需要注意在想不到的地方出现'\0'的问题
神-气 2012-10-23
  • 打赏
  • 举报
回复
我找到原因了,结贴。

原因是,在读取文件之后,fgets(line,max_line_len,input),使得line这块内存中有很多\0,而strrchr(line,'\n')这个函数应该是靠\0判断字符串是否结尾,然后再往左查找'\n'位置,因此strrchr总是找不到换行符,因为提前终止了。
神-气 2012-10-23
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

是不是数组line大小不够了,把换行挤出去了呀
[/Quote]

line是动态内存,如果找不到换行就会再扩大一倍。但现在是一直找不到,结果到最后只能无法内存申请那么大的空间而挂掉了。
snrrk1184 2012-10-23
  • 打赏
  • 举报
回复
是不是数组line大小不够了,把换行挤出去了呀
神-气 2012-10-23
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

你是文本打开还是二进制打开
[/Quote]

文本打开
慧钦 2012-10-23
  • 打赏
  • 举报
回复
你是文本打开还是二进制打开
snrrk1184 2012-10-23
  • 打赏
  • 举报
回复
有可能换行符的表示不同吧,可能是'\r\t'

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧