I'm reading a file called data.txt
with looks like the following:
b 5
b 2
b 9
Each line has 3 characters: the space between being a tab.
I have the following code to read one line at a time.
int main(int argc, char * argv[]){
FILE * filePointer;
filePointer = fopen("data.txt", "r");
char singleLine[3*sizeof(char)];
while(!feof(filePointer)){
fgets(singleLine, 3*sizeof(char), filePointer);
//printCharArray(singleLine);
puts(singleLine);
}
fclose(filePointer);
return 0;
}
singleLine is basically the buffer that the text of each line is put into. I originally made it like char singleLine[20]
, an arbitrary big enough number, but I want it to be exact so I did char singleLine(3*sizeof(char))
. My logic was each line has 3 characters, so this should work.
Sadly it did not, when running it, it printed out like the following:
b
5
b
2
b
9
When I do the careless way, char singleLine[20]
, it works correctly as shown below. But I want to do it the correct way. What is wrong?
b 5
b 2
b 9