你建一个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 );szNameThe name of the file to be opened during construction.nModeAn 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. 只要设置这两个参数就可以了
如果你是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的例子, 你可以参考一下,呵呵
#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 );szNameThe name of the file to be opened during construction.nModeAn 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.
只要设置这两个参数就可以了
然后建一个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的例子,
你可以参考一下,呵呵
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();
就是乱码,不知道什么原因
你这个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了,当然,这个字符窜的格式怎么样,你还可以自己拼凑