【问题标题】:Writing out to a file in C++在 C++ 中写入文件
【发布时间】:2013-12-20 08:18:05
【问题描述】:

这是我的代码:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()

{

string fname,lname;
double pincrease //pay increase percentage,pay;
ifstream infile;
ofstream outfile;

infile.open("C:\\Users\\Connor\\Desktop");
outfile.open("C:\\Users\\Connor\\Desktop");

while(!infile.eof())
{
    infile>>lname>>fname>>pay>>pincrease;
    pay = pay*(pay*pincrease);
    outfile<<fname<<" "<<lname<<" "<<pay<<"\n";
    cin.clear();
}

infile.close();
outfile.close();

}

这是我infile的内容:

Miller Andrew 65789.87 5
Green Sheila 75892.56 6
Sethi Amit 74900.50 6.1

信息格式为Last Name:First Name:Pay:Pay Increase Percentage

我写信给outfile时,名字和姓氏的订单交换和排除加薪百分比是故意的。

我正在尝试读取infile 的内容,对其进行修改,然后将其写入outfile

但是,当我执行代码时,我很确定这是一个无限循环,但我不确定如何修复它。

【问题讨论】:

  • 您是否正在读取和写入桌面文件夹?
  • @mgamba 是的。为了方便,我在桌面上有两个单独的文本文件,分别命名为 infile 和 outfile。
  • 看起来infileoutfile 指向Desktop 文件夹本身。 lname 声明也被注释掉了。
  • @mgamba 是的,注释掉是个意外。将 cmets 放在帖子中以进行澄清,但我做错了,所以我把它们删除了。

标签: c++ file-io


【解决方案1】:

这些语句都不可能打开文件:

infile.open("C:\\Users\\Connor\\Desktop");
outfile.open("C:\\Users\\Connor\\Desktop");

相反,他们尝试(并且可能失败)打开您的桌面文件夹。相反,您可能想要更多类似的东西:

infile.open("C:\\Users\\Connor\\Desktop\\infile");
outfile.open("C:\\Users\\Connor\\Desktop\\outfile");

当然,最后的infileoutfile 应该替换为实际的文件名。

您可以通过检查infile.is_open()outfile.is_open() 来测试您的文件是否打开成功。您可以添加明确的 if 语句来测试:

if (!infile.is_open())
    // report/handle that you couldn't open input file

if (!outfile.is_open())
    // report/handle that you couldn't open output file

对于你的主循环,你不应该像你那样测试eof。相反,使用这样的东西:

while(infile>>lname>>fname>>pay>>pincrease)
{
    pay = pay*(pay*pincrease);
    outfile<<fname<<" "<<lname<<" "<<pay<<"\n";
    cin.clear();
}

以您以前的方式测试 EOF 将尝试在文件末尾之外读取一条额外的记录。

只有在读取尝试失败后才会设置 EOF 标志。因此,您应该在尝试阅读后始终测试 EOF。

标准ifstream 的设置方式使得典型的ifstream 输入操作(即infile &gt;&gt; item)将返回对ifstream 对象的引用。这就是您可以执行infile &gt;&gt; item1 &gt;&gt; item2 &gt;&gt; item3 之类的操作的方式。

当您将它放在循环控件的上下文中(如上)时,ifstream 具有适当的运算符重载,导致它根据读取是否成功告诉while 是否继续循环。

其他人已经解释过在其他地方足够好地超载魔法。有关循环终止魔术的更多信息:Why istream object can be used as a bool expression?

【讨论】:

  • 非常感谢。尤其是链接。
  • ihmo rathe 不切实际的语法,当您必须根据之前读取的数据来决定要读取的数据类型时。
  • @ValentinHeinitz:当然,这绝对是一个语言设计问题。标准ifstream 不适合更通用的语言标记器/语法。但如果这就是你所需要的,那么你应该构建一些不同于ifstream 提供的基本工具的东西。
【解决方案2】:

替换这个

infile.open("C:\\Users\\Connor\\Desktop");
outfile.open("C:\\Users\\Connor\\Desktop");

有了这个

infile.open("C:\\Users\\Connor\\infile.txt");
outfile.open("C:\\Users\\Connor\\outfile.txt");

由于两个原因,您发布的代码无法编译。一、变量pay没有被声明二、你注释掉了以下代码的终止。

double pincrease //pay increase percentage,pay;

试试下面的代码。希望对你有帮助

            int main()

            {

                string fname,lname;
                double pincrease; //pay increase percentage,pay;
                double pay;
                ifstream infile;
                ofstream outfile;

                infile.open("C:\\Users\\Connor\\infile.txt");
                outfile.open("C:\\Users\\Connor\\outfile.txt");

                while(!infile.eof())
                {
                    infile>>lname>>fname>>pay>>pincrease;
                    pay = pay*(pay*pincrease);
                    outfile<<std::setprecision(2)<<std::showpoint << std::fixed;;
                    outfile<<fname<<" "<<lname<<" "<<pay<<"\n";
                    cin.clear();
                }

                infile.close();
                outfile.close();

            }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-26
    • 2015-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多