Tag: fwrite

FILE结尾*指针不等于写入数据的大小

非常简单地说,我有以下代码片段: FILE* test = fopen(“C:\\core.u”, “w”); printf(“Filepointer at: %d\n”, ftell(test)); fwrite(data, size, 1, test); printf(“Written: %d bytes.\n”, size); fseek(test, 0, SEEK_END); printf(“Filepointer is now at %d.\n”, ftell(test)); fclose(test); 它输出: Filepointer at: 0 Written: 73105 bytes. Filepointer is now at 74160. 这是为什么? 为什么写入的字节数与文件指针不匹配?

可以从同一文件描述符的两个线程并行调用fwrite和fclose吗?

如果从同一文件描述符的两个线程并行调用fwrite和fclose会发生什么?

C fread()神奇地读取动态分配的struct成员,怎么样?

这是我为一个我正在研究的大型项目编写的测试程序。 它与使用fwrite()将struct数据写入磁盘然后使用fread()读取该数据有关。 结构的一个成员是动态分配的。 首先,这是我的代码 #include #include #include #define STRING_LEN 128 struct Person { int age; char *name; }; int main(int argc, const char *argv[]) { struct Person *person = calloc(1, sizeof(struct Person)); person->age = 22; person->name = calloc(STRING_LEN, sizeof(char)); char *name = “Name that is really, really, really, really, really, really, long.”; strncpy(person->name, name, STRING_LEN); […]

检查返回值fread和fwrite

#include #include int main(int argc, char *argv[]){ if(argc != 3){ printf(“Usage: ./copy filename newfile\n”); exit(1); } int bytes; long file_size, file_copied_size; FILE *file_to_copy, *new_file; if((file_to_copy = fopen(argv[1], “rb”)) == NULL){ printf(“File cannot be opened – read\n”); exit(1); } if((new_file = fopen(argv[2], “wb”)) == NULL){ printf(“File cannot be opened – write\n”); exit(1); } fseek(file_to_copy, 0, SEEK_END); […]

fwrite“<?xml version”上的chokes

当字符串<?xml version通过fwrite写入文件时,后续的写入操作会变慢。 这段代码: #include #include #include int main() { const long index(15000000); clock_t start_time(clock()); FILE* file_stream1 = fopen(“test1.txt”,”wb”); fwrite(“<?xml version",1,13,file_stream1); for(auto i = 1;i < index ;++i) fwrite("only 6",1,6,file_stream1); fclose(file_stream1); std::cout << "\nOperation 1 took : " << static_cast(clock() – start_time)/CLOCKS_PER_SEC << " seconds."; start_time = clock(); FILE* file_stream2 = fopen("test2.txt","wb"); fwrite("<?xml versioX",1,13,file_stream2); for(auto i […]

将原始结构内容(字节)写入C中的文件。对写入的实际大小感到困惑

基本问题,但我希望这个结构占用13个字节的空间(1个用于char,12个用于3个无符号的int)。 相反, sizeof(ESPR_REL_HEADER)给了我16个字节。 typedef struct { unsigned char version; unsigned int root_node_num; unsigned int node_size; unsigned int node_count; } ESPR_REL_HEADER; 我想要做的是用一些值初始化这个结构并将它包含的数据(原始字节)写入文件的开头,这样当我打开这个文件后我才能重构这个结构并获得一些元数据有关文件其余部分包含内容的数据。 我正在初始化结构并将其写入文件,如下所示: int esprime_write_btree_header(FILE * fp, unsigned int node_size) { ESPR_REL_HEADER header = { .version = 1, .root_node_num = 0, .node_size = node_size, .node_count = 1 }; return fwrite(&header, sizeof(ESPR_REL_HEADER), 1, fp); } 我试验时node_size当前是4。 在向其编写结构后,该文件包含以下数据: […]

如何使用fread和fwrite函数来读写二进制文件?

嗨,在我的项目中,我要读取一个.bin文件,其中包含short(16 bit values)forms的传感器数据。 我正在使用fread函数进入缓冲区,但我觉得读入没有正确发生。 我的意思是我写的东西和我读的东西之间没有一致性。你们能否说出这里出了什么问题? 这不是我项目中的代码……我只是想在这里validationfread和fwrite函数。 #include void main() { FILE *fp = NULL; short x[10] = {1,2,3,4,5,6,5000,6,-10,11}; short result[10]; fp=fopen(“c:\\temp.bin”, “wb”); if(fp != NULL) { fwrite(x, 2 /*sizeof(short)*/, 10 /*20/2*/, fp); rewind(fp); fread(result, 2 /*sizeof(short)*/, 10 /*20/2*/, fp); } else exit(0); printf(“\nResult”); printf(“\n%d”,result[0]); printf(“\n%d”,result[1]); printf(“\n%d”,result[2]); printf(“\n%d”,result[3]); printf(“\n%d”,result[4]); printf(“\n%d”,result[5]); printf(“\n%d”,result[6]); printf(“\n%d”,result[7]); printf(“\n%d”,result[8]); printf(“\n%d”,result[9]); fclose(fp) } […]

将2d数组写入C中的文件

我曾经使用下面的代码将1D数组写入文件: FILE *fp; float floatValue[5] = { 1.1F, 2.2F, 3.3F, 4.4F, 5.5F }; int i; if((fp=fopen(“test”, “wb”))==NULL) { printf(“Cannot open file.\n”); } if(fwrite(floatValue, sizeof(float), 5, fp) != 5) printf(“File write error.”); fclose(fp); /* read the values */ if((fp=fopen(“test”, “rb”))==NULL) { printf(“Cannot open file.\n”); } if(fread(floatValue, sizeof(float), 5, fp) != 5) { if(feof(fp)) printf(“Premature end of […]

C中的fwrite在输出文件中给出不同的值

当我在同一个函数中的另一个函数VERSUS fwrite中使用fwrite时,为什么输出文件不同? output1.txt包含像Ê这样的垃圾值,这是不正确的 output2.txt包含“b”,这是正确的 #include #include void writeData(char *buf, char *path) { FILE *fp1; fp1 = fopen(path, “a”); fwrite(&buf, sizeof(char), strlen(buf), fp1); } int main () { char buf[2] = “a”; char buf2[3] = “yb”; char file1_path[12] = “output1.txt”; char file2_path[12] = “output2.txt”; memset(buf, 0, sizeof(buf)); memcpy(buf, &buf2[1], strlen(buf2)); printf(“%s\n”, buf); writeData(buf, file1_path); FILE *fp2; […]

C编程 – 文件 – fwrite

我有一个关于编程和文件的问题。 while(current!=NULL) { if(current->Id_Doctor!=’\0′) { current=current->next; id_doc=(current->Id_Doctor); } if(current->Id_Doctor==’\0′) { id_doc=id_doc+1; printf(“%d”, id_doc); break; } } fwrite(&id_doc, sizeof(char), 1, Archivo); 我不知道为什么,但它不是在名为’Archivo’的二进制文件上写id_doc的值…可能是什么问题? 我添加了一个id_doc的printf,并打印了值。我真的不知道 好的,inheritance人的完整代码(更多): struct Medico { int Id_Doctor; int Estado; char Nombre[60]; char Clave_Acceso[20]; char Especialidad[40]; struct Medico *next; }; void Dar_Alta_Med (int estado); void MenuPrincipal(char enta); int main(void) { char enta; MenuPrincipal(enta); } void […]