fread函数读出来是乱码,求立。
fread函数读出来是乱码,求破。。。
#include <cstdio>
#include <fstream>
#include <cstring>
#include <windows.h>
using namespace std;
void main( )
{
FILE *fp;
unsigned char a[3];
unsigned char b[3];
fp=fopen("d:\\abc.txt","rb+");
fread(a,sizeof(char),3,fp);
fread(b,sizeof(char),3,fp);
printf("%s",a);
printf("%s",b);
fclose(fp);
system("pause");
}
------解决方案--------------------
写进去数据的时候,是不是也是以二进制的方式写进去的呢?
------解决方案--------------------
提问的时候把你的情况说清楚点,文件里面怎样,读出来显示怎样。打开文件看看是不是乱码,是乱码的话读出来也是啦。
你的字符数组可以开大一点嘛,也有可能是这个原因哦
------解决方案--------------------
用u-edit32 先打开你的文件看看里面的内容编码对不对
改成下面样子看看
unsigned char a[256];
memset(a,0,sizeof(a));
unsigned char b[256];
memset(b,0,sizeof(b));
fp=fopen("d:\\abc.txt","rb+");
if(fp==-1)
return;
------解决方案--------------------
A fundamental problem with binary I/O is that it can be used to read only data that has been written on the same system.
1.The offset of a member within a structure can differ between compilers and systems, because of different alignment requirements
2.The binary formats used to store multibyte integers and floating-point values differ among machine architectures
------解决方案--------------------
unsigned char a[3];
unsigned char b[3];
最好初始化
unsigned char a[3] = "";
unsigned char b[3] = "";
fp=fopen("d:\\abc.txt","rb+");
打开是否成功要判断
fread(a,sizeof(char),3,fp);
fread(b,sizeof(char),3,fp);
第三个参数应该为1吧
fread(a,sizeof(char),1,fp);
fread(b,sizeof(char),1,fp);
------解决方案--------------------
没有初始化额~
------解决方案--------------------
------解决方案--------------------
楼主用了puts(),但在puts()的参数的位置上却放了个字符数组的数组名。可以加一行,给这个字符数组封个尾,让它成为字符串:
------解决方案--------------------
memset(s,0,100);初始化数组
#include <cstdio>
#include <fstream>
#include <cstring>
#include <windows.h>
using namespace std;
void main( )
{
FILE *fp;
unsigned char a[3];
unsigned char b[3];
fp=fopen("d:\\abc.txt","rb+");
fread(a,sizeof(char),3,fp);
fread(b,sizeof(char),3,fp);
printf("%s",a);
printf("%s",b);
fclose(fp);
system("pause");
}
------解决方案--------------------
写进去数据的时候,是不是也是以二进制的方式写进去的呢?
------解决方案--------------------
提问的时候把你的情况说清楚点,文件里面怎样,读出来显示怎样。打开文件看看是不是乱码,是乱码的话读出来也是啦。
你的字符数组可以开大一点嘛,也有可能是这个原因哦
------解决方案--------------------
用u-edit32 先打开你的文件看看里面的内容编码对不对
改成下面样子看看
unsigned char a[256];
memset(a,0,sizeof(a));
unsigned char b[256];
memset(b,0,sizeof(b));
fp=fopen("d:\\abc.txt","rb+");
if(fp==-1)
return;
------解决方案--------------------
A fundamental problem with binary I/O is that it can be used to read only data that has been written on the same system.
1.The offset of a member within a structure can differ between compilers and systems, because of different alignment requirements
2.The binary formats used to store multibyte integers and floating-point values differ among machine architectures
------解决方案--------------------
unsigned char a[3];
unsigned char b[3];
最好初始化
unsigned char a[3] = "";
unsigned char b[3] = "";
fp=fopen("d:\\abc.txt","rb+");
打开是否成功要判断
fread(a,sizeof(char),3,fp);
fread(b,sizeof(char),3,fp);
第三个参数应该为1吧
fread(a,sizeof(char),1,fp);
fread(b,sizeof(char),1,fp);
------解决方案--------------------
没有初始化额~
------解决方案--------------------
fp=fopen("d:\\abc.txt","wb+");
fwrite(a,sizeof(char),3,fp);
------解决方案--------------------
楼主用了puts(),但在puts()的参数的位置上却放了个字符数组的数组名。可以加一行,给这个字符数组封个尾,让它成为字符串:
fread(s,i,count,fp);
s[count] = '\0';
puts(s);
------解决方案--------------------
memset(s,0,100);初始化数组