开发者

array size in fgets()

开发者 https://www.devze.com 2023-02-10 17:53 出处:网络
i m using fgets to read line form .txt file. i m pass开发者_如何学运维ing an array as the first argument. different lines fill in different amount of space in the array, but i want to know the exact l

i m using fgets to read line form .txt file. i m pass开发者_如何学运维ing 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?


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);
  ..
} 
0

精彩评论

暂无评论...
验证码 换一张
取 消