gpt4 book ai didi

c++ - ofstream 在询问其内容之前创建文件

转载 作者:太空宇宙 更新时间:2023-11-04 13:05:35 25 4
gpt4 key购买 nike

<分区>

我正在处理的项目的一部分将有关 3D 打印机的信息保存到文本文件中。更具体地说,它应该:

  • 检查文件是否已经存在
  • 如果存在,继续
  • 如果不存在,请用户输入所需的数据

我的问题是该程序似乎跳过了最后一步,而是选择创建一个空文本文件并继续前进而不询问用户他们的数据。这是似乎导致问题的 block :

int configCheck() {

if (std::ifstream(configName)) {

std::cout << "Configuration file already exists." << std::endl;

}
std::ofstream file(configName);
if (!file) {

std::cout << "Configuration file not found." << std::endl;

// ask for machine settings

std::cout << "Machine Configuration" << std::endl;
std::cout << "---------------------" << std::endl;
std::cout << "Machine Width (mm): ";
std::cin >> xLim;
std::cout << std::endl;
std::cout << "Machine Length (mm): ";
std::cin >> yLim;
std::cout << std::endl;
std::cout << "Machine Height (mm): ";
std::cin >> zLim;
std::cout << std::endl;
std::cout << "Nozzle Size (mm): ";
std::cin >> nozzleDia;
std::cout << std::endl;
std::cout << "Filament Size (mm) ";
std::cin >> filDia;
std::cout << std::endl;

// make and fill a configuration file

std::cout << "Creating configuration file..." << std::endl;
std::ofstream config;
config << xLim << std::endl;
config << yLim << std::endl;
config << zLim << std::endl;
config << nozzleDia << std::endl;
config << filDia << std::endl;
config.close();

}
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com