【发布时间】:2015-03-01 18:17:02
【问题描述】:
我正在读取每行大于 63 个字符的文件,我希望将字符截断为 63。但是,它无法截断从文件中读取的行。
在这个程序中,我们假设文件有 10 行
目标:我想从每行读取 63 个字符。任何超过 63 个字符的行,读取 63 个字符并截断其余字符。如果有更简单的方法可以做到这一点,请告诉我。
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char a[10][63];
char line[255];
int count = 0;
//Open file
FILE *fp;
fp = fopen("lines.dat", "r");
//Read each line from file to the "line array"
while(fgets(line, 255,fp) != NULL)
{
line[63] = '\0';
//copy the lines into "a array" char by char
int x;
for(x = 0; x < 64; ++x)
{
a[count][x] = line[x];
}
count++;
}
fclose(fp);
//Print all lines that have been copied to the "a array"
int i;
for(i = 0; i < 10; i++)
{
printf("%s", a[i]);
}
}
【问题讨论】:
-
这不是和你之前的stackoverflow.com/questions/28793054/…一模一样的问题吗?
标签: c arrays string char truncate