简体   繁体   中英

fgets/scanf reading file gives no values

I have a txt file containing numerous lines of unsigned character triples. The data comes from an OpenCV BGR picture, so each byte triple is a BGR Colour value.

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. 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. 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:

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:

here is my text file: 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 ...

defined as follows: char *imageData;

You need to check the return values on those functions. See fgets and sscanf . 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...

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:

if FrontTexture->widthStep == 1 , and x == 0 and y == 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

[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. You should also test what fgets returns, and for extra safety that sscanf is returning 3 - the number of things it matched. The direct call to fscanf might be better, avoiding the fgets

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

as that will handle spaces better.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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