【问题标题】:ingore Enter(line break) when read data from text file in C language从 C 语言文本文件中读取数据时忽略 Enter(换行符)
【发布时间】:2020-12-22 03:28:21
【问题描述】:

我正在从文件“test1.txt”中读取一些数据,一切正常,但是当遇到 Enter(换行符)时,它给出了我不想要的答案

代码和文件:

ma​​in.c

#include "fscan.h"
#include <stdio.h>
 
int main(){
    float lstm_val;
    int row;
    int col;
    float dense[2][4];
    FILE *fp = fopen("test1.txt","r");
    for (row = 0; row < 2; row++){
        for (col = 0; col < 4; col++){
            lstm_val = fscan(fp);
            dense[row][col] = lstm_val;
        }
    }

    return 0;
}

fscan.c

#include <stdlib.h>
#include <stdio.h>
#define MAXCN 50

float fscan(FILE *fp)
{   //FILE* lstm_txt = NULL;
    char lstm_weight[MAXCN] = {0};
    int lstm = 0;
    int i = 0;
    float lstm_val;
    
    while ((i + 1 < MAXCN) && ((lstm = fgetc(fp)) != ' ')  && (lstm != EOF)){
        lstm_weight[i++] = lstm;
    }
    printf("\n lstm_weight: %s\n\n", lstm_weight);
    lstm_val = atof(lstm_weight);
    printf("\n convert \"lstm_weight\" to lstm_val is : %f\n\n", lstm_val);
    return lstm_val;
 }

fscan.h

#include <stdio.h>

extern float fscan(FILE *fp);

test1.txt

4.217959344387054443e-01 -2.566376626491546631e-01 2.173236161470413208e-01 4.217959344387054443e-01
2.173236161470413208e-01 4.217959344387054443e-01 4.217959344387054443e-01 -2.566376626491546631e-01 

enter image description here 第一行最后一个“4.217959344387054443e-01”和第二行第一个“2.173236161470413208e-01”之间是回车,显然遇到这个回车结果是错误的

结果是:

 lstm_weight: 4.217959344387054443e-01


 convert "lstm_weight" to lstm_val is : 0.421796


 lstm_weight: -2.566376626491546631e-01


 convert "lstm_weight" to lstm_val is : -0.256638


 lstm_weight: 2.173236161470413208e-01


 convert "lstm_weight" to lstm_val is : 0.217324


 lstm_weight: 4.217959344387054443e-01
2.173236161470413208e-01


 convert "lstm_weight" to lstm_val is : 0.421796


 lstm_weight:


 convert "lstm_weight" to lstm_val is : 0.000000


 lstm_weight: 4.217959344387054443e-01


 convert "lstm_weight" to lstm_val is : 0.421796


 lstm_weight: 4.217959344387054443e-01


 convert "lstm_weight" to lstm_val is : 0.421796


 lstm_weight: -2.566376626491546631e-01


 convert "lstm_weight" to lstm_val is : -0.256638

如何避免?

【问题讨论】:

  • while ((i + 1 &lt; MAXCN) &amp;&amp; ((lstm = fgetc(fp)) != ' ') &amp;&amp; lstm != '\n' &amp;&amp; (lstm != EOF))??只需添加支票lstm != '\n'
  • 注意,在实际代码中还应避免使用atof()atoi()等。提供对转换的零错误检查,并且很乐意接受 atof ("my cow"); 返回 0 而没有任何错误指示。而是使用strtof()(或至少sscanf()——这也将消除您检查lstm != '\n'的需要)

标签: c enter


【解决方案1】:

我是关于编写解决方案的,但对 Rankin 先生表示敬意,他解释得很好。它与转义序列有关。例如,Enter 实际上是 \n,其中所有需要计算机理解并在换行符(\n)或制表符(\t)中为您显示它的地方。 检查此以获取更多信息:Wikipedia - Table of Escapes

如果有什么需要省略的,比如 ,你必须实现它 lstm != ',' 。 这有时用于可读性 42,376.98(四万二千三百七十六点九十八)例如

最终代码将是

while ((i + 1 < MAXCN) && ((lstm = fgetc(fp)) != ' ') && (lstm != '\n') && (lstm != EOF))

查看 David C. Rankin 的答案以及转义序列的维基百科链接。 更多关于:

此外,Linux man pages online (man7.org) 可以为您可能遇到的任何标准功能提供正确的用法。

【讨论】:

  • 感谢您的署名,做得很好。 Wikipedia 和 cplusplus.com 是合理的外部参考,但要小心使用其他参考(很多都是完全错误的)这次我会为你重新格式化你的链接:)
  • 尊敬的 Rankin 先生,感谢您的编辑和评论。它看起来更好的格式。我也很抱歉重新发布答案。您首先找到了解决方案。我只是想向 OP 说明 '\n(Newline Escape Sequence)' 的原因。
  • 非常高兴有您帮助发布它。欢迎使用 StackOverflow。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-29
  • 2011-12-08
  • 1970-01-01
  • 2014-02-23
  • 1970-01-01
  • 1970-01-01
  • 2018-03-29
相关资源
最近更新 更多