读取文件并获取字符串长度

发布于 2024-09-12 10:12:00 字数 1125 浏览 3 评论 0原文

gcc 4.4.4 c89

我正在使用以下代码使用 fgets 读取文件。我只想获取性别,可以是 M 或 F。

但是,因为性别始终是字符串中的最后一个字符。我以为我可以通过使用 strlen 来获取该字符。但是,由于某种原因,我必须获取 strlen 和负 2。我知道 strlen 不包括 nul。但是,它将包含回车符。

我正在阅读的确切文本行是这样的:

"Low, Lisa" 35 F

我的代码:

int read_char(FILE *fp)
{
#define STRING_SIZE 30
    char temp[STRING_SIZE] = {0};
    int len = 0;

    fgets(temp, STRING_SIZE, fp);

    if(temp == NULL) {
        fprintf(stderr, "Text file corrupted\n");
        return FALSE;
    }

    len = strlen(temp);
    return temp[len - 2];
}

当我觉得它应该返回 16 时,strlen 返回 17。字符串长度包括回车符。我觉得我应该做 - 1 而不是 - 2。

如果您理解我的问题,请提出任何建议。

谢谢,

编辑:

fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to  by  s.   Reading  stops
       after  an  EOF  or  a  newline.  If a newline is read, it is stored into the buffer.  A '\0' is stored after the last character in the
       buffer

所以缓冲区将包含:

"Low, Lisa" 35 F\0\r

如果包含 \r,它将从 strlen 返回 17?我的想法正确吗?

gcc 4.4.4 c89

I am using the following code to read in file using fgets. I just want to get the gender which could be either M or F.

However, as the gender is always the last character in the string. I thought I could get the character by using strlen. However, for some reason I have to get the strlen and minus 2. I know that the strlen doesn't include the nul. However, it will include the carriage return.

The exact line of text I am reading in is this:

"Low, Lisa" 35 F

My code:

int read_char(FILE *fp)
{
#define STRING_SIZE 30
    char temp[STRING_SIZE] = {0};
    int len = 0;

    fgets(temp, STRING_SIZE, fp);

    if(temp == NULL) {
        fprintf(stderr, "Text file corrupted\n");
        return FALSE;
    }

    len = strlen(temp);
    return temp[len - 2];
}

The strlen returns 17 when I feel it should return 16. String length including the carriage return. I feel I should be doing - 1 instead of - 2.

Any suggestions if you understand my question.

Thanks,

EDIT:

fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to  by  s.   Reading  stops
       after  an  EOF  or  a  newline.  If a newline is read, it is stored into the buffer.  A '\0' is stored after the last character in the
       buffer

So the buffer will contain:

"Low, Lisa" 35 F\0\r

Which will return 17 from strlen if it is including the \r? Am I correct in thinking that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

丶情人眼里出诗心の 2024-09-19 10:12:00

缓冲区将包含:

"Low, Lisa" 35 F\n\0

所以 -2 是正确的:strlen - 0 是空终止符,-1 是换行符,-2 是字母 F。

此外,

if(temp == NULL) {

temp 是一个数组 - 它永远不能为 NULL。

The buffer will contain:

"Low, Lisa" 35 F\n\0

so -2 is correct: strlen - 0 would be the null terminator, -1 the newline, and -2 is the letter F.

Also,

if(temp == NULL) {

temp is an array - it can never be NULL.

菩提树下叶撕阳。 2024-09-19 10:12:00

不是

if (temp == NULL) 

检查 fgets 的返回值,如果它为 null,则表示失败

if ( fgets(temp, STRING_SIZE, fp) == NULL )

yes, strlen 包括换行符

,请注意,如果您位于文件的最后一行,并且该行末尾没有 \n如果您假设字符串中始终存在 \n,则会遇到问题。

另一种方法是像您一样读取字符串,但检查最后一个字符,如果没有 \n 那么您不应该使用 -2 偏移量,而应使用 -1 。

Instead of

if (temp == NULL) 

check the return value from fgets instead, if its null then that would indicate failure

if ( fgets(temp, STRING_SIZE, fp) == NULL )

yes, strlen includes the newline

note that if you are on the last line of the file and there is no \n at the end of that line you make encounter a problem if you assume there is always \n in the string.

an alternative way would be to read the string as you do but check the last char, if there is no \n then you shouldn't use -2 offset but -1 instead.

你的背包 2024-09-19 10:12:00

这取决于用于保存文件的操作系统:

  • 对于 Windows,回车符是 \r\n
  • 对于 Linux,它们是 \n

It depends on the operating system used to save the files :

  • for Windows, carriage returns are \r\n
  • for Linux, they are \n
野侃 2024-09-19 10:12:00

你调试并找到 Len 到底发生了什么吗?如果您在 c 中执行此操作,请添加监视并找出在您的值 len 处显示的内容。

Did u debug and find what exactly coming at Len. If u are doing it in c add watch and find out the what is displaying at your value len.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文