求vc二进制文件读写代码

作者&投稿:斋卷 (若有异议请与网页底部的电邮联系)
VC++ 二进制读写文件~

char buffer[8];
int i;
CFile file1("D:\\file1",CFile::modeRead|CFile::typeBinary); //打开文件

CFile file2("D:\\file2",CFile::modeWrite|CFile::typeBinary); //打开文件
for (i = 0; i <= file.GetLength(); i += 8)
{
file1.Read(buffer,8);
file2.Write(buffer,8);
}
file1.Close();
file2.Close();

不知道你的fwrite和fread中间有什么代码,fwrite后,文件指针指向末尾,这时读是读不到东西的,必须使用fseek将指针移到最前面或是关闭文件后再重新打开。

#include <stdio.h>
main()
{
int M=10,N=15,i;
long data_length=M*N;
FILE *fp,*fp1;
if((fp1=fopen("d:\\data\\15_10.txt","wt+")) == NULL)
{
printf("File 100-151.txt cannot be opened! \n");
exit(0);
}
if((fp= fopen("d:\\data\\15_10.vct", "rb")) == NULL )
{
printf("File 100-151.vct cannot be opened! \n");
exit(0);
}
{
fseek(fp,0,SEEK_SET);
for(i=1;i<=data_length;i++)
{
short tmp;
fread(&tmp, 2, 1,fp);
fprintf(fp1,"v[%5d]=%d ",i,tmp);
if(i%100==0)
fprintf(fp1,"\n");
}
fclose(fp);
}

}

说白了就是把N换成二进制,然后把1位上的3的幂输出

比如20=(10100)2
所以是{9,81}

具体算法要用到高精度计算3的幂,换成二进制就用除2倒取余,复杂度O(l*logn),l是计算高精度的复杂度。

手头没有C++,不想写了,毕竟用记事本写没有缩进很不爽

include <iostream>
const int max_bit=100;
int num[max_bit]={1};

int main(){
using namespace std;
unsigned long n;
int l=0;
cin>>n;
while (n>0){
if (n%2==1){
for (int i=l;i>=0;i--) cout<<num[i];
cout<<endl;
}
n\=2;
for (int i=0;i<=l;i++) num[i]*=3;
for (int i=0;i<l;i++)
if (num[i]>=10) {
num[i+1]+=num[i]\10;
num[i]%=10;
}
while (num[l]>=10){
num[l+1]+=num[l]\10;
num[l++]%=10;
}
}
}
大概就是这么回事,这个程序应该不能编译吧,用notepad写的…很久不写程序了,一些基本的语法都未必正确……你自己改吧。
高手表踩我…

unsigned long long int buffer;

FILE *file1 = fopen("xxxxx", "rb");

FILE *file2 = fopen("xxxxxxx", "wb");

fread(&buffer, 8, 1, file1);//读文件

fwrite(&buffer, 8, 1, file2);//写文件

