c - ingore C语言从文本文件中读取数据时回车(换行)
问题描述
我正在从文件“test1.txt”中读取一些数据,一切正常,但是当遇到 Enter(换行符)时,它给了我不想要的答案
代码和文件:
主.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);
测试1.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
在第一行最后一个“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
我怎样才能避免它?
解决方案
I was about the write the solution but kudos to Mr. Rankin, he explain well. It is regarding with the escape sequences. For example Enter is actually \n where all those required for computer to understand and display it for you in a Newline(\n) or Tab(\t). Check this for more information : Wikipedia - Table of Escapes
If there is anything require omitting, such as , you have to implement it lstm != ',' . This is sometimes used in readability 42,376.98(forty-two-thousand-three-hundred-seventy-six-point-nine-eight) for example
Final code will be
while ((i + 1 < MAXCN) && ((lstm = fgetc(fp)) != ' ') && (lstm != '\n') && (lstm != EOF))
Check David C. Rankin answer and also wikipedia link for escape sequences. More about :
Also, the Linux man pages online (man7.org) can provide the proper usage for any standard function you may encounter.
推荐阅读
- java - java日历中的未来日期给出了一个奇怪的行为
- javascript - 意外的 Chrome 自动填充行为 [禁用 chrome 自动填充]
- ios - 在表格上显示排序结果 - Swift4
- xml - XSLT 1.0 两个不同节点之间的所有节点
- jquery - 无法通过 AJAX 调用 WebAPI 方法,如果在 AJAX 调用中使用了调试器,则可以使用
- javascript - HTML5 Canvas - 当它超出我的矩形时隐藏文本部分
- python - 为什么 django 不使用我的自定义编码器类?
- python - 保持数据库外键一致
- python - Python搜索元组列表并根据索引返回多个条件
- php - 仅使用实时代理的代理轮换