I'm attempting to set each line of a file, into an char *arr[]
.
For Example, I have an file set like this
arq.txt:
linha1
linha2
linha3
linha4
linha5
linha6
linha7
linha8
linha9
linha10
I want to store linha1
into the first position from my string array, so arr[0] = "linha1"
What I have coded to attempt to obtain this result was:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(){
FILE *in_file = fopen("arq.txt", "r");
char singleLine[150];
char* test[30];
int i = 0;
if (!in_file)
{
printf("Error! Could not open file\n");
return 0;
}
while (!feof(in_file)){
fgets(singleLine, 150, in_file);
if (i == 0){
test[0] = singleLine;
puts(singleLine);
}
i++;
}
size_t length = sizeof(test)/sizeof(test[0]);
printf("Size: %d", length);
printf("Here: %s\n", test[0]);
fclose(in_file);
return 0;
}
Althought, the result from that is printing the wrong data value:
(puts): linha1
Size: 30
Here: linha10
I attempted to get the size of the array from the content inside of it, so in this case, having only one element, I should have gotten 1 instead of 30.