C++:怎么向文件中写入数据?
3个回答
展开全部
如下:
#include<iostream>
using namespace std;
#include <fstream>
//包含的文件流头文件
#include <cstdlib>
int main()
{
fstream dataFile;
//定义文件操作对象
dataFile.open("aaa.tex",ios::out); //创建一个文件
if(!dataFile)
//检查文件是否创建成功
{
cout<<"error
open"<<endl;
exit(0);
}
dataFile<<"jjhshhs\n";
// 向文件写入数据
dataFile<<"dhjhjhsj\n";
dataFile.close();
//关闭文件。如果没关闭文件的数据将没有写入。因为有一个输入 缓冲区
system("pause");
return 0;
}
展开全部
#include <iostream>
#include <conio.h>
#include <fstream> // 头文件
//#include <cstdlib>
using namespace std;
const unsigned long MEGA_BYTE = (unsigned long)1024*1024;
ofstream fout; // 声明ofstream 类的对象 fout 因为class中需用到 所以先要给出声明
class COMPUTER
{
public:
COMPUTER(unsigned long mem, unsigned long hd, int cd)
{ // 声明构造函数
memory = mem;
hard_disk = hd;
cdrom = cd;
if(memory > (unsigned long)16*MEGA_BYTE)
{
cout << "You should install Windows 95!\n";
fout << "You should install Windows 95!\n";
// fout 的用法与cout 的一样
}
else
{
cout << "You should only install Window 3.2!\n";
fout << "You should only install Window 3.2!\n";
}
if(hard_disk > (unsigned long)800*MEGA_BYTE)
{
cout << "You can install Visual C++ 4.5! \n";
fout << "You can install Visual C++ 4.5! \n";
}
else
{
cout << "You can only install Borland C++ 4.5! \n";
fout << "You can only install Borland C++ 4.5! \n";
}
if(cdrom > 4)
{
cout << "You can see Video CD! \n";
fout << "You can see Video CD! \n";
}
else
cout << "You can only lidten Audio! \n";
}
~COMPUTER() // 定义析构函数 其结构只有在txt文件才能看到
{
cout << "You should make good use of your computer!\n";
fout << "You should make good use of your computer!\n";
}
private:
unsigned long memory;
unsigned long hard_disk;
unsigned long cdrom;
};
int main()
{
//ofstream fout;
fout.open("构造函数与析构函数的文件输出.txt"); // 建立并打开txt文件 将内容写入其中
//if(!fout.open() )
//// {
// cout << "cound not open the file " << endl;
// exit(EXIT_FAILURE);
// }
COMPUTER my_computer(32*MEGA_BYTE, 1024*MEGA_BYTE, 8);
cout << "I have a good computer! \n";
fout << "I have a good computer! \n";
getch();
return 0;
}
分析一下这个例子 应该桥的了
#include <conio.h>
#include <fstream> // 头文件
//#include <cstdlib>
using namespace std;
const unsigned long MEGA_BYTE = (unsigned long)1024*1024;
ofstream fout; // 声明ofstream 类的对象 fout 因为class中需用到 所以先要给出声明
class COMPUTER
{
public:
COMPUTER(unsigned long mem, unsigned long hd, int cd)
{ // 声明构造函数
memory = mem;
hard_disk = hd;
cdrom = cd;
if(memory > (unsigned long)16*MEGA_BYTE)
{
cout << "You should install Windows 95!\n";
fout << "You should install Windows 95!\n";
// fout 的用法与cout 的一样
}
else
{
cout << "You should only install Window 3.2!\n";
fout << "You should only install Window 3.2!\n";
}
if(hard_disk > (unsigned long)800*MEGA_BYTE)
{
cout << "You can install Visual C++ 4.5! \n";
fout << "You can install Visual C++ 4.5! \n";
}
else
{
cout << "You can only install Borland C++ 4.5! \n";
fout << "You can only install Borland C++ 4.5! \n";
}
if(cdrom > 4)
{
cout << "You can see Video CD! \n";
fout << "You can see Video CD! \n";
}
else
cout << "You can only lidten Audio! \n";
}
~COMPUTER() // 定义析构函数 其结构只有在txt文件才能看到
{
cout << "You should make good use of your computer!\n";
fout << "You should make good use of your computer!\n";
}
private:
unsigned long memory;
unsigned long hard_disk;
unsigned long cdrom;
};
int main()
{
//ofstream fout;
fout.open("构造函数与析构函数的文件输出.txt"); // 建立并打开txt文件 将内容写入其中
//if(!fout.open() )
//// {
// cout << "cound not open the file " << endl;
// exit(EXIT_FAILURE);
// }
COMPUTER my_computer(32*MEGA_BYTE, 1024*MEGA_BYTE, 8);
cout << "I have a good computer! \n";
fout << "I have a good computer! \n";
getch();
return 0;
}
分析一下这个例子 应该桥的了
更多追问追答
追问
差不多懂了,但是还有几个小小的疑问,一,#include 用来干嘛的?用了这里面的什么函数啊?二,getch();这是什么意思?用来干嘛?谢谢!
追答
呵呵 那个getch()是读取下一次键击 conio.h>是他的头文件 当然 你也可以把它去掉 不影响程序的运行
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <fstream>
using namespace std;
.....
void f( )
{
ofstream out("abc.txt");
out << 3 << '\t' << "abc" << endl;
out.close( );
}
using namespace std;
.....
void f( )
{
ofstream out("abc.txt");
out << 3 << '\t' << "abc" << endl;
out.close( );
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询