简单的 C++写入文件:什么也没有写入!应该很容易,但我很难过
非常感谢你对我的帮助!每个人都那么快,那么优秀!再次感谢!
在我测试此代码后,发生的情况是没有数据写入我的文件。只出现一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试一些测试:
stdout
而不是文件,会发生什么?"C:\\out.htm"
),会发生什么情况?open
,myfile << “测试”;
,关闭
?A few tests to try:
stdout
instead of to a file?"C:\\out.htm"
?open
,myfile << "test";
,close
?[编辑] 我在十多年前的 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.
你的代码在我使用 VS 2005 的机器上运行良好。这个表达式给出了一个错误:
转换为
哪个更好,因为你正在使用字符串库,并且方法可以检查字符串是否为空,为什么不使用它。
Your code runs fine on my machine using VS 2005. This expression was giving an error :
Converted to
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.
要检查未打开的流,请使用:
其余看起来都不错,只是文件路径错误,或者无法创建文件。
To check for a stream that was not opened, use:
The rest looks good, its just that the file path is wrong, or the file cannot be created.