Tag: 文件

在C中读取文件:“r”和“a +”标志的不同行为

我想打开一个文件,读取其内容,然后在文件中附加一行。 我以为我应该使用“a +”标志来完成任务。 我有一个打开文件并返回指向该文件的指针的函数。 FILE* open_weekly_disk_file(char* filename){ FILE* weekly_log; weekly_log = fopen(filename, “a+”); //weekly_log = fopen(filename, “r”); if(! weekly_log){ printf(“The attempt to open the weekly log failed!\n”); return NULL; } else{ return weekly_log; } } 然后我有一个函数调用上面的函数并使用scanf从文件中读取内容: void sample_function(char* filename){ FILE* log; char token[100], current_read[100]; int limit; log = opened_weekly_disk_file(filename); // The problem happens here for(limit=0; […]

如何在c中写入特定的文件行?

所以我有一个项目,我正在创建一个允许用户写入文件的程序,如下所示: #include #include FILE *fd; FILE *fw; struct store { char Word[512]; char NWord[512]; } stock; struct store2 { char Definition[512]; } stock2; char done=’y’; int count=1; int c=0; int d=0; int main(void) { fw=fopen(“Test Z W.txt”,”w”); fd=fopen(“Test Z D.txt”,”w”); do { printf(“Word %d: “,count); gets(stock.Word); while((c= getchar()) != ‘\n’ && c != EOF); printf(“Definition […]

C – 获取下一个项目

(我只是被授权使用:malloc,免费,阅读) 我正在尝试使用任何BUF_SIZE获取文件中的下一行但是它返回了错误的值。 (42school / Epitech的get_next_line项目) 所以,返回(错误值,缺少第一个字符)值: 3 1 – #include 1 – dlib.h> 1 – clude “libft/libft.h” 1 – BUFF_SIZE 32 我的源代码: #include #include #include #include “libft/libft.h” # define BUFF_SIZE 32 int get_next_line(int const fd, char **line) { char buffer[BUFF_SIZE + 1]; size_t i; size_t size; if (!(*line = malloc(BUFF_SIZE))) return (-1); *line[0] = ‘\0’; […]

逐字逐行读取文件

我有一个文件,每行有两个未知长度,用空格分隔。 数字可以是任何大小,最多500个数字,所以我需要逐个字符地读取它并将其存储到整数数组中。 我试过这样的事情: while(fgets(line, sizeof line, fp) != NULL) { // assuming buff handles the line lenght //read each character until ” // store it //read each character until ‘\n’ // store it } 我尝试使用fgetc读取直到”和’\ n’,但它没有用。 任何建议将不胜感激。

如何在c中阅读.exe

我正在制作一个小型压缩程序的小项目。 为此,我想读取一个文件,比如一个.exe,然后用char解析它char并使用一些简单的字典算法来加密它。 为了阅读文件我只是使用一个简单的代码我发现: char *readFile(char *fileName) { FILE *file; char *code = malloc(10000* sizeof(char)); file = fopen(fileName, “rb”); do { *code++ = (char)fgetc(file); } while(*code != EOF); return code; } 我的问题是,读取.exe或任何文件似乎是不可能的。 在制作“代码”的printf()时,没有任何内容被写入。 我能做什么?

读取器接口,在C中使用FILE和char *

我正在修改我inheritance的解析器,目前只能通过读取从FILE *读取。 它现在需要能够从char *常量中提取数据,因此可以解析C字符串中的内联文本。 我已经看过以“读者”的forms提供一个简单的界面,所以你可以提供一个文件阅读器和一个char阅读器,解析器可以从中获取字符。 例如: // Inputs const char *str = “stringToParse”; FILE *f = fopen(…); // Creating a reader. Each reader stores a function ptr to a destructor // which closes the file if required and an internal state object. Reader *r = FileReader(f); // -or- Reader *r = CharReader(str); // Start parsing […]

如何在文件*流中的特定点停止并扫描某些值?

我有一个名为test.txt的文件,该文件包含: 10.213123 41.21231 23.15323 当我看到我希望扫描后的3个数字,如下所示: x = 10.213123 y = 41.21231 z = 23.15323 我有点困惑,因为在这里,fgets扫描整条线,我怎样才能将3个数字扫描成双线? 因为数字可以是各种长度? 我这样做是为了打印出它从文件中读取的内容,但我无法绕过它。 void print_lines(FILE *stream) { char line[MAX_LINE_LENGTH]; while (fgets(line, MAX_LINE_LENGTH, stream) != NULL) { fputs(line, stdout); } }

为什么从文件函数读取崩溃?

试图从文件中读取多行以将它们存储在由字符串元素组成的结构中,但是当我运行程序时它只是崩溃而我不知道原因。 有问题的function: Hashtbl* loadfromfile(Hashtbl* hashtbl, char *path){ int i = 0; char line[100]; char* string[40]; FILE *f = fopen(path, “r”); if(f == NULL){ printf(“FILE NO FOUND!”); }else{ while(fgets(line, sizeof(line), f)!=NULL){ strcpy(string[i],line); i++; } fclose(f); for(i = 0; i<(SIZE*2); i++){ strcpy(hashtbl[i].subscript, string[i]); i++; } for(i = 1; i<(SIZE*2); i++){ strcpy(hashtbl[i].value, string[i]); i++; } return hashtbl; } […]

从链表保存到文件并加载回来

我无法从文件加载到链接列表,一整天都在尝试 首先,这是我的结构 typedef struct Sensor { int id; int intervalo; char local[30]; char tipo[30]; //bool active; int active; struct Sensor* anterior; struct Sensor* proximo; } Sensor; 这是我的保存function,我认为它的工作正常,因为文件被创建,内容就在那里。 void gravaLista(Sensor* l) { FILE *ficheiro; Sensor* temp = l; ficheiro = fopen(“sensores.txt”, “r+t”); if (ficheiro == NULL) { ficheiro = fopen(“sensores.txt”, “w+t”); } while (temp != NULL) […]

如何将文件中的明文解析成2d矩阵/数组?

我编写的以下代码应该转换从文件中获取的行,如下所示: (3670, 1882) (1574, 7255) (4814, 8566) (1609, 3153) (9725, 13468) (8297, 3006) (9091, 6989) (8521, 10432) (14669, 12201) (4203, 9729) (469, 2444) (10107, 8318) (1848, 13650) (5423, 847) (11755, 8827) (4451, 4495) (11645, 1670) (10937, 5692) (14533, 13696) (7291, 12158) (1891, 2405) (1776, 4971) (2486, 2499) (13389, 236) (8533, 7531) (10618, 10288) (9119, 11226) […]