将字符串对象向量输出到文件

发布于 2024-09-09 11:08:22 字数 531 浏览 7 评论 0原文

我正在尝试将字符串对象向量输出到文件中。但是,我的代码只有 输出每个字符串的前两个元素。

下面这段代码写道:

1
1

到一个文件。相反:

2009 年 7 月 1 日
2010 年 7 月 1 日

这就是我所需要的。

ofstream file("dates.out");  

vector<string> Test(2); 

Test[0] = "01-Jul-09"; 
Test[1] = "01-Jul-10"; 

for(unsigned int i=0; i<Test.size(); i++)    
     file << Test[i] << endl;

file.close();

我不清楚可能会出现什么问题,因为我使用了字符串对象 之前在类似的情况下。

欢迎任何帮助!

I'm trying to output a vector of string objects to a file. However, my code only
outputs the first two elements of each string.

The piece of code below writes:

1
1

to a file. Rather then:

01-Jul-09
01-Jul-10

which is what I need.

ofstream file("dates.out");  

vector<string> Test(2); 

Test[0] = "01-Jul-09"; 
Test[1] = "01-Jul-10"; 

for(unsigned int i=0; i<Test.size(); i++)    
     file << Test[i] << endl;

file.close();

Is not clear to me what could be going wrong as I have used string objects
before in similar contexts.

Any help would be welcome!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我一直都在从未离去 2024-09-16 11:08:22

正如已经观察到的,代码看起来很好,所以:

  • 您在程序运行后是否正在查看正确的 dates.out ?您是否验证了正在查看的文件的日期/时间以确保它不是以前的数据?
  • 您有写入该文件的权限吗?也许您的程序无法覆盖现有数据。
  • 您向我们展示了所有重要的代码吗?还有其他我们需要了解的函数调用吗? Marcelo/ereOn 的答案中的代码是否会产生与您的问题相同的问题?
  • 您确定正在运行您认为的二进制文件吗? (可能是路径问题)。

As already observed, the code appears fine, so:

  • Are you looking at the right dates.out after your program runs? Did you verify the date/time on the file you're looking at to make sure it isn't previous data?
  • Do you have permission to write to the file? Perhaps your program is failing to overwrite existing data.
  • Did you show us ALL the important code? Are there any other function calls we need to know about? Does the code in Marcelo/ereOn's answers produce the same problem as in your question?
  • Are you sure that you're running the binary you think you are? (PATH issues possibly).
抱猫软卧 2024-09-16 11:08:22

以下代码按预期工作:

marcelo@macbookpro-1:~/play$ cat dateout.cc
#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

int main()
{
  ofstream file("dates.out");

  vector<string> Test(2);

  Test[0] = "01-Jul-09";
  Test[1] = "01-Jul-10";

  for(unsigned int i=0; i<Test.size(); i++)
    file << Test[i] << endl;
  file.close();
}
marcelo@macbookpro-1:~/play$ make dateout && ./dateout
g++     dateout.cc   -o dateout
marcelo@macbookpro-1:~/play$ cat dates.out 
01-Jul-09
01-Jul-10
marcelo@macbookpro-1:~/play$ 

The following code works as expected:

marcelo@macbookpro-1:~/play$ cat dateout.cc
#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

int main()
{
  ofstream file("dates.out");

  vector<string> Test(2);

  Test[0] = "01-Jul-09";
  Test[1] = "01-Jul-10";

  for(unsigned int i=0; i<Test.size(); i++)
    file << Test[i] << endl;
  file.close();
}
marcelo@macbookpro-1:~/play$ make dateout && ./dateout
g++     dateout.cc   -o dateout
marcelo@macbookpro-1:~/play$ cat dates.out 
01-Jul-09
01-Jul-10
marcelo@macbookpro-1:~/play$ 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文