C语言文件处理问题
菜鸟求助!!!
现在需要将以下格式的txt文档读入二维数组:
Ada
Dan
David
Hume
a
abbreviate
able
abnormal
about
.
.
.
以便方便地访问使用每个单词。高手们给点建议啊!!!
[解决办法]
while(!feof(pFileIn))
{
fgets(buff, SIZE, pFileIn);
//other operations
}
[解决办法]
#define MAXLINES 100000 //最多行数
#define MAXCHARS 40 //每行最多字符数
char a[MAXLINES][MAXCHARS];
int i,n;
FILE *fp;
......
i=0;
while (1) {
if (NULL==fgets(a[i],MAXCHARS,fp)) break;
i++;
if (i>=MAXLINES) break;//忽略多余行
}
n=i;
......
[解决办法]
- C/C++ code
#include <stdio.h>#define MAX 100int main() { FILE *fp; char word[MAX][MAX]; int i; if((fp = fopen("data.txt", "rb")) == NULL) { perror("oper file"); return 1; } rewind(fp); for(i = 0; fgets(word[i], MAX, fp) != NULL; i++) printf("%s", word[i]); return 0;}