gpt4 book ai didi

c - 读取文件,然后将其排序为 10 列

转载 作者:太空宇宙 更新时间:2023-11-04 04:13:42 24 4
gpt4 key购买 nike

我想对一个包含 1199 个数字的 txt 文件进行排序,每行 10 列,我知道最后一行将有 10 或更少的列。但是,第一行有 11 列。

感谢您的帮助。

#include <stdio.h>

#define t 1024

int main()
{
int i=0, c;
char p[t];
FILE *f;
f = fopen("CMB.txt", "r");
while ((c = getc(f)) != EOF)
{
fscanf(f, "%s", p);
if(i%10 == 0 && i > 0)
{
printf("\n");
}
printf("%s ", p);
if (c == '\n')
{
i++;
}
}
printf("\n %d", i+1);
fclose(f);
}

最佳答案

当我尝试你的代码时,它正确地打印了你期望的 10 列,也许你在修改后没有重新编译。这是你的代码,我只删除了文件读取和打印字符。

#include <stdio.h>

int main()
{
int i=0;
//, c;
// char p[t];
// FILE *f;
// f = fopen("CMB.txt", "r");
while (i<30)
{
// fscanf(f, "%s", p);
if(i%10 == 0 && i > 0)
{
printf("\n");
}
printf("%d ", i);
// if (c == '\n')
// {
i++;
// }
}
// printf("\n %d", i+1);
// fclose(f);
}

关于c - 读取文件,然后将其排序为 10 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54296056/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com