简体   繁体   English

如何在 c/c++ 中使用 gzgetc 检测换行符

[英]How to detect the newline character with gzgetc in c/c++

I want to read an entire line of a file character by character using gzgetc and stop when the newline is encountered.我想使用 gzgetc 逐个字符地读取文件的整行,并在遇到换行符时停止。 I know there is a function to grab the entire line but I would like to try to do it this way first.我知道有一个 function 可以抓住整条线,但我想先尝试这样做。 I tried:我试过了:

Int c;

do {
  c = gzgetc((gzFile) fp);
  cout << c;
} while (c != '\n');

The result was an infinite loop.结果是一个无限循环。 I tried adding (char) before c, still the same result.我尝试在 c 之前添加(char),结果仍然相同。 What am I doing wrong?我究竟做错了什么? The data file I am trying to read is encoded in base64 and I want to read in each token separated by space.我试图读取的数据文件以 base64 编码,我想读取以空格分隔的每个令牌。 Some of the lines are variable length and have a mixture of encoded and not encoded data which I set up an algorithm for I just need to know how to stop at newline.有些行是可变长度的,并且混合了编码和未编码的数据,我设置了一个算法,我只需要知道如何在换行处停止。

You need to also check for gzgetc() returning -1 , which indicates an error or end of file, and exiting the loop in that case.您还需要检查gzgetc()返回-1 ,这表示错误或文件结束,并在这种情况下退出循环。 Your infinite loop is likely due to one of those.您的无限循环可能是由于其中之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM