在 C 中使用 fgets 读取文件
Using fgets to read through file in C
我正在尝试通读给定的文件然后将其标记化。我遇到的唯一问题是 fgets.The 文件打开没有收到任何错误。我在网站的其他地方看到过这个,但是无论我如何设置它,包括将 fileLine 设置为固定数量,如 (char fileline [200]) 我都会遇到分段错误。在此先感谢您的帮助。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char *argv[]){
char *fileName = "0";
char *tokenize, *savePtr;
struct Record *database= malloc(sizeof(database[0]));
int recordNum =0;
char *fileLine = malloc(sizeof(char *));//have replaced with fileline[200] still didnt work
FILE *fd = open(fileName,O_RDWR);
if(fd< 0){
perror("ERROR OPENING FILE");
}
while(fgets(fileLine,200,fd) !=NULL){
printf("%s\n", fileLine);
tokenize = strtok_r(fileLine,",",&savePtr);
while(tokenize != NULL){
//TOKENIZING into a struct
}
}
这一行
char *fileLine = malloc(sizeof(char *));
为 char *
类型分配内存,4 或 8 个字节(取决于平台)。
所以当你这样做时
fgets(fileLine,200,fd)
它预计有 200 字节的可用内存。
试试这个:
char *fileLine = malloc(200);
if (fileLine == NULL) { ... } // check for error
这将分配所需的内存。
您正在使用 open()
而不是 fopen()
。
您不能确定文件是否正确打开,因为 fopen()
不是 return 整数,而是指向 FILE *
对象的指针,失败时 returns NULL
,所以正确的条件是
FILE *file;
file = fopen(filename, "r");
if (file == NULL)
{
perror("fopen()");
return -1;
}
在您的代码中,即使 fopen()
失败,您仍然会继续使用 fgets()
,在这种情况下您应该中止程序。
此外,malloc()
将字节数作为大小参数,因此如果您希望 fgets()
被限制为仅读取 count
字节,则 malloc()
应该是
char *buffer;
size_t count;
count = 200; /* or a value obtained someway */
buffer = malloc(count);
if (buffer == NULL)
{
fclose(file);
perror("malloc()");
return -1;
}
如果启用编译警告,编译器会指出代码中的所有问题。
为什么要改用 open() with FILE
? Use fopen()。
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]) {
char *fileName = "test.txt";
char *tokenize, *savePtr;
char fileLine[200] = {0}; // init this to be NULL terminated
FILE *fd = fopen(fileName, "r");
if (fd == 0) { // error check, equal to 0 as iharob said, not less than 0
perror("ERROR OPENING FILE");
return -1;
}
while (fgets(fileLine, 200, fd) != NULL) {
printf("%s\n", fileLine);
tokenize = strtok_r(fileLine, ",", &savePtr);
while (tokenize != NULL) {
tokenize = strtok_r(NULL, ",", &savePtr); // do not forget to pass NULL
//TOKENIZING into a struct
}
}
return 0;
}
正如风向标所说,如果您使用 open()
,fd < 0
会起作用。但是,对于 fopen()
,您应该检查指针是否为 NULL
,等同于 fd == 0
.
可以在以下位置找到打开文件的函数之间的比较:
- open and fopen function
- C fopen vs open
我的理解是fopen()
级别更高
我正在尝试通读给定的文件然后将其标记化。我遇到的唯一问题是 fgets.The 文件打开没有收到任何错误。我在网站的其他地方看到过这个,但是无论我如何设置它,包括将 fileLine 设置为固定数量,如 (char fileline [200]) 我都会遇到分段错误。在此先感谢您的帮助。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char *argv[]){
char *fileName = "0";
char *tokenize, *savePtr;
struct Record *database= malloc(sizeof(database[0]));
int recordNum =0;
char *fileLine = malloc(sizeof(char *));//have replaced with fileline[200] still didnt work
FILE *fd = open(fileName,O_RDWR);
if(fd< 0){
perror("ERROR OPENING FILE");
}
while(fgets(fileLine,200,fd) !=NULL){
printf("%s\n", fileLine);
tokenize = strtok_r(fileLine,",",&savePtr);
while(tokenize != NULL){
//TOKENIZING into a struct
}
}
这一行
char *fileLine = malloc(sizeof(char *));
为 char *
类型分配内存,4 或 8 个字节(取决于平台)。
所以当你这样做时
fgets(fileLine,200,fd)
它预计有 200 字节的可用内存。
试试这个:
char *fileLine = malloc(200);
if (fileLine == NULL) { ... } // check for error
这将分配所需的内存。
您正在使用 open()
而不是 fopen()
。
您不能确定文件是否正确打开,因为 fopen()
不是 return 整数,而是指向 FILE *
对象的指针,失败时 returns NULL
,所以正确的条件是
FILE *file;
file = fopen(filename, "r");
if (file == NULL)
{
perror("fopen()");
return -1;
}
在您的代码中,即使 fopen()
失败,您仍然会继续使用 fgets()
,在这种情况下您应该中止程序。
此外,malloc()
将字节数作为大小参数,因此如果您希望 fgets()
被限制为仅读取 count
字节,则 malloc()
应该是
char *buffer;
size_t count;
count = 200; /* or a value obtained someway */
buffer = malloc(count);
if (buffer == NULL)
{
fclose(file);
perror("malloc()");
return -1;
}
如果启用编译警告,编译器会指出代码中的所有问题。
为什么要改用 open() with FILE
? Use fopen()。
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]) {
char *fileName = "test.txt";
char *tokenize, *savePtr;
char fileLine[200] = {0}; // init this to be NULL terminated
FILE *fd = fopen(fileName, "r");
if (fd == 0) { // error check, equal to 0 as iharob said, not less than 0
perror("ERROR OPENING FILE");
return -1;
}
while (fgets(fileLine, 200, fd) != NULL) {
printf("%s\n", fileLine);
tokenize = strtok_r(fileLine, ",", &savePtr);
while (tokenize != NULL) {
tokenize = strtok_r(NULL, ",", &savePtr); // do not forget to pass NULL
//TOKENIZING into a struct
}
}
return 0;
}
正如风向标所说,如果您使用 open()
,fd < 0
会起作用。但是,对于 fopen()
,您应该检查指针是否为 NULL
,等同于 fd == 0
.
可以在以下位置找到打开文件的函数之间的比较:
- open and fopen function
- C fopen vs open
我的理解是fopen()
级别更高