简单的 C++写入文件:什么也没有写入!应该很容易,但我很难过

发布于 2024-09-07 14:52:52 字数 2510 浏览 4 评论 0原文

非常感谢你对我的帮助!每个人都那么快,那么优秀!再次感谢!

在我测试此代码后,发生的情况是没有数据写入我的文件。只出现一个 0。

我做错了什么?

void CreateHtmlFile(string myMessages[])
{
  int i = 0;
  int emptyarray = 0;
  int myEmptyCounter = 0;
  int emptyArrayCounter = 0;
  string myEmpty;
  ofstream myfile;

  myfile.open ("C:\\Users\\Andrews\\Documents\\Visual Studio 2010\\Projects\\computerclass\\Debug\\outages.htm", ios::out);
   if(!myfile) // is there any error?
    {
       cout << "Error opening the file! Aborting…\n";
       exit(1);
    }
  myfile << "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>\n";
  myfile << "<html>\n";
  myfile << "<head>\n";
  myfile << "<title>Livermore Readerboard</title>\n";
  myfile << "<style type='text/css'>\n";
  myfile << "table {font-family:Helvetica Narrow, sans-serif;font-size:42px;}\n";
  myfile << "body\n";
  myfile << "{\n";
  myfile << "text-align: center;\n";
  myfile << "background: #000000;\n";
  myfile << "color:#00FF00;\n";
  myfile << "}\n";
  myfile << "#container\n";
  myfile << "{\n";
  myfile << "margin-left: auto;\n";
  myfile << "margin-right: auto;\n";
  myfile << "width: 93em;\n";
  myfile << "text-align: left;\n";
  myfile << "</style>\n";
  myfile << "<META HTTP-EQUIV= \"refresh\" content= \"5;URL=readerboard.htm\">\n";
  myfile << "</head>\n";
  myfile << "<body>\n";
  myfile << "<div id='container'>\n";
  myfile << "<table class='Design6' border=1 cellpading=1 cellspacing=0>\n";

  myEmpty.clear();
while (i != 10)
{
       if (myMessages[i] != "")
       {
       myfile << "<tr>\n";
       myfile << "<td><b>" << myMessages[i] << "</b></td>\n";
       myfile << "</tr>\n";
       i++;
       }
       else
       {
              i++;
              emptyArrayCounter++;
       }
}

if (emptyArrayCounter == 9)
{
       //empty array so insert default message
       myfile << "<tr>\n";
       myfile << "<td><b>" << "No Outages" << "</b></td>\n";
       myfile << "</tr>\n";
}

  myfile << "</div>\n";
  myfile << "</body>\n";
  myfile << "</html>\n";

  myfile.close();
}

Thank you so much for helping me! Everyone is so fast and excellent! Thanks again!

What is happening is no data is being written to my file, after I test this code. Just a 0 appears.

What am I doing wrong?

void CreateHtmlFile(string myMessages[])
{
  int i = 0;
  int emptyarray = 0;
  int myEmptyCounter = 0;
  int emptyArrayCounter = 0;
  string myEmpty;
  ofstream myfile;

  myfile.open ("C:\\Users\\Andrews\\Documents\\Visual Studio 2010\\Projects\\computerclass\\Debug\\outages.htm", ios::out);
   if(!myfile) // is there any error?
    {
       cout << "Error opening the file! Aborting…\n";
       exit(1);
    }
  myfile << "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>\n";
  myfile << "<html>\n";
  myfile << "<head>\n";
  myfile << "<title>Livermore Readerboard</title>\n";
  myfile << "<style type='text/css'>\n";
  myfile << "table {font-family:Helvetica Narrow, sans-serif;font-size:42px;}\n";
  myfile << "body\n";
  myfile << "{\n";
  myfile << "text-align: center;\n";
  myfile << "background: #000000;\n";
  myfile << "color:#00FF00;\n";
  myfile << "}\n";
  myfile << "#container\n";
  myfile << "{\n";
  myfile << "margin-left: auto;\n";
  myfile << "margin-right: auto;\n";
  myfile << "width: 93em;\n";
  myfile << "text-align: left;\n";
  myfile << "</style>\n";
  myfile << "<META HTTP-EQUIV= \"refresh\" content= \"5;URL=readerboard.htm\">\n";
  myfile << "</head>\n";
  myfile << "<body>\n";
  myfile << "<div id='container'>\n";
  myfile << "<table class='Design6' border=1 cellpading=1 cellspacing=0>\n";

  myEmpty.clear();
while (i != 10)
{
       if (myMessages[i] != "")
       {
       myfile << "<tr>\n";
       myfile << "<td><b>" << myMessages[i] << "</b></td>\n";
       myfile << "</tr>\n";
       i++;
       }
       else
       {
              i++;
              emptyArrayCounter++;
       }
}

if (emptyArrayCounter == 9)
{
       //empty array so insert default message
       myfile << "<tr>\n";
       myfile << "<td><b>" << "No Outages" << "</b></td>\n";
       myfile << "</tr>\n";
}

  myfile << "</div>\n";
  myfile << "</body>\n";
  myfile << "</html>\n";

  myfile.close();
}

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

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

