我想读取整个文本文件到C中的字符串数组这是我到目前为止有:如何在纯C中将文本文件读入数组?
#include <stdio.h>
#include <time.h>
int main(){
FILE *fp;
long lSize;
char *buffer;
size_t result;
int array_val;
char random_word[100];
//open fle
fp = fopen("dictionary.txt", "rb");
//get file size
fseek (fp, 0, SEEK_END);
lSize = ftell(fp);
rewind(fp);
//allocate RAM for file
buffer = (char *) malloc (sizeof(char)*lSize);
//put file in buffer
result = fread (buffer, 1, lSize, fp);
srand(time(NULL));
array_val = rand()%lSize + 1;
random_word = result[array_val];
return 0;
}
然而,当我建,它不工作(废话),我得到这些错误:
subscripted value is neither array nor vector: line 36
和:
warning: incompatible implicit declaration of built in function 'malloc'
谢谢大家!
请尝试评论以解释反对票,以便OP可以改善他们拥有的东西。 – 2013-11-15 18:21:39