c++ - 我的程序没有从文件 :/输出任何内容

标签 c++ input ofstream

我正在做以下练习:

enter image description here

我的代码:

#include <string>
#include <fstream>
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    ifstream inFile;
    ofstream outFile;

    double currentSalary, increaseRate, updatedSalary;
    string firstName, lastName;

    inFile.open ("Data.txt");
    outFile.open("Output.dat");
    outFile << fixed << showpoint << setprecision(2);
    inFile >> lastName >> firstName;
    inFile >> currentSalary >> increaseRate;
    updatedSalary = currentSalary * (1 + increaseRate / 100);
    outFile << firstName << " " << lastName<< " " << updatedSalary << endl;
    inFile >> lastName >> firstName;
    inFile >> currentSalary >> increaseRate;
    updatedSalary = currentSalary * (1 + increaseRate / 100);

    outFile << firstName << " " << lastName<< " " << updatedSalary << endl;
    inFile >> lastName >> firstName;
    inFile >> currentSalary >> increaseRate;
    updatedSalary = currentSalary * (1 + increaseRate / 100);
    outFile << firstName << " " << lastName<< " " << updatedSalary << endl;

    system("PAUSE");
    return 0;
}

但是当我用 MS VS 调试它时..它只是说“按任意键继续...”

我在哪里添加 Data.txt 文件?

最佳答案

好吧,鉴于您没有向屏幕输出任何内容,我一点也不惊讶您看到的就是这些。

如果我是你,我会查看 Output.dat 文件,看它是否正在写入任何内容。

如果您在该文件中看不到任何内容,则可能是因为您在运行的目录中没有 Data.txt 文件。对于 VS,这通常位于解决方案目录内某处的 bindebug 目录下。

您可以通过将 system("cd"); 放在代码的开头并运行它来找出是哪个目录。

关于c++ - 我的程序没有从文件 :/输出任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4952785/

相关文章:

javascript - jquery 表单验证需要一组输入中的一个并且要求输入是一个整数

android - 如何在 Canvas 上获取文本输入?

c++ - 重复使用相同的 ofstream 对象

c++ - 如何封装两个流缓冲区

c++ - 从 query_string 中读取值

c++ - 基本 C++ 递归

c - 从 c 中的用户输入中读取 2 个整数

c++ - 如何在 C++ 中重定向断言输出?

c++ - 什么是类型安全以及 "type safe"替代方案是什么?

c++ - 函数的 const 参数是否自动通过引用传递?