发布评论

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

评论(4

为你拒绝所有暧昧 2024-09-14 14:52:52

尝试一些测试:

  • 如果将输出发送到 stdout 而不是文件,会发生什么?
  • 如果您使用不同的文件路径(例如 "C:\\out.htm"),会发生什么情况?
  • 如果在文件不存在时运行此命令会发生什么?如果您在运行程序之前手动创建(空)输出文件怎么办?
  • 如果将程序简化为简单的 open, myfile << “测试”;关闭
  • 如果您尝试使用 C 样式文件 I/O(fopen、fprintf、fclose)而不是流来运行程序的精简版本,会发生什么情况?

A few tests to try:

  • What happens if you send your output to stdout instead of to a file?
  • What happens if you use a different file path, like "C:\\out.htm"?
  • What happens if you run this when the file doesn't exist? What about if you manually create the (empty) output file before running the program?
  • What happens if you simplify the program down to just a simple open, myfile << "test";, close?
  • What happens if you try to run the abridged version of the program using C-style file I/O (fopen, fprintf, fclose) instead of streams?
瘫痪情歌 2024-09-14 14:52:52

[编辑] 我在十多年前的 MSVC 6 等旧的、损坏的、预标准的编译器中遇到过对显式 flash() 调用的需求。它可能不再是必要的,并且显然是有问题的库实现的解决方法。

我已经尝试了代码(删除输出消息的部分并将输出文件名重命名为我系统上的名称)。输出是正确的。

可能值得尝试写入本地文件只是为了看看会发生什么。您已经检查了打开文件流进行输出是否失败,但您的情况似乎出现了一些奇怪的情况。

[编辑:我可能会因为多管闲事而被否决,但是...]您在给定范围顶部定义所有变量的 C 风格习惯已经过时了。考虑在可以正确初始化变量并且范围更有限的情况下定义变量。现在这可能不是什么大问题,但如果您曾经在任何合理规模的 C 系统上工作并遇到第一个未初始化的变量错误,您将开始鄙视它。每个人都有个人喜好的权利,但客观地说,促进错误的风格是低劣的风格。

[Edit] The need for explicit flush() calls was something I encountered with old, broken, pre-standard compilers like MSVC 6 and earlier over a decade ago. It is probably not necessary any more and apparently was a workaround for problematic library implementations.

I've tried out the code (removing the section to output messages and renaming the output file name to something on my system). The output was correct.

It might be worth trying to write to a local file just to see what happens. You already check for failure to open the file stream for output but there appears to be something odd going on for your case.

[Edit: I'm probably going to get down-voted for being nosy but...] Your C style habits of defining all variables at the top of a given scope are obsolete. Consider defining variables no sooner than where they can be properly initialized and with a more limited scope. It may not be a big deal now, but if you ever work on a C system of any reasonable scale and encounter your first uninitialized variable bugs, you're going to start despising it. Everyone's entitled to their personal preferences, but a style that promotes bugs is an inferior one, objectively speaking.

浅暮の光 2024-09-14 14:52:52

你的代码在我使用 VS 2005 的机器上运行良好。这个表达式给出了一个错误:

myMessages[i] != ""

转换为

!myMessages[i].empty() 

哪个更好,因为你正在使用字符串库,并且方法可以检查字符串是否为空,为什么不使用它。

Your code runs fine on my machine using VS 2005. This expression was giving an error :

myMessages[i] != ""

Converted to

!myMessages[i].empty() 

which is better because you are using the string library and method is there to check if a string is empty or not, why not use it.

你不是我要的菜∠ 2024-09-14 14:52:52

要检查未打开的流,请使用:

if (myfile.bad())

其余看起来都不错,只是文件路径错误,或者无法创建文件。

To check for a stream that was not opened, use:

if (myfile.bad())

The rest looks good, its just that the file path is wrong, or the file cannot be created.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文