这是我的。我不明白为什么这个 getline() 返回 1 而不是返回它读取的任何字符。
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
FILE *fp;
if (argc!=3)
{
printf("improper number of input\n");
exit(1);
}
size_t len=0;
ssize_t read;
char *line=NULL;
fp=fopen(argv[1],"r");
if (fp==NULL)
{
printf("file is empty\n");
exit(EXIT_FAILURE);
}
while (read=getline(&line, &len, fp)!=-1)
{
printf("retrieved line length %zu \n",read );
//printf("%s\n",line );
}
fclose(fp);
free(line);
}
这是我的输出。
retrieved line length 1
我使用了这里提到的 getline() http://man7.org/linux/man-pages/man3/getline.3.html
我的文件包含以下文本:
The official and de facto national language of Bangladesh is Modern Standard Bengali (Literary Bengali).[6][7][8][9] It serves as the lingua franca of the nation, with 98% of Bangladeshis being fluent in Bengali (including dialects) as their first language.[10][11] Within India, Bengali is the official language of the states of West Bengal, Tripura and the Barak Valley in the state of Assam. It is also spoken in different parts of the Brahmaputra valley of Assam. It is also the most widely spoken language in the Andaman and Nicobar Islands in the Bay of Bengal,[12] and is spoken by significant minorities in other states including Jharkhand, Bihar, Mizoram, Meghalaya, and Odisha. With approximately 250–300 million total speakers worldwide,[13] Bengali is usually counted as the seventh most spoken native language in the world by population
最佳答案
由于 !=
的绑定(bind)比 =
更强,因此您需要使用括号:
while((read=getline(&line, &len, fp)) != -1)
您基本上将比较结果分配给 read
,如果计算结果为 true,则为 1
,否则为 0
。
关于c - 为什么 getline() 仅返回 1 而不是返回它读取的字符 no,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53584597/