📄 subject_29371.htm
字号:
<p>
序号:29371 发表者:枫叶游子 发表日期:2003-02-06 12:35:38
<br>主题:请教如何把结果输出到二进制文件?
<br>内容:请教:<BR><BR>在C++的console模式下编程,(不是VC 的MFC)<BR><BR>如何把一个浮点数写入一个新建立的二进制文件再读出?<BR><BR><BR>初学者,请写一段例程供学习。包括如何生成并打开文件,<BR>如何写入,读出。。。。<BR>对文件操作还不熟悉。<BR><BR><BR>谢谢!!
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
回复者:tjhe 回复日期:2003-02-06 12:59:06
<br>内容:#include <iostream.h><BR>#include <fstream.h><BR><BR>void main()<BR>{ <BR> float x,y;<BR> cout << "请输入两个浮点数" << endl;<BR> cin >> x >> y;<BR> //建立文件并写入 <BR> ofstream outf("abc.dat",); //文件名可自行定义<BR> outf << x << y;<BR> outf.close();<BR> //打开文件并读出<BR> ifstream inf("abc.dat");<BR> inf >> x >> y;<BR> inf.close()<BR> cout << "x=" << x << endl;<BR> cout << "y=" << y << endl;<BR>}
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:枫叶游子 回复日期:2003-02-06 13:44:22
<br>内容:谢谢。<BR><BR>我把你的程序稍改一下,如下所示,<BR>#include <iostream.h><BR>#include <fstream.h><BR><BR>void main()<BR>{ <BR> float x=1.0;<BR> float y=2.0;<BR> <BR> ofstream outf("\\data\\1.dat"); <BR> outf << x << y;<BR> outf.close();<BR> <BR> ifstream inf("\\data\\1.dat");<BR> inf >> x >> y;<BR> inf.close();<BR> cout << "x=" << x << endl;<BR> cout << "y=" << y << endl;<BR>} <BR><BR><BR>运行得到:<BR>x=12<BR>y=2<BR><BR>与所赋初值不一致,为什么??<BR>盼回复。<BR><BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:tjhe 回复日期:2003-02-06 14:50:53
<br>内容:outf << x << y; //改成outf << x << ' ' << y;<BR><BR><BR>#include <iostream.h><BR>#include <fstream.h><BR>#include <stdlib.h><BR><BR>void main()<BR>{ <BR> float x=1.0;<BR> float y=2.0;<BR> <BR> ofstream outfile("1.dat"); <BR> if(!outfile)<BR> {<BR> cout << "file could not be created!" << endl;<BR> exit(1);<BR> }<BR><BR> outfile << x << ' ' << y;<BR> outfile.close();<BR> <BR> ifstream infile("1.dat");<BR> if(!infile)<BR> {<BR> cout << "file could not be opened!" << endl;<BR> exit(1);<BR> }<BR> <BR> infile >> x >> y;<BR> infile.close();<BR><BR> cout << "x=" << x << endl;<BR> cout << "y=" << y << endl;<BR>} <BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:枫叶游子 回复日期:2003-02-07 03:58:34
<br>内容:朋友,谢谢你的热心帮助。<BR>这个问题已经解决。<BR><BR>但你能解释一下outf << x << y; //改成outf << x << ' ' << y;<BR>的道理吗?<BR><BR>另外,如果我是把一个2维浮点数组写入一个2进制文件保存呢?<BR>程序如何写? 直接用2重 for循环写入,读出吗?<BR><BR>请指教!<BR>谢谢
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:枫叶游子 回复日期:2003-02-07 04:50:55
<br>内容:再提一个问题:<BR><BR>我单独写了一个程序如下,读一个文件(里面是一个2维数组)并输出到屏幕:<BR><BR>#include <iostream.h><BR>#include <fstream.h><BR>#include <stdlib.h><BR><BR><BR>void main()<BR><BR>{<BR><BR>double output=0.0;<BR><BR>// open file for read<BR> ifstream infile("\\data\\1.dat");<BR> if(!infile)<BR> {<BR> cout << "file creating failed!" << endl;<BR> exit(1);<BR> }<BR><BR><BR> //read from datafile<BR>{<BR> for (int i=0;i<2;i++)<BR> <BR> { <BR> for(int j=0;j<3;j++)<BR> {<BR> infile >> output;<BR> cout<< output;<BR> }<BR> <BR> cout<<endl;<BR> }<BR><BR> infile.close();<BR>}<BR><BR>}<BR><BR>结果是:<BR>000<BR>000<BR><BR>我的c:\data路径下其实并没有文件,是一个空目录;<BR>为什么程序默认里面的数据全是0???<BR>如果我希望程序不要自动生成文件,而是发现没有该文件存在时<BR>报出错信息,那么又该怎么写??<BR><BR>麻烦你了。<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:Herbert_24 回复日期:2003-02-07 14:32:21
<br>内容:#include "StdAfx.h"<BR>#include <iostream.h><BR>#include <fstream.h><BR>#include <stdlib.h><BR><BR>void main()<BR>{ <BR>/* <BR> double x=1.02;<BR> double y=2.04;<BR> <BR> ofstream outfile("1.txt",<BR> ios::out |<BR> ios::binary,<BR> filebuf::sh_write); <BR> if(outfile.fail())<BR> {<BR> cout << "file could not be created!" << endl;<BR> exit(1);<BR> }<BR><BR> outfile << x << " " << y;<BR> outfile.close();<BR> <BR> ifstream infile("1.txt",<BR> ios::in |<BR> ios::binary,<BR> filebuf::sh_read);<BR> <BR> if(infile.fail())<BR> {<BR> cout << "file could not be opened!" << endl;<BR> exit(1);<BR> }<BR> <BR> infile >> x >> y;<BR> infile.close();<BR><BR> cout << "x=" << x << endl;<BR> cout << "y=" << y << endl;<BR>*/<BR> double output=2.35 ;<BR><BR>// open file for read<BR> ifstream infile("1.txt", // filename .<BR> ios::in | // read mode .<BR> ios::binary | // binary mode .<BR> ios::nocreate , // if does not file . the error .<BR> filebuf::sh_read); // Read sharing allowed .<BR> if(infile.fail())<BR> {<BR> cout << "file creating failed!" << endl;<BR> exit(1);<BR> }<BR><BR><BR> for (int i=0; i<3; i++)<BR> {<BR> infile >> output ;<BR> cout << output << endl ;<BR> }<BR> infile.close();<BR>} <BR><BR> 当文件1.txt为空时. 输出到屏幕的值只是output 的初始化的值.<BR> 你看看VC 的MSDN 上面都有.
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -