在C FREAD和fwrite的问题

编程入门 行业动态 更新时间:2024-10-26 21:20:40
本文介绍了在C FREAD和fwrite的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

以下是我的code写的一些硬codeD INT值(1,2,3,4,5)到一个文件,关闭文件,在读模式下打开相同的文件并阅读元素写入。和fwrite正确偏偏从输出看到的,但FREAD不能正确读取。

#包括LT&;&stdio.h中GT;诠释的main(){    FILE * FPTR;    FILE * OPTR;    为const char *文件路径=E:\\\\ testinput.txt              INT的buf [5] = {1,2,3,4,5};    诠释OBUF [5];         int值;    为int * PTR =安培;价值;     INT num_bytes_read;    INT no_of_iterations;    INT I;    INT RET; //为FWRITE返回值    诠释计数= 0;    no_of_iterations = 5;    //打开文件    FPTR = FOPEN(文件路径,WB);    如果(FPTR == NULL){       的printf(错误打开输入文件);    }    / * OPTR = FOPEN(outFilepath,WB);    如果(OPTR == NULL){           的printf(错误打开输出文件);    } * /    的printf(INT%D的sizeof(INT));    对于(I = 0; I&小于5;我++){        的printf(写%D,BUF [I]);    RET = FWRITE(BUF,sizeof的(INT),1,FPTR);     如果(RET!= 1)     {         的printf(在FWRITE错误数:%d \\ n,RET);     }    }    //写入输入文件    FCLOSE(FPTR);    FPTR = FOPEN(文件路径,RB);            如果(FPTR == NULL){               的printf(错误打开输入文件);            }    对于(I = 0; I&小于5;我++){            //从输入文件中读取           num_bytes_read = FREAD(PTR,sizeof的(INT),1,FPTR);           如果(num_bytes_read == 1){           OBUF [i] = * PTR; //存入BUF什么是从文件中读取           的printf(读成功数:%d \\ n,OBUF [I]);           算上++;           }           其他{               数= 99;            打破;           }         的printf(\\ NCOUNT%D,计数);  }  FCLOSE(FPTR);  返回0;}

输入文件写的,但如果手动打开(在任何文本编辑器)5个非字母数字字符(同符号重复)被认为是下面是在Eclipse中输出

INT 4写作1writing 2writing 3writing 4writing 5read成功:1count1read成功:1count2read成功:1count3read成功:1count4read成功:1count5

解决方案

现在的问题是在这条线:

= RET fwrite的(BUF,sizeof的(INT),1,FPTR);

所有5调用 FWRITE 有相同的缓冲区地址所以每次你正在写的数组的第一个元素时间,这是 1 。

既然你想要一个写所有的数组元素之一,通过 BUF + I 作为起始地址 FWRITE :

= RET fwrite的(BUF + I,的sizeof(INT),1,FPTR);

现在的缓冲区 FWRITE 得到的起始地址是的地址我日数组的元素

the following is my code to write some hardcoded int values(1,2,3,4,5) into a file, close that file, open the same file in read mode and read the elements written. The fwrite happens properly as seen from the output but the fread does not read correctly.

#include<stdio.h> 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 opening input file"); } /*optr = fopen(outFilepath, "wb"); if(optr == NULL){ printf("error in opening output file"); }*/ printf("int %d ", sizeof(int)); for(i=0;i<5;i++){ printf("writing %d",buf[i]); ret = fwrite(buf,sizeof(int),1,fptr); if(ret != 1) { printf("error in fwrite:%d\n", ret); } } //written to input file fclose(fptr); fptr = fopen(filepath, "rb"); if(fptr == NULL){ printf("error in opening input file"); } for(i=0;i<5;i++){ //reading from input file num_bytes_read = fread(ptr,sizeof(int),1,fptr); if(num_bytes_read == 1){ obuf[i] = *ptr;//storing into buf what is read from file printf("read successful: %d\n", obuf[i]); count++; } else{ count = 99; break; } printf("\ncount%d", count); } fclose(fptr); return 0; }

The input file written, if opened manually(in any text editor) 5 non-alphanumeric character(same symbol is repeated) is seen Here is the output in Eclipse

int 4 writing 1writing 2writing 3writing 4writing 5read successful: 1 count1read successful: 1 count2read successful: 1 count3read successful: 1 count4read successful: 1 count5

解决方案

The problem is in this line:

ret = fwrite(buf,sizeof(int),1,fptr);

All the 5 calls to fwrite have the same buffer address so every time you are writing the first element of the array which is 1.

Since you want to write all the array elements one by one, pass buf+i as the starting address to fwrite:

ret = fwrite(buf+i,sizeof(int),1,fptr);

Now the starting address of the buffer that fwrite gets is the address of the ith element of the array.

更多推荐

在C FREAD和fwrite的问题

本文发布于:2023-08-05 11:21:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1305202.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:FREAD   fwrite

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!