在线等:怎样将一个浮点型的数组存储为*.dat或者*.txt?

wfth 2005-07-24 05:17:15
请哪位好心的大哥帮帮忙啊!
...全文
280 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
moonsuns 2005-11-14
  • 打赏
  • 举报
回复
但如果要重txt文件中读取浮点数,又如何操作呢!
wfth 2005-07-24
  • 打赏
  • 举报
回复
您真是太好了,我这下总算明白了.谢谢.
handsomerun 2005-07-24
  • 打赏
  • 举报
回复
有msdn的话,可以去看看msdn,嗯嗯,慢慢研究,嘿嘿
handsomerun 2005-07-24
  • 打赏
  • 举报
回复
那当然是乱吗啦
你这个a是一个double的数组
那么file.Write(a,10)
的话,你就把a的地址输出来了

Write的第一个参数得要是一个字符篡才行呢,呵呵

你把你的这些float拼成字符窜才能通过Write写出来
这个和ff<<a[0]<<endl是不一样的

CFile file("test.txt",CFile::modeCreate|CFile::modeReadWrite);
double a[10] = {1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,2.0};
CString ss;
ss.Format("%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f",a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
//file.Write(a,10);
file.Write(ss,ss.GetLength());
file.Close();

这样就ok了,当然,这个字符窜的格式怎么样,你还可以自己拼凑
wfth 2005-07-24
  • 打赏
  • 举报
回复
我是这样做的:
CFile file("e:\\test.txt",CFile::modeCreate|CFile::modeReadWrite);
double a[10] = {1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,2.0};
file.Write(a,10)
file.close();
就是乱码,不知道什么原因
handsomerun 2005-07-24
  • 打赏
  • 举报
回复
你怎么存的呢,怎么会存成乱吗了
wfth 2005-07-24
  • 打赏
  • 举报
回复
存一个字符串的时候,我倒是会一点,可是如果是存浮点型的数据的时候,存了之后就变成乱码了,不知道是怎么回事.
handsomerun 2005-07-24
  • 打赏
  • 举报
回复
点右上角的管理就可以了
晕了
handsomerun 2005-07-24
  • 打赏
  • 举报
回复
如果你是mfc的程序,那么可以用cfile的类
然后建一个cfile对象,用write函数把数组的内容写进去

HANDLE hFile = CreateFile(_T("C:\\FOO.DAT"),
GENERIC_WRITE, FILE_SHARE_READ,
NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

if (hFile == INVALID_HANDLE_VALUE)
AfxMessageBox(_T("Couldn't create the file!"));
else
{
// Attach a CFile object to the handle we have.
CFile myFile(hFile);

static const char sz[] = "Hockey is best!";

// write string, without null-terminator
myFile.Write(sz, lstrlen(sz));

// We can call Close() explicitly, but the destructor would have
// also closed the file for us. Note that there's no need to
// call the CloseHandle() on the handle returned by the API because
// MFC will close it for us.
myFile.Close();
}

这是msdn上使用cfile的例子,
你可以参考一下,呵呵
wfth 2005-07-24
  • 打赏
  • 举报
回复
怎么给分啊?
wfth 2005-07-24
  • 打赏
  • 举报
回复
非常感谢!
handsomerun 2005-07-24
  • 打赏
  • 举报
回复
你建一个win32console程序

然后有如下代码就可以写进去了

#include "stdafx.h"
#include <fstream.h>


int main(int argc, char* argv[])
{
fstream ff("a.text",ios::out);
double a[10] = {1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,2.0};
ff<<"a[0]=%f"<<a[0]<<endl;
ff<<"a[2]=%f"<<a[2]<<endl;
ff<<"a[4]=%f"<<a[4]<<endl;
ff<<"a[6]=%f"<<a[6]<<endl;

return 0;
}


a.text是你的文件名,没有设置路径的话,保存在当前目录下
构造fstream对象的时候
fstream( const char* szName, int nMode, int nProt = filebuf::openprot );

szName

The name of the file to be opened during construction.

nMode

An integer that contains mode bits defined as ios enumerators that can be combined with the bitwise OR ( | ) operator. The nMode parameter must have one of the following values:

ios::app The function performs a seek to the end of file. When new bytes are written to the file, they are always appended to the end, even if the position is moved with the ostream::seekp function.


ios::ate The function performs a seek to the end of file. When the first new byte is written to the file, it is appended to the end, but when subsequent bytes are written, they are written to the current position.


ios::in The file is opened for input. The original file (if it exists) will not be truncated.


ios::out The file is opened for output.


ios::trunc If the file already exists, its contents are discarded. This mode is implied if ios::out is specified, and ios::ate, ios::app, and ios:in are not specified.


ios::nocreate If the file does not already exist, the function fails.


ios::noreplace If the file already exists, the function fails.


ios::binary Opens the file in binary mode (the default is text mode).
Note that there is no ios::in or ios::out default mode for fstream objects. You must specify both modes if your fstream object must both read and write files.


只要设置这两个参数就可以了


sant 2005-07-24
  • 打赏
  • 举报
回复
按BYTE的内存形式存。
wfth 2005-07-24
  • 打赏
  • 举报
回复
是的,我就是这个意思,我几乎没有接触过VC,现在需要用一下完成一些简单的操作.可就是搞不懂,麻烦你说详细一点好吗?
handsomerun 2005-07-24
  • 打赏
  • 举报
回复
没看懂,

你的意思是把数据存到一个文件里面吗??

如果是话,用fstream进行写文件吧,
wfth 2005-07-24
  • 打赏
  • 举报
回复
没人回答呀?谁帮我一下嘛

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