Tag: fwrite

在C中读取和写入64位64位

我有这个超级简单的代码,我读取8个字节的块(我将在代码中稍后加密它们),然后将它们写在一个新文件中。 它运行良好,但最后8个字节没有写入。 知道为什么吗? #include #include #include #include int main() { uint64_t data; FILE *input, *output; // create output file output = fopen(“output.txt”, “w”); // read file input = fopen(“test.txt”, “rb”); if(input) { while(fread(&data, 8, 1, input) == 1) { fwrite(&data, 8, 1, output); } size_t amount; while((amount = fread(&data, 1, 8, input)) > 0) { […]

如何使用fread从文件中读取特定数据?

以下代码使用fwrite将student的数据写入文件并使用fread读取数据: struct record { char name[20]; int roll; float marks; }student; #include void main() { int i; FILE *fp; fp=fopen(“1.txt”,”wb”); //opening file in wb to write into file if(fp==NULL) //check if can be open { printf(“\nERROR IN OPENING FILE”); exit(1); } for(i=0;i<2;i++) { printf("ENTER NAME, ROLL_ NO AND MARKS OF STUDENT\n"); scanf("%s %d %f",student.name,&student.roll,&student.marks); fwrite(&student,sizeof(student),1,fp); […]

C函数fwrite()不写入文件

我正在尝试将tempGroupFile结构写入GroupFile 。 写入时fwrite()返回1,但实际上没有数据写入文件GroupFile 。 函数printRec()打印出屏幕上的结构。 data是结构的变量。 这些操作后,文件GroupFile为空。 码: GWTemp = fopen(tempGroupFile, “rb”); GW = fopen(GroupFile, “wb”); if((GW == NULL) || (GWTemp == NULL)) { puts(“Failed to open file.”); fflush(stdin); getchar(); return 0; } while(fread(&data, sizeof data, 1, GWTemp)) { if(fwrite(&data, sizeof data, 1, GW)) { printRec(data); } }

使用C将整数值写入二进制文件

我试图将9位数写入二进制文件。 例如,我想写整数值:275为100010011 ,依此类推。 fwrite只允许一次写入一个字节,我不知道如何操作这些位才能做到这一点。

fwrite将垃圾值写入文件

#include struct struct_type { int d; }; int main() { struct struct_type *cust; cust->d=13; FILE* fp; fp = fopen(“path to file”, “wb+”); or, fp = fopen(“path to file”, “w+”); fwrite(cust, sizeof(struct struct_type), 1, fp); fclose(fp); return 0; } 预期产出 13 但是将垃圾值写入文件。

尝试将结构实例写入文件时出现分段错误

我正在尝试将结构写入文件,但在运行时遇到分段错误: #include //just a struct for purposes of demonstration struct my_struct{ int prop1; int prop2; }; //writes dummy struct to filename.dat void writeStruct(){ FILE *file_pointer; file_pointer = fopen(“filename.dat”,”w”); //define and assign variables to a quick dummy struct struct my_struct *this_struct; this_struct->prop1=0; //seg faults here! this_struct->prop2=1; //write struct to file fwrite(this_struct, sizeof(*this_struct), 1, file_pointer); fclose(file_pointer); } […]

将9位值的流作为字节写入C中的文件

我有一个数组,其整数值从0-511(最多9位)。 我试图用fwrite写这个文件。 例如,使用数组: [257, 258, 259] Which is 100000001, 100000010, 100000011 I am trying to write 100000001100000010100000011 + extra padding of 0s to the file 但由于fwrite限制一次写入1个字节,我不知道该怎么做。 我是按位操作的新手,而不是如何分离各个字节。

在C中使用fread和fwrite的问题

以下是我的代码,将一些硬编码的int值(1,2,3,4,5)写入文件,关闭该文件,在读取模式下打开同一文件并读取写入的元素。 从输出中看到fwrite正常发生但fread无法正确读取。 #include int main() { FILE *fptr; FILE *optr; const char *filepath = “E:\\testinput.txt”; int buf[5]={1,2,3,4,5}; int obuf[5]; int value; int *ptr = &value; int num_bytes_read; int no_of_iterations; int i; int ret;//return value for fwrite int count = 0; no_of_iterations = 5; //open the file fptr = fopen(filepath, “wb”); if(fptr == NULL){ printf(“error in […]

我需要从一个我们不知道矩阵尺寸的文件中读取矩阵

我有这样的结构 struct Data { int ID; double test_sample[2065][1]; int XX_row; int XX_col double **XX; //size=[2065][changing] double **alpha_new; //size=[changing][1] int alpha_new row; int alpha_new_col; double t3; double kernel_par; }person[20]; 我使用fwrite为每个人(20人)写了这个结构到20个文件: fwrite(&person, sizeof( struct Data ), 1,Ptr ); 现在我有20个二进制文件。 每个文件包含一个人的这些变量。 一切都好了。 问题:由于在每个文件中, XX和alpha_new矩阵的维度不同 ,我无法读取文件并将其识别为敲击文件。 (在文件[2065] [8]中,其中一些[2065] [12]) 我需要使用fread (或不同的)读取这些变量并输入到人脸识别程序……有没有办法在文件中单独读取变量或者我是否应该更改写入方法? 我不知道如何在不使用struct的情况下将所有变量矩阵写入一个文件中! 我希望我能在这里解释我的问题,抱歉我的英语不好,我等着你的帮助完成我在c的最后项目; 我正在使用visual studio 2012

在X中将XImage写入.bmp文件

我正在使用以下代码将XImage写入文件“bitmap0.bmp”但是当我在fedora中使用imageViewer打开新创建的文件时,它会出现“文件过早结束”错误并且不显示任何内容。 有谁可以请指出以下代码中的问题? typedef struct tagBITMAPFILEHEADER { WORD bfType; DWORD bfSize; WORD bfReserved1; WORD bfReserved2; DWORD bfOffBits; } BITMAPFILEHEADER; typedef struct tagBITMAPINFOHEADER{ DWORD biSize; LONG biWidth; LONG biHeight; WORD biPlanes; WORD biBitCount; DWORD biCompression; DWORD biSizeImage; LONG biXPelsPerMeter; LONG biYPelsPerMeter; DWORD biClrUsed; DWORD biClrImportant; BITMAPINFOHEADER; void saveXImageToBitmap(XImage *pImage) { BITMAPFILEHEADER bmpFileHeader; BITMAPINFOHEADER bmpInfoHeader; FILE *fp; static […]