Hey guys,
Can you take a look at my code and tell me what is wrong?
I am trying to use fgets to print the first n characters of each line of a file.
When this function gets called 3 strings are passed to it, the command (which is irrelevant to this example). The maximum length of each line and the file name to be opened for reading.
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int trunc(char *command, char *maxLen, char *fileName)
{
FILE *finp;
int i = atoi(maxLen);
char line[i];
if((finp = fopen(fileName, "r")) == NULL) {
printf("Error - Cannot open file: %s\n\n", fileName);
return 1;
}
while(fgets(line, i, finp) != NULL) {
printf("%s", line);
}
printf(" \n");
if(fclose(finp) == EOF) {
printf("Error in closing file\n");
}
return 0;
}
For ouput, I get every line of the text file completely and not n characters long as i would like(or i in this example)
Any ideas ?