【问题标题】:Visual studio does not create output file without any error messageVisual Studio 不会创建没有任何错误消息的输出文件
【发布时间】:2021-02-23 10:54:32
【问题描述】:

我是计算机科学专业的,我有一个项目需要使用 c++ 创建输出文件。编码是正确的,但视觉工作室没有为我创建输出文件。为了确保不是拼写错误,我使用 C++ 在线编译器测试了我的代码,并且代码正在使用文件输出文件。我确实要求我的教授帮助我解决这个问题,他在最后测试了我的代码,在他的存储库中创建了文件输出,但不知何故我的 Visual Studio 没有创建输出文件,并且代码运行良好,到目前为止没有错误消息.我尝试以管理员身份运行它,重新启动我的笔记本电脑并卸载然后重新安装 Visual Studio,但没有任何效果。任何帮助将不胜感激。

非常感谢。

如果有人想知道,这是我的代码

#include <iostream>
#include <math.h>
#include <vector>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;

ifstream fin("input.txt");// Open the file input
ofstream fout("output_1.txt");// create the file output.txtx
ofstream fout_Sor("output_sorted.txt");//create the file output_sorted

double calcDistance(double x1, double y1, double z1, double x2, double y2, double z2)
{
    // A function to calculate the distance and return it

    double mathX = pow(x1 - x2, 2);//calculate with the power to 2
    double mathY = pow(y1 - y2, 2);
    double mathZ = pow(z1 - z2, 2);

    double calDistance = sqrt(mathX + mathY + mathZ); // squart the distance

    return calDistance;
}

int main() {
    double x1, x2, y1, y2, z1, z2;
    vector<double> distance;
    


    while (fin.good()) // REad the file which doesnt know how man yvalua
    {
        fin >> x1 >> y1 >> z1 >> x2 >> y2 >> z2; // Save it in side variable
        distance.push_back(calcDistance(x1, y1, z1, x2, y2, z2)); // call the fucntion to calculate and save it into vector


    }

    for (int i = 0; i < distance.size(); i++)// Save each value of the vector to the output file
    {
        fout << distance.at(i) << endl;
    }
    
    sort(distance.begin(), distance.end()); // sort the vector

    for (int i = 0; i < distance.size(); i++)
    {
        fout_Sor << distance.at(i) << endl;// Save the sort value into the output_sort file
    }

    // close file
    fout.close();
    fin.close();
    fout_Sor.close();
    return 0;

}

【问题讨论】:

  • 尝试在解决方案目录中搜索该文件。 IIRC,MSVS 使用源文件所在的文件夹作为程序的工作目录。
  • 调试器中的默认工作目录应该是$(ProjectDir),这是一个指向包含项目的文件夹的 Visual Studio 变量。这适用于所有 Visual Studio 版本,而不是“Visual Studio Code”,它是一个完全不同的程序,具有相似的名称。
  • 我的超能力告诉我,这些文件对象无法打开文件(即找不到文件、错误的目录等)。将复杂对象(如文件)的实例声明为全局变量确实不是一个好主意。在您的 main 函数中构造这些对象,并在文件无法打开时打印错误。
  • 无关:while (fin.good()) 有缺陷。逻辑看起来像 1. 如果流是好的,2. 从流中读取,3. 使用从流中读取的数据 4. 转到 1. 如果流被渲染为无效,它将在点 2,在点 1 的检查之后,允许第 3 点继续处理无效数据。更喜欢while (fin &gt;&gt; x1 &gt;&gt; y1 &gt;&gt; z1 &gt;&gt; x2 &gt;&gt; y2 &gt;&gt; z2) { distance.push_back(...); } 之类的东西,因为它颠倒了第 1 点和第 2 点的顺序。只有从文件中读取所有变量时,循环体才会进入。

标签: c++ visual-studio-2019


【解决方案1】:

与所有其他文件打开函数一样,ifstream&amp; ofstream 类在提供相对路径时使用当前目录。

  1. 首先建议你使用绝对路径ofstream fout("D:\\testfolder\\output_1.txt");来测试文件是否被创建。

  2. 找到你的项目所在的目录,看看有没有.txts。

  3. 如果没有找到,建议你在整个磁盘中搜索txt这两个文件。

如果还是不行,建议你重置VS设置Tools-&gt;Import and Export Settings-&gt;Reset all settings

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多