0

I'm reading a .pgm file (like the lines below) with "fgetc(file)". When getting only the numbers with a loop in which I avoid the spaces in between (it seems it treats each space, no matter how big it is, as a single character), it joints the last number of a line and the first one from the following line (ex, 146 last number and 105 first number, it gives 146105).

113 116 97  124 146
105 100 112 98  88
100 117 98  87  126
131 101 87  137 161

When simply printing the contents of the file, I get a lot of spaces on the change of line in the console, like this:

113     116     97      124     146                                                                                                                                                                                                                                                                                                                                                      
105

When printing the contents, each character on a single line, i get this other output:

9
7
    //one line
1
2
4
    //one line
1
4
6
    //change of linne in the file, two lines here!!!!

1
0
5
    //one line
1
0
0

Any ideas about how I should treat this part of the file? Here is my code:

int get_number(int current[4], int n)
{
    int final_number, index = 0, max = n;
    while (index < max)
    {
        final_number += current[index] * pow(10, (n-1));
        index++;
        n --;
    }
    return final_number;
}

file = fopen("baboon.pgm", "r");
int info = fgetc(file), n;

while (info != EOF)
{
        n = 0;
        while (info != '    ' || info != '  ' || info != '  ' || info != '  ')
        {
                printf("%c", info);
                int current_number[4];
                n++;
        }
        if (current_number[0] != 0)
        {
                printf("%d ", get_number(current_number, n));
        }
        info = fgetc(file);
}

I think that with this piece of the code you can get an idea. If need more, say it. I also leave you here a link to download the image file if you want: https://drive.google.com/file/d/1ZYIU6fcUOnhRND2zph5MUr8545P6U8bX/view?usp=sharing

DoZmeR
  • 11
  • 1
  • 3
    Show the code as a [complete minimal verifiable example](https://stackoverflow.com/help/minimal-reproducible-example). We can't help find problems in something we can't see. – kaylum Jan 04 '21 at 22:02
  • 2
    When you go to the doctor, don't just say "ouch!" – Jens Jan 04 '21 at 22:09
  • `info != ' ' || info != ' ' || info != ' ' || info != ' ')` is certainly wrong. If `info` is a space character, then it's not a tab and the condition is true. If `info` is a tab, then it's not a space and the condition is true. IOw `while(info != ' ' || info != ' ' || info != ' ' || info != ' ')` is equivalent to `while(1)` – William Pursell Jan 04 '21 at 22:46

0 Answers0