Reputation: 1
int i=0;
int numProgs=0;
char* input[500];
char line[500];
FILE *file;
file = fopen(argv[1], "r");
while(fgets(line, sizeof line, file)!=NULL) {
/*check to be sure reading correctly
add each filename into array of input*/
input[i] = (char*)malloc(strlen(line)+1);
strcpy(input[i],line);
printf("%s",input[i]);/*it gives the hole line with\n*/
i++;
/*count number of input in file*/
numProgs++;
}
/*check to be sure going into array correctly*/
fclose(file);
I don't know the size of the input txt for each line so i need to do it dynamically. I need to use char* input[] this type of array and also i need to hold line numbers with int numProgs. Input text file has multiple lines and each line has unknown size of character.
Upvotes: 0
Views: 2526
Reputation: 65
FILE *file;
file = fopen("test.txt", "r");
fseek(file, 0, SEEK_SET);
int numRow = 1, c;
while ((c = fgetc(file)) != EOF)
{
if (c == '\n')
numRow++;
}
fseek(file, 0, SEEK_SET);
char **input;
input = malloc(sizeof(char) * numRow);
int i, j = 0, numCol = 1, curPos;
for (i = 0; i < numRow; i++)
{
curPos = (int)ftell(file);
while((c = fgetc(file)) != '\n')
{
numCol++;
}
input[i] = malloc(sizeof(char) * numCol);
fseek(file, curPos, SEEK_SET);
while((c = fgetc(file)) != '\n')
{
input[i][j++] = c;
}
input[i][j + 1] = '\n';
numCol = 1;
j = 0;
}
Upvotes: 0