0
我想知道是否可以在计算出如何从文件中读入文本到C中的数组中获得一些帮助。我的主要目标是创建一个文本格式化程序将正确的文字,但目前我只是想正确阅读的文字。如何将文本读取到C语言数组中
在此先感谢!
#include<stdlib.h>
#include<stdio.h>
#define TEMP 1000
int main() {
int words_in_line = 0;
int spaces_to_put = 0;
int z; //for loop
int i; //for loop
int count; //indicates when to put a new line.
int k; //for loop
int w; //for loop
int drop_spaces; //for loop
int check;
size_t nread;
FILE *f_open;
char buffer[TEMP];
f_open = fopen("hollow.txt", "r");
char *buf = malloc(TEMP);
printf("Enter the length of chars in a line: \n");
scanf("%d", &w);
if (buf == NULL) {
printf("Buf is null \n");
}
while ((nread = fread(buf, 1, TEMP, f_open)) > 0) { //going through the stream.
buffer[TEMP] = fwrite(buf, 1, nread, stdout); //need to load the steam into the array.
//size_t cannot match char[].
}
if (ferror(f_open)) {
printf("Error reading file \n");
}
return 0;
}
只需制作'mmap'文件,然后像使用巨大的字符串变量一样工作。顺便说一句,在你的情况下,你可以简单地用'getline'逐行读取 –
在'strcpy'或'memcpy'上进行阅读。 –
'fwrite()'返回写入的项目数量。 – BLUEPIXY