您是否有任何理由(语法除外)
FILE *fdopen(int fd, const char *mode);
Run Code Online (Sandbox Code Playgroud)
要么
FILE *fopen(const char *path, const char *mode);
Run Code Online (Sandbox Code Playgroud)
代替
int open(const char *pathname, int flags, mode_t mode);
Run Code Online (Sandbox Code Playgroud)
在Linux环境中使用C时?
我试图读取给定的文件然后标记它.我唯一的问题是fgets.文件打开收到没有错误.我在网站的其他地方已经看到了这个,但无论我如何设置它包括将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
}
}
Run Code Online (Sandbox Code Playgroud)