confused
confused

Reputation: 31

array size in fgets()

i m using fgets to read line form .txt file. i m passing an array as the first argument. different lines fill in different amount of space in the array, but i want to know the exact length of the line that is read and make decision based in that. is it possible?

FILE * old;
old = fopen("m2p1.txt","r");
char third[100];
fgets(third,sizeof(third),old);

now if i ask for sizeof(third), its obviously 100 because i declared so myself (i cant declare 'third' array without specifying the size) but i need to get the exact size of the line read from file(as it may not fill in the enitre array).

is it possible? what should do?

Upvotes: 0

Views: 5816

Answers (1)

nos
nos

Reputation: 229294

If fgets succeeds, it'll read a string into your buffer. use strlen() to find its length.

char third[100];

if(fgets(third,sizeof(third),old) != NULL) {
  size_t len = strlen(third);
  ..
} 

Upvotes: 7

Related Questions