Answer on Question #44554, Programming, C
Problem.
How is it possible to create a table header that is filled in with a standard input from a text file using arrays?
Solution.
Code
#include <stdio.h>
int main()
{
char headers[20][40]; // array to store headers
int n = 0; // number of elements in headers
int i; // counter
// Read data
FILE *file = fopen("headers.txt", "r");
while(fscanf(file, "%s", headers[n]) != EOF)
{
n++;
}
fclose(file);
// Output results
for (i = 0; i < 13 * n + 1; i++) // top border
{
printf ("-");
}
printf("\n!");
for (i = 0; i < n; i++) // headers
{
printf ("% - 10s |", headers[i]);
}
printf("\n");
for (i = 0; i < 13 * n + 1; i++) // bottom border
{
printf ("-");
}
return 0;
}Result
http://www.AssignmentExpert.com/
Comments