C语言读取文件时换行的问题

问题描述

我正在读取带有“fgetc(file)”的 .pgm 文件(如下面的几行)。当只获取带有循环的数字时,我避免了它们之间的空格(它似乎将每个空格,无论它有多大,都视为单个字符),它将行的最后一个数字和第一个从下面一行(例如,最后一个数字是 146,第一个数字是 105,它给出了 146105)。

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

简单地打印文件内容时,我在控制台的换行处得到了很多空格,如下所示:

113     116     97      124     146                                                                                                                                                                                                                                                                                                                                                      
105

打印内容时,每个字符在一行上,我得到另一个输出

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

关于我应该如何处理文件的这一部分的任何想法? 这是我的代码

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);
}

我认为通过这段代码,您可以得到一个想法。如果需要更多,说出来。如果您愿意,我还会在此处为您提供下载图像文件链接https://drive.google.com/file/d/1ZYIU6fcUOnhRND2zph5MUr8545P6U8bX/view?usp=sharing

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)