使用C ++编写文件 [英] File writing with C++
问题描述
我有一个程序需要做一些事情。
首先,文件需要不断更新新信息。
因为我这样做的功能并不总是在运行,我需要
才能关闭文件并重新打开它。
我知道这个可以通过以下方式完成(理论上),但是对于某些原因,我没有任何运气。我无法获得程序
打开文件,写入,关闭它,然后重新打开它以获得更多文件
写作。
我有一些示例代码,但它们都非常基本。
//打开
outfile.open (timestamp_file_name.c_str(),ios :: out | ios :: app);
//关闭
outfile.flush();
outfile.close();
有人可以帮我解决这个问题。为了解决这个问题,我不得不做一些非常有趣的小步骤,现在它已经成为了解决问题的更多问题。 。
感谢任何帮助,
Kris
I have a program that needs to have a couple things happen.
First, the file needs to constantly be updated with new information.
Because the function in which I do this is not always running, I need
to be able to close the file and re-open it.
I know this can be accomplished (in theory) by the following, but for
some reason, I don''t have any luck with it. I can''t get the program
to open the file, write to it, close it, then re-open it for more file
writing.
I have some sample code, but it''s all really basic.
// to open
outfile.open(timestamp_file_name.c_str(),ios::out | ios::app);
// to close
outfile.flush();
outfile.close();
Can someone help me solve this problem. I''ve had to make some pretty
interesting side steps to get around this problem before, and now it''s
more of a problem to make the work around.
Any help is appreciated,
Kris
推荐答案
Admin写道:
Admin wrote:
我有一个程序,需要做一些事情。
首先,文件需要不断更新新信息。
因为我这样做的功能并不总是在运行,我需要
才能关闭提交并重新打开它。
我知道这可以通过以下方式完成(理论上),但是对于
某些原因,我不知道好运。我无法获得程序
打开文件,写入,关闭它,然后重新打开它以获得更多文件
写作。
我有一些示例代码,但它们都非常基本。
//打开
outfile.open (timestamp_file_name.c_str(),ios :: out | ios :: app);
//关闭
outfile.flush();
outfile.close();
有人可以帮我解决这个问题。为了解决这个问题,我不得不做一些非常有趣的小步骤,现在它已经成为了解决问题的更多问题。 。
感谢任何帮助,
Kris
I have a program that needs to have a couple things happen.
First, the file needs to constantly be updated with new information.
Because the function in which I do this is not always running, I need
to be able to close the file and re-open it.
I know this can be accomplished (in theory) by the following, but for
some reason, I don''t have any luck with it. I can''t get the program
to open the file, write to it, close it, then re-open it for more file
writing.
I have some sample code, but it''s all really basic.
// to open
outfile.open(timestamp_file_name.c_str(),ios::out | ios::app);
// to close
outfile.flush();
outfile.close();
Can someone help me solve this problem. I''ve had to make some pretty
interesting side steps to get around this problem before, and now it''s
more of a problem to make the work around.
Any help is appreciated,
Kris
这适用于我:
#include< fstream>
int main()
{
std :: ofstream out;
out.open(" kris.txt",std :: ios_base :: out |
std: :ios_base :: app);
out<< hello \ n;
out.flush();
out.close();
//为我工作,并且没有打电话给()
// out.clear();
out.open(" kris.txt" ,std :: ios_base :: out |
std :: ios_base :: app);
out<< bye \ n;
out.flush();
out.close();
返回0;
}
我跑了5次,最后的''kris.txt''包含:
你好
再见
你好
再见
你好
再见
你好
再见
This worked for me:
#include <fstream>
int main( )
{
std::ofstream out;
out.open("kris.txt", std::ios_base::out |
std::ios_base::app);
out << "hello\n";
out.flush();
out.close();
// worked for me both with, and without calling clear()
// out.clear();
out.open("kris.txt", std::ios_base::out |
std::ios_base::app);
out << "bye\n";
out.flush();
out.close();
return 0;
}
I ran it 5 times, and the final ''kris.txt'' contained:
hello
bye
hello
bye
hello
bye
hello
bye
Larry Smith写道:
Larry Smith wrote:
out.flush();
out.close();
out.flush();
out.close();
在这种情况下是否需要调用flush()?如果需要,我认为close()叫
flush()。
Nate
Is it necessary to call flush() in this case? I thought close() called
flush() if needed.
Nate
Larry Smith写道:
Larry Smith wrote:
Admin写道:
Admin wrote:
我有一个程序需要有几件事情发生。
首先,文件需要不断更新新信息。
因为我这样做的功能是并不总是在运行,我需要
才能关闭文件并重新打开它。
我知道这可以通过以下方式实现(理论上)以下,但是对于
某些原因,我没有运气。我无法获得程序
打开文件,写入,关闭它,然后重新打开它以获得更多文件
写作。
我有一些示例代码,但它们都非常基本。
//打开
outfile.open (timestamp_file_name.c_str(),ios :: out | ios :: app);
//关闭
outfile.flush();
outfile.close();
有人可以帮我解决这个问题。为了解决这个问题,我不得不做一些非常有趣的小步骤,现在它已经成为了解决问题的更多问题。 。
感谢任何帮助,
Kris
I have a program that needs to have a couple things happen.
First, the file needs to constantly be updated with new information.
Because the function in which I do this is not always running, I need
to be able to close the file and re-open it.
I know this can be accomplished (in theory) by the following, but for
some reason, I don''t have any luck with it. I can''t get the program
to open the file, write to it, close it, then re-open it for more file
writing.
I have some sample code, but it''s all really basic.
// to open
outfile.open(timestamp_file_name.c_str(),ios::out | ios::app);
// to close
outfile.flush();
outfile.close();
Can someone help me solve this problem. I''ve had to make some pretty
interesting side steps to get around this problem before, and now it''s
more of a problem to make the work around.
Any help is appreciated,
Kris
这适用于我:
#include< fstream>
int main()
{
std :: ofstream out;
out.open(" kris.txt",std :: ios_base :: out |
std: :ios_base :: app);
out<< hello \ n;
out.flush();
out.close();
//为我工作,并且没有打电话给()
// out.clear();
out.open(" kris.txt" ,std :: ios_base :: out |
std :: ios_base :: app);
out<< bye \ n;
out.flush();
out.close();
返回0;
}
我跑了5次,最后的''kris.txt''包含:
你好
再见
你好
再见
你好
再见
你好
bye
This worked for me:
#include <fstream>
int main( )
{
std::ofstream out;
out.open("kris.txt", std::ios_base::out |
std::ios_base::app);
out << "hello\n";
out.flush();
out.close();
// worked for me both with, and without calling clear()
// out.clear();
out.open("kris.txt", std::ios_base::out |
std::ios_base::app);
out << "bye\n";
out.flush();
out.close();
return 0;
}
I ran it 5 times, and the final ''kris.txt'' contained:
hello
bye
hello
bye
hello
bye
hello
bye
使用std :: ios_base :: out |
With the std::ios_base::out |
std :: ios_base :: app);
std::ios_base::app);
什么是ios_base?我以前没见过。
What is the ios_base? I''ve not seen this before.
这篇关于使用C ++编写文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!