简体   繁体   English

fgets / scanf读取文件没有给出值

[英]fgets/scanf reading file gives no values

I have a txt file containing numerous lines of unsigned character triples. 我有一个包含多行无符号字符三元组的txt文件。 The data comes from an OpenCV BGR picture, so each byte triple is a BGR Colour value. 数据来自OpenCV BGR图片,因此每个字节三元组是BGR Color值。

When I try to read the file, the lines I'm reading in with fgets() are empty after maybe a third of the picture file. 当我尝试读取文件时,我用fgets()读取的行在可能是图片文件的三分之一后是空的。 Heres my code: 继承我的代码:

    FILE* DS;
    DS = fopen("Data.txt", "r");
    char line[100];
    for (int x=0; x<image->width; x++)
    {
        for (int y=0; y<image->height; y++)
        {
            fgets(line, 10, DS);
            sscanf(line, "%c %c %c", &FrontTexture->imageData[FrontTexture->widthStep * y + x * 3 + 0], 
                                     &FrontTexture->imageData[FrontTexture->widthStep * y + x * 3 + 1],
                                     &FrontTexture->imageData[FrontTexture->widthStep * y + x * 3 + 2]);
        }
    }
    fclose(DS);

I'm sure that the lines are filled with three characters because I went into the file and looked at line x*y. 我确定这些行填充了三个字符,因为我进入文件并查看了行x * y。 Nevertheless I get only one blank character in my line after a third of the file. 然而,在文件的三分之一后,我的行中只有一个空白字符。

Hope thats clear enough. 希望这足够清楚。 Thanks in advance. 提前致谢。

EDIT: 编辑:

a part of the txt file: txt文件的一部分:

Z a `
Y ^ a
Z ` a
Y ^ a
Y _ `
Z ` a
Y a `
Z b a
V c a
X b a
V c a
V c a
V c a
V c a
T c a
T c a
S c a
S c a
R b `
R b `
U b `
W a `
W a `
Y a `
Z b a
[ b a
Z b a
[ c b
Y c b
Y c b

This was written to the file by: 这是通过以下方式写入文件的:

for (int x=0; x<image->width; x++)
    {
        for (int y=0; y<image->height; y++)
        {
            fprintf(DS, "%c %c %c\n",   FrontTexture->imageData[FrontTexture->widthStep * y + x * 3 + 0], 
                                        FrontTexture->imageData[FrontTexture->widthStep * y + x * 3 + 1], 
                                        FrontTexture->imageData[FrontTexture->widthStep * y + x * 3 + 2]);
        }
    }

EDIT2: EDIT2:

here is my text file: http://www.2shared.com/document/SmLbhYzH/Datensatz.html size: 6,15mb 这是我的文本文件: http ://www.2shared.com/document/SmLbhYzH/Datensatz.html size:6,15mb

EDIT3 the image data of my OpenCV image is just a character array that should be filled with b0 g0 r0 b1 g1 r1 ... EDIT3我的OpenCV图像的图像数据只是一个字符数组,应填充b0 g0 r0 b1 g1 r1 ...

defined as follows: char *imageData; 定义如下:char * imageData;

You need to check the return values on those functions. 您需要检查这些函数的返回值。 See fgets and sscanf . fgetssscanf Those return values are important and will tell you where things are going amiss. 这些返回值很重要,并会告诉您事情的进展。

Using a code base very similar to yours I'm able to read the whole file: 使用与您的代码库非常相似的代码库,我能够读取整个文件:

unsigned char a, b, c;

DS = fopen("/home/mike/win_share/Datensatz.txt", "r");
char line[100];
while(fgets(line, 10, DS) != NULL)
{
    sscanf(line, "%c %c %c", &a, &b, &c);
    printf("%c (%d) %c (%d) %c (%d)\n", a, a, b, b, c, c);
}

I'm seeing you say this I get only one blank character in my line after a third of the file and then I'm wondering... 我看到你说这个I get only one blank character in my line after a third of the file ,然后我想知道......

Are you verifying that the characters are read correctly by looking at the file? 您是否通过查看文件验证字符是否正确读取? You know you have non-displayable characters in there correct? 你知道你有不可显示的字符吗?

x (120) o (111) m (109)
{ (123) t (116) s (115)  <-- I'm guessing this is the last line that looks OK
  (127) u (117) w (119)  <-- (127) DEL char won't show
� (129) z (122) | (124)
� (131)   (127) � (128)

Second thought... is your array index access correct? 第二个想法......你的数组索引访问是否正确? I'm not sure what widthStep is set to, but it could cause problems: 我不确定widthStep的设置是什么,但它可能会导致问题:

if FrontTexture->widthStep == 1 , and x == 0 and y == 0 如果FrontTexture->widthStep == 1 ,并且x == 0y == 0

[1 * 0 + 0 * 3 + 0] => [0 + 0 + 0] => [0]
[1 * 0 + 0 * 3 + 1] => [0 + 0 + 1] => [1]
[1 * 0 + 0 * 3 + 2] => [0 + 0 + 2] => [2]

Then on the next iteration: if FrontTexture->widthStep == 1 , and x == 0 and y == 1 然后在下一次迭代中:如果FrontTexture->widthStep == 1 ,并且x == 0y == 1

[1 * 1 + 0 * 3 + 0] => [1 + 0 + 0] => [1]  // Overwrite the data in imageData[1]
[1 * 1 + 0 * 3 + 1] => [1 + 0 + 1] => [2]  // Overwrite the data in imageData[2]
[1 * 1 + 0 * 3 + 2] => [1 + 0 + 2] => [3]

Have you tried printing out a few steps to verify everything's working as you expect? 您是否尝试过打印几个步骤来验证所有内容是否按预期工作?

Use: 使用:

sscanf(line, " %c %c %c", ...

Note the space at the beginning of the string. 注意字符串开头的空格。 That will avoid reading a blank as a valid first character. 这样可以避免将空白作为有效的第一个字符读取。

We need to see a sample of the input file really. 我们需要真正看到输入文件的示例。 However there are a number of issues. 但是有很多问题。

fgets will read at most 10 characters, if a line is 11 characters then the first call will read 10 of them, and the second the remaining one. fgets最多可读取10个字符,如果一行是11个字符,则第一个调用将读取其中的10个,第二个调用将读取剩余的一个。 You should also test what fgets returns, and for extra safety that sscanf is returning 3 - the number of things it matched. 您还应该测试fgets返回的内容,以及sscanf返回的额外安全性3 - 它匹配的内容数量。 The direct call to fscanf might be better, avoiding the fgets 直接调用fscanf可能会更好,避免fgets

fscanf(DS, "%c %c %d", ...

as that will handle spaces better. 因为这会更好地处理空间。

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

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