//参考代码
#include <stdio.h>
main()
{
 int M=10,N=15,i;
 long data_length=M*N;
 FILE *fp,*fp1;
 if((fp1=fopen("d:\\data\\15_10.txt","wt+")) == NULL)//判断文件是否打开成功
 {
  printf("File 100-151.txt cannot be opened! 
");
  exit(0);
 }
 if((fp= fopen("d:\\data\\15_10.vct", "rb")) == NULL )//判断文件是否打开成功
 {
  printf("File 100-151.vct cannot be opened! 
");
  exit(0);
 } 
 {
  fseek(fp,0,SEEK_SET);
  for(i=1;i<=data_length;i++)
  {
   short tmp;
   fread(&tmp, 2, 1,fp);//读文件
   fprintf(fp1,"v[%5d]=%d  ",i,tmp);//写文件
   if(i%100==0)
    fprintf(fp1,"
");
  }
  fclose(fp);
 }

}


用c的话
fprint
fscanf
等等
你可以看看书啊
就是文件那章的
。。。
若是c++的话。。如下
#include
using
namespace
std;

//
authinfo
是自定义的
struct
struct
authinfo
auth_info;
string
susername,
spassword;
/*
写文件
*/
//
清零
zeromemory
(
&auth_info,
sizeof
(
auth_info
)
);
susername
=
“tanggaowei@gmail.com”;
spassword
=
“000000″;
//
内存拷贝
memcpy(auth_info.username,
susername.c_str(),
susername.length());
memcpy(auth_info.password,
spassword.c_str(),
spassword.length());
//
定义打开输出流
ofstream
fout(”mbc.dat”,
ios::binary);
//
写入
fout.write((char
*)(&auth_info),
sizeof(auth_info));
//
关闭输出流
fout.close();
/*
读文件
*/
zeromemory
(
&auth_info,
sizeof
(
auth_info
)
);
ifstream
fin
(
“mbc.dat”,
ios::binary
);
fin.read((char
*)(&auth_info),
sizeof(auth_info));
susername
=
auth_info.username;
spassword
=
auth_info.password;
zeromemory
(
auth_info.username,
100
);
//
authinfo.username[100]
zeromemory
(
auth_info.password,
50
);
//
authinfo.password[50]
memcpy(auth_info.username,
susername.c_str(),
susername.length());
memcpy(auth_info.password,
spassword.c_str(),
spassword.length());
fin.close();

你说用vc,那么是使用c方法,c++方法或者是mfc方法呢?


vc如何对二进制文件修改保存
打开文件->读入文件->修改文件->保存文件,基本思路就是这样,用sdi就可以,当然,这其中需要一些数据处理,不过都是技巧问题。

VC读写二进制文件
把你的代码贴上来,估计是打开文件的方式不对,是不是选择了文本方式打开,改为用二进制方式打开并写入就可以了。

求一个读写二进制文件VC源程序,简单点就行,能读,能写
include <fstream> using namespace std;int main(){ fstream fsctrl;fsctrl.open("C:\\\\read.txt",ios::in|ios::binary);if(!fsctrl){ return -1;} string str;fsctrl >> str;fsctrl.close;fsctrl.open("C:\\\\write.txt",ios::out|ios::binary);if(!fsctrl){ return -1;} fs...

VC6 中怎么读取二进制文件
typedef struct xxx \/\/定义一个你的数据类型 { int a;byte b;rect c;}xxx,*lpxxx;你就可以在程序用这个类型了。void readfile(){ xxx m;CFile i;i.open( "你的文件 ",CFile;;modeRead);i.Read(&m,sizeof(xxx));i.close;当然你也可以重复读 xxx m[10];for (int y=0;y <10;y...

使用VC++读取两个文件和写文件?
二、关闭文件 打开的文件使用完成后一定要关闭,fstream提供了成员函数close()来完成此操作,如:file1.close();就把file1相连的文件关闭。三、读写文件 读写文件分为文本文件和二进制文件的读取,对于文本文件的读取比较简单,用插入器和析取器就可以了;而对于二进制的读取就要复杂些,下要就详细的...

请教VC读取二进制文件问题
你把你个len指定一个常数就行了 例如: #define len 10000 把len开大点就行了

vc++\/C++ txt文本文件读写
1.fopen的函数原型:FILE * fopen(const char * path,const char * mode); fopen函数的第一个参数是文件路径,第二个参数是打开方式,有以下几种方式:r 以只读方式打开文件,该文件必须存在。r+ 以可读写方式打开文件,该文件必须存在。rb+ 读写打开一个二进制文件,允许读数据。rw+ 读写打开...

[高分][紧急]vc2005中关于读入二进制文件
1) 用BYTE 替代 char 2) 加密过程不用CString 读一个BYTE写一个BYTE是最好算法,即 Open( f1...,CFile::modeRead Open( f2...,CFile::modeWrite f1.Read(...\/\/转化 f2.Write(...3)用CFile 即可,不用CStdioFile 4) 最好能用UNICODE方式(不用亦可)...

如何用VC++60编写查看二进制文件程序
File类的fopen,fread或者CFile类函数open,read都可以

如何快速读取二进制文件
Visual C++ 如何:读取二进制文件 下面的代码示例演示如何从文件中读取二进制数据。使用了 System.IO 命名空间中的两个类:FileStream 和 BinaryReader。FileStream 表示实际的文件。BinaryReader 为允许二进制访问的流提供接口。下面的代码示例使用由如何:编写二进制文件中的代码创建的称为 data.bin 的文件。

安宁区15919597546: 求vc二进制文件读写代码
蔺是普必: #include &lt;stdio.h&gt; main() { int M=10,N=15,i; long data_length=M*N; FILE *fp,*fp1; if((fp1=fopen("d:\\data\\15_10.txt","wt+")) == NULL) { printf("File 100-151.txt cannot be opened! \n"); exit(0); } if((fp= fopen("d:\\data\\15_10.vct", "rb"...

安宁区15919597546: 求vc二进制文件读写代码 -
蔺是普必: #include <stdio.h> main() { int M=10,N=15,i; long data_length=M*N; FILE *fp,*fp1; if((fp1=fopen("d:\\data\\15_10.txt","wt+")) == NULL) { printf("File 100-151.txt cannot be opened! \n"); exit(0); } if((fp= fopen("d:\\data\\15_10.vct", "rb")) ==...

安宁区15919597546: 如何用vc 实现对文件的读写呀? 是二进制那种的. -
蔺是普必: 1.文件的打开fopen() fopen(char *filename,char *type); 其中*filename是要打开文件的文件名指针,一般用双引号括起来的文件名表示,也可使用双反斜杠隔开的路径名.而*type参数表示了对打开文件的操作方式.其可采用的操作方式如下:方...

安宁区15919597546: 求一个读写二进制文件VC源程序,简单点就行,能读,能写 -
蔺是普必: #include <fstream> using namespace std; int main() { fstream fsctrl; fsctrl.open("C:\\read.txt",ios::in|ios::binary); if(!fsctrl) { return -1; } string str; fsctrl >> str; fsctrl.close; fsctrl.open("C:\\write.txt",ios::out|ios::binary); if(!fsctrl) { return -1; } fsctrl << str;...

安宁区15919597546: VC读取二进制文件 - --急用
蔺是普必: 保存 UpdateData(true); FILE *stream; stream=fopen("aa.dat","wb+"); if(stream==NULL) {MessageBox("文件创建失败","提示"); return; } fprintf(stream,"%lf\t%c\t\n...",a,b...); fclose(stream); UpdateData(false); fprintf中 ,"%lf\t%c\...

安宁区15919597546: vc语言32位二进制文件读写 -
蔺是普必: int n = 100; char *s = "abcdefg";// read CFile f; f.Open("c:\\bin", CFile::modeCreate|CFile::modeWrite); f.Write(&n, sizeof(n)); f.Write(s, strlen(s)); f.Close();// write char str[100]; f.Open("c:\\bin", CFile::modeRead); f.Read(&n, sizeof(n)); f.Read(str, 7); f.Close();

安宁区15919597546: 求一个c++二进制输入输入文件的代码 -
蔺是普必: ios::in //供读,文件不存在则创建(ifstream默认的打开方式) ios::out //供写,文件不存在则创建,若文件已存在则清空原内容 ios::binary//二进制格式文件 现写一个例子如下, 往c.txt(为空白文件)中按二进制写入内容和读取内容:#include <...

安宁区15919597546: C语言编写一个程序:读取任何二进制文件,并将文件内所有字节按相反排序存入另一文件中 -
蔺是普必: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37#include <stdio.h> #define FILE_NAME "E:/2.bin"// 读取的文件名 #define NEW_FILE_NAME "E:/2$.bin"// 新文件名 intmain() { ...

安宁区15919597546: c语言二进制文件读写的编程题 -
蔺是普必: #include <stdio.h> int main(int argc, char* argv[]) { FILE *fp1,*fp2; if(argc != 3) { printf("useage:\ncopy infile outfile\n"); return 1; } fp1 = fopen(argv[1],"rb"); if(!fp1) { printf("can't open file %s\n",argv[1]); return 2; } fp2 = fopen(argv[2],"wb"); if(!...

安宁区15919597546: vc++读取二进制 转换字符 -
蔺是普必: 使用C++读写二进制文件要读取文件必须包含fstream头文件,这里包含了C++读写文件的方法.可以使用fstream类,这个类可以对文件进行读写操作.1、打开文件.打要读取文件必须包含fstream头文件,这里包含了C++读写文件的方法. 可...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网