我编写了一个函数,该函数应该读取 .txt 文件中的句子数,但是当调用并完成该函数时,它给我的值为 0。
这个程序总体上还有 3 个函数来计算文件的不同属性,我让它们工作得很好。这个函数的布局与我编写其他函数的方式相同,只是寻找一些关于为什么我的句子数量为 0 的建议。
void ptrCntS (FILE* sp1, int sCount)
{
char sentence = 'O';
int myChr;
if (!(sp1 = fopen("Hello.txt", "r")))
{
printf("error opening Hello.txt");
return(1);
}
while ((myChr = fgetc(sp1)) != EOF)
{
if ('.')
{
sentence ='O';
}
else if (sentence == 'O')
{
sCount++;
sentence = 'I';
}
}
fclose(sp1);
printf ("Total number of sentences are:\t%d", sCount);
return;
}
最佳答案
使用 return(sCount) 代替 return; 并将返回值分配给调用函数中的某个 int 变量 喜欢 int 发送计数; 。 。 。 sendCount=ptrCntS(param1,param2);
关于c - 为什么我的函数返回 0 而不是文件中的行数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33011309/