为什么创建的文本文件是空白的?
Why text file created is blank?
我的密码是
#include <iostream.h>
#include <conio.h>
#include <fstream.h>
#include <stdio.h>
struct info
{
char product_name[100], Seller_Name[100], DOP[30];
int price;
}data;
void main()
{
ofstream fout("code.txt",ios::out);
fout<< "ofstream fout(\"data.dat\",ios::binary|ios::out);\n";
while(1)
{
cout << "Enter Product Name: ";
gets(data.product_name);
cout<<"Enter Seller Name: ";
gets(data.Seller_Name);
cout<<"Enter Date of Purchase: " ;
gets(data.DOP);
cout<<"Enter Price:" ;
cin>>data.price;
fout<<"strcpy(data.product_name,"<<data.product_name<<");";
fout<<"nstrcpy(data.Seller_Name,"<<data.Seller_Name<<");";
fout<<"nstrcpy(data.DOP,"<<data.DOP<<");";
fout<<"nstrcpy(data.price,"<<data.price<<");";
fout<<"fout.write((char*)&data,sizeof(info));n";
}
}
我正在开发一个软件并为它制作示例数据。所以我制作了这个应用程序,所以我只需要复制语句而不必重新编写。第一次用,现在不行了。
完成后尝试关闭输出流
fout.close()
查看此处了解更多信息:
http://www.cplusplus.com/doc/tutorial/files/
短:
数据被缓冲并且仅作为优化定期写入。
关闭文件会刷新缓冲区(写入所有内容)并为其他进程释放文件。
您的缓冲区可能没有被刷新。
试试这个:
while(1){
//...
fout.flush();
}
我的密码是
#include <iostream.h>
#include <conio.h>
#include <fstream.h>
#include <stdio.h>
struct info
{
char product_name[100], Seller_Name[100], DOP[30];
int price;
}data;
void main()
{
ofstream fout("code.txt",ios::out);
fout<< "ofstream fout(\"data.dat\",ios::binary|ios::out);\n";
while(1)
{
cout << "Enter Product Name: ";
gets(data.product_name);
cout<<"Enter Seller Name: ";
gets(data.Seller_Name);
cout<<"Enter Date of Purchase: " ;
gets(data.DOP);
cout<<"Enter Price:" ;
cin>>data.price;
fout<<"strcpy(data.product_name,"<<data.product_name<<");";
fout<<"nstrcpy(data.Seller_Name,"<<data.Seller_Name<<");";
fout<<"nstrcpy(data.DOP,"<<data.DOP<<");";
fout<<"nstrcpy(data.price,"<<data.price<<");";
fout<<"fout.write((char*)&data,sizeof(info));n";
}
}
我正在开发一个软件并为它制作示例数据。所以我制作了这个应用程序,所以我只需要复制语句而不必重新编写。第一次用,现在不行了。
完成后尝试关闭输出流
fout.close()
查看此处了解更多信息: http://www.cplusplus.com/doc/tutorial/files/
短:
数据被缓冲并且仅作为优化定期写入。 关闭文件会刷新缓冲区(写入所有内容)并为其他进程释放文件。
您的缓冲区可能没有被刷新。
试试这个:
while(1){
//...
fout.flush();
}