如何解决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 举报,一经查实,本站将立刻删除。