Como posso truncar perfeitamente uma string em c?
Estou lendo de um arquivo no qual cada linha tem mais de 63 caracteres e quero que os caracteres sejam truncados em 63. No entanto, ele falha ao truncar as linhas lidas no arquivo.
Neste programa, estamos assumindo que o arquivo tenha apenas 10 linhas:
#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]);
}
}