[英]getline() reading garbage value while reading from a text file
在下面的代碼中,我想讀取第一行為<p> -> <many_declaration> <many_expression>
的文本文件。 代碼片段如下:
ssize_t read;
size_t len = 200;
FILE *fptr;
fptr = fopen(fn ,"r");
if(fptr==NULL){
printf("Error\n");
fclose(fptr);
}
else{
int line_number=1;
char * line = (char *)malloc(sizeof(char)*200);
while((read = getline(&line, &len, fptr)) != -1)
{
char * tokens = strtok(line, " \t\n");
while( tokens != NULL )
{
printf("%s \n",tokens);
printf("%zu \n",strlen(tokens));
tokens = strtok(NULL, " \t\n");
}
}
}
但是,在使用getline()
讀取它然后使用strtok()
拆分字符串時,第一個標記應該是大小為 3 的<p>
但正在讀取的字符串是一些不可見字符,后跟<p>
並且長度為 6 . 你能告訴我問題是什么嗎? 謝謝!
該文件可能以 UTF8 BOM 開頭。 如果是這樣,您應該將文件讀取為 UTF-8。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.