嗨,大家還沒有找到如何搜索文本文件中的任何特定單詞,所以在這裏,這是我現在所擁有的,只是閱讀並打印整個文本文件。在文本文件中搜索特定單詞
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main (void)
{
static const char filename[] = "chat.log";
FILE *file = fopen (filename, "r");
if (file != NULL)
{
char line [ 128 ]; /* or other suitable maximum line size */
while (fgets (line, sizeof line, file) != NULL) /* read a line */
{
fputs (line, stdout); /* write the line */
}
fclose (file);
}
else
{
perror (filename); /* why didn't the file open? */
}
return 0;
}
謝謝:d
正確'fgets(行,sizeof行,文件)!= NULL','sizeof(行)' –