我不确定如何读取文件的所有行,atm它只读取文本文件中代码的第一行。有人可以告诉我如何让它读完所有的行吗?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv)
{
FILE *fp;
fp = fopen("specification.txt", "r");
char ** listofdetails;
listofdetails = malloc(sizeof(char*)*6);
listofdetails[0] = malloc(sizeof(char)*100);
fgets(listofdetails[0], 100, fp);
/*strcpy(listofdetails[0], "cars");*/
printf("%s \n", listofdetails[0]);
free(listofdetails[0]);
free(listofdetails);
fclose(fp);
return 0;
}
我的文字文件:
10X16 de4 dw9 ds8 g8,7 m3,4 h6,5 p2,2
10X16 de4 dw9 ds8 g8,7 m3,4 h6,5 p2,2
10X16 de4 dw9 ds8 g8,7 m3,4 h6,5 p2,2
答案 0 :(得分:1)
#include <stdio.h>
#include <assert.h>
int main(int argc, const char * argv[])
{
{
FILE *file = fopen("specification.txt", "r");
char currentline[100];
assert(file != NULL);
while (fgets(currentline, sizeof(currentline), file) != NULL) {
fprintf(stderr, "got line: %s\n", currentline);
/* Do something with `currentline` */
}
(void)fclose(file);
}
}
答案 1 :(得分:0)
使用getline(3),假设您的操作系统&amp; libc符合Posix2008标准(例如在Linux上):
FILE *fp = fopen("specification.txt", "r");
if (!fp) { perror("specification.txt"); exit(EXIT_FAILURE); };
size_t sizelist = 6; // initial guess of number of lines
size_t nblines = 0;
char ** listofdetails = calloc(sizelist, sizeof(char*));
if (!listofdetails) { perror("initial calloc"); exit(EXIT_FAILURE); };
char*curline = NULL;
size_t cursize = 0;
do {
ssize_t curlen = getline(&curline, &cursize, fp);
if (curlen < 0) break;
if (nblines >= sizelist) {
size_t newsizelist = 3*sizelist/2+5;
char**newlist = calloc(newsizelist, sizeof(char*));
if (!newlist) { perror("growing calloc"); exit(EXIT_FAILURE); };
memcpy (newlist, sizelist, nbline*sizeof(char*));
sizelist = newsizelist;
};
if (!(sizelist[nblines++] = strdup(curline)))
{ perror("strdup"); exit(EXIT_FAILURE); };
} while (!feof(fp));
上述代码可以接受系统资源允许的尽可能多的行和大行。在我强大的笔记本电脑(16Gbytes RAM)上,我可能能够读取超过一百万行的文件,每行近千个字符(或一行数百万字符的文件)。
最后(你的程序)你应该更好地释放记忆:
for (size_t ix=0; ix<nblines; ix++) {
free(sizelist[ix]), sizelist[ix] = NULL;
}
free(sizelist), sizelist = NULL;
free(curline), curline = NULL; cursize = 0;
答案 2 :(得分:0)
如果您想阅读&#39; specification.txt&#39;文本文件逐行,您可以这样做:
char row[255];
FILE *fp;
fp = fopen( "specification.txt", "r" );
if ( fp == NULL ) {
// error handling..
}
while ( fgets( row, sizeof( row ), fp ) != NULL ) {
puts( row );
}
fclose( fp );
确保您的排&#39;缓冲区足够大。
答案 3 :(得分:0)
另一个示例:如果您正在定位POSIX平台,则可以使用getline
。它分配存储线所需的空间,但你必须自己释放它。如果出现错误或EOF,则getline返回-1。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv)
{
FILE *fp;
fp = fopen("specification.txt", "r");
char** listofdetails;
int ROWS = 6;
listofdetails = calloc(ROWS,sizeof(char*));
int i;
size_t len;
ssize_t readed;
for (i = 0; i < ROWS; i++) {
if ((readed = getline(&listofdetails[i], &len, fp)) == -1) {
break;
}
}
for (i = 0; i < ROWS; i++) {
if (listofdetails[i] == NULL) {
break;
}
printf("%s\n",listofdetails[i]);
free(listofdetails[i]);
}
fclose(fp);
free(listofdetails);
return 0;
}
答案 4 :(得分:-1)
您可以通过以下方式阅读整个文件:
char *buffer;
FILE *fp = fopen("filename.txt", "rb");
if (fp != NULL)
{
fseek(fp, 0L, SEEK_END);
long stell = ftell(fp);
rewind(fp);
buffer = (char *)malloc(stell);
if (buffer != NULL)
{
fread(buffer, stell, 1, fp);
fwrite(buffer, stell, 1, stdout);
fclose(fp);
fp = NULL;
free(buffer);
}
}
以上代码会读取filename.txt
文件并将其打印到stdout
。