简体   繁体   English

使用fscanf读取输入文件中线失败

[英]Using fscanf to read an input file fails mid-line

Long time, first time. 好久不见 I've done a ton of digging on this to no avail, so I'll get right to it... 我已经做了很多无济于事的挖掘工作,所以我会正确解决的...

I'm working in C for a physics research project. 我在C从事一个物理研究项目。 I am trying to read in a file of input values... each line contains 12 values of type double that are space-delimited, and the input file is 1006 lines long. 我正在尝试读取输入值文件...每行包含12个以空格分隔的double类型的值,并且输入文件长1006行。 Since the file input is well-known, I have elected to use fscanf() to ingest the input, perform an operation to reorder it, and then output it into another file that will be more suitable for gnuplot use. 由于文件输入是众所周知的,因此我选择使用fscanf()来摄取输入,执行操作以对其进行重新排序,然后将其输出到另一个更适合gnuplot使用的文件中。 Here is the line prior to the problem line and the problem line in question: 这是问题行和有问题的行之前的行:

2.250000000000000 0.500000000000000 2.668878914693362 0.081121085306632 2.668879345525446 0.081120608880718 0.081120609109235 2.668879508723290 -1.139145600698256 -0.478208465494011 -0.476544273039587 -0.184392862164658
2.250000000000000 0.550000000000000 2.723599351123168 0.076400593002322 2.723599435547186 0.076400582995125 0.076400821024960 2.723599264264542 -0.996035795911154 -0.408011755823990 -0.409827430433329 -0.178196609653836

All I am trying to do at the moment is read the file and output only what I am concerned with before adding more logic, but fscanf() is being problematic. 目前,我要做的只是读取文件并仅输出我所关心的内容,然后再添加更多逻辑,但是fscanf()出现了问题。 The relevant code in question follows (debug statements included): 有关的相关代码如下(包括调试语句):

int readEOF = 0;
double maxThreePhaseParticleCount = 0.0;
double particlesATotal = 0.0;
double particlesBTotal = 0.0;
double rhoA1 = 0;
double rhoA2 = 0;
double rhoA3 = 0;
double rhoB1 = 0;
double rhoB2 = 0;
double rhoB3 = 0;

FILE * two_phase_coords;
char two_phase_coords_name[255];
sprintf(two_phase_coords_name,"~/threePhaseDiagram-densities-twoPhases_tcA%f_tcB%f_aA%f_aAB%f_aB%f.dat", tcA, tcB, aA, aAB, aB);
two_phase_coords = fopen(two_phase_coords_name, "r");

readEOF = fscanf(two_phase_coords, "%lf %lf %lf %lf %lf %lf %lf %lf %*lf %*lf %*lf %*lf", &particlesATotal, &particlesBTotal, &rhoA1, &rhoB1, &rhoA2, &rhoB2, &rhoA3, &rhoB3);
while (readEOF != EOF) {
    readEOF = fscanf(two_phase_coords, "%lf %lf %lf %lf %lf %lf %lf %lf %*lf %*lf %*lf %*lf", &particlesATotal, &particlesBTotal, &rhoA1, &rhoB1, &rhoA2, &rhoB2, &rhoA3, &rhoB3);
    printf("just read %i...\n%.15f %.15f %.15f %.15f %.15f %.15f %.15f %.15f %.15f\n", readEOF, particlesATotal, particlesBTotal, maxThreePhaseParticleCount, rhoA1, rhoB1, rhoA2, rhoB2, rhoA3, rhoB3);
}

The 6th field of the problem line (line 988 out of 1006) is where things fall on its face... instead of reading out 0.076400582995125, the value 0.000000000000000 is read instead... readEOF returns 6 instead of the expected 8, and fscanf() fails/returns EOF on the next loop iteration. 问题行的第6个字段(1006中的第988行)是东西掉下来的地方...而不是读出0.076400582995125,而是读取值0.000000000000000 ... readEOF返回6,而不是预期的8,并且fscanf ()失败/在下一个循环迭代中返回EOF。

I'm baffled. 我很困惑。 Things I have tried... 我尝试过的东西

  1. Lots of Googling. 很多谷歌搜索。
  2. Changing from fscanf() to fgets()/sscanf()... failure occurs at exactly the same location, and the remaining 18 lines are still NOT processed. 从fscanf()更改为fgets()/ sscanf()...失败发生在完全相同的位置,其余18行仍未处理。
  3. Creating dummy input files of 1000+ carbon copies of both the line before the problem line and the problem line... processing of each file is complete and without error. 创建问题行之前和问题行之前的行的1000多个碳复本的虚拟输入文件...每个文件的处理已完成且没有错误。
  4. Examining the input file with a hex editor... everything before and after the problem line looks like standard/expected ASCII to me. 用十六进制编辑器检查输入文件...问题行之前和之后的所有内容在我看来都像标准/预期的ASCII。
  5. Reading all 12 values on each input line into separate variables (ie not using the ignore character in %*lf in fscanf()). 将每个输入行上的所有12个值读入单独的变量(即,不使用fscanf()中%* lf中的忽略字符)。
  6. Lots of Googling. 很多谷歌搜索。

I would appreciate any and all help... it's been a very long time since I've been a C guru. 我将不胜感激,因为我当了C语言大师已经很长时间了。 And since this is my very first SO post, apologies in advance if I've accidentally stomped on any community expectations/etiquette. 由于这是我的第一篇SO帖子,因此,如果我不小心踩踏了任何社区期望/礼节,请提前道歉。

Thank you for all of your help!! 谢谢您的帮助!!

So I'm not sure how often this happens, but naturally right after I post this question I manage to track down what was happening... the function in question is part of a larger environment, and the call to this function was happening before the file pointer to the input file was closed (undefined behavior). 因此,我不确定这种情况发生的频率,但是自然而然地,在我发布此问题之后,我便设法跟踪正在发生的事情...所讨论的功能是较大环境的一部分,并且对该功能的调用是在指向输入文件的文件指针已关闭(未定义的行为)。 I want to down vote myself. 我想自己投反对票。

Thank you for chiming in everyone! 谢谢大家的指教!

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

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