Chinaunix首页 | 论坛 | 博客
  • 博客访问: 433565
  • 博文数量: 138
  • 博客积分: 4114
  • 博客等级: 上校
  • 技术积分: 1341
  • 用 户 组: 普通用户
  • 注册时间: 2007-10-14 20:41
文章分类

全部博文(138)

文章存档

2014年(1)

2013年(2)

2012年(78)

2011年(13)

2010年(34)

2009年(10)

我的朋友

分类: LINUX

2011-10-14 17:07:32

本以为 对c++够熟悉了,但是还是不行,今天遇到的一个问题,让我搞了十几分钟
  1. #include <iostream>
  2. #include <sstream>
  3. #include <fstream>

  4. using namespace std;

  5. int main()
  6. {
  7. // ofstream out("test.txt", ios_base::trunc | ios_base::out | ios_base::app);
  8.         ofstream out("test.txt", ios_base::trunc | ios_base::out );
  9.         string str("nihao");
  10.         stringstream ss;
  11.         ss << str;
  12.         ss << "1213213213213\n";
  13.         ss << "this is ss\n";
  14.         out << ss.str();
  15. }


如果用注释的那一行,而不是用注释下面那一行的话,你是永远也看不到'test.txt'的
在  cplusplus reference 上看到的是
app   (append) Set the stream's position indicator to the end of the stream before each output operation.
trunc  (truncate) Any current content is discarded, assuming a length of zero on opening.

然后我天真的认为  trunc 用于文件打开的时候,app用于打开后的流操作的模式
这样 就可以在有旧文件的情况下,删除旧文件的内容,添加新内容
但是结果却不是这样的
trunc | app 导致的结果是没有任何输出
其实我想要的需求  只要  一个  trunc就可以了
但是我不知道 为什么加了  app会什么都没有, 有知道的朋友 给我说一声

另外还发现一个很有意思的东西
  1.    1. stringstream ss;
  2.    2. cve_b_id.append("/cve_b_file.txt");
  3.    3. ofstream of( cve_b_id.c_str(), ios_base::out | ios_base::trunc );   4.

  4.    5. for(temp_node = list_head_p->next; temp_node != NULL; temp_node = temp_node->next)
  5.    6. {
  6.    7. ss << "[";
  7.    8. ss << temp_node->file_name;
  8.    9. ss << "]\n";
  9.   10. ss << "cve_id:\n";
  10.   11. ss << temp_node->this_plugin_info.script_cve_id;
  11.   12. ss << "\n";
  12.   13. ss << "b_id:\n";
  13.   14. ss << temp_node->this_plugin_info.script_bugtraq_id;
  14.   15. ss << "\n";
  15.   16. of << ss.str();
  16.   17. cout << ss.str();
  17.   18. }
  18.   19. ss.str("");
  19.   20. of.close();
如果将 第19行的内容加到  17行下面的话,你的文件中只有最有一个节点的信息
至于为诶什么 会这样, 我还没找到答案
阅读(735) | 评论(0) | 转发(0) |
0

上一篇:makefile中的变量

下一篇:编程名言

给主人留下些什么吧!~~