c++ - 读取文件c ++时如何跳过空格
问题描述
这是确切输入文件的代码共享链接:https ://codeshare.io/5DBkgY
好的,如您所见,在 8 和 ROD 之间有 2 个空行(或制表符)。我将如何跳过该程序并继续该程序?我试图将每一行放入 3 个向量中(因此键、灯和杆放入一个向量等)。这是我的代码(但它不会跳过空行)。:
#include <string>
#include <iostream>
#include <sstream>
#include <vector>
#include <fstream>
using namespace std;
int main() {
ifstream objFile;
string inputName;
string outputName;
string header;
cout << "Enter image file name: ";
cin >> inputName;
objFile.open(inputName);
string name;
vector<string> name2;
string description;
vector<string> description2;
string initialLocation;
vector<string> initialLocation2;
string line;
if(objFile) {
while(!objFile.eof()){
getline(objFile, line);
name = line;
name2.push_back(name);
getline(objFile, line);
description = line;
description2.push_back(description);
getline(objFile, line);
initialLocation = line;
initialLocation2.push_back(initialLocation);
} else {
cout << "not working" << endl;
}
for (std::vector<string>::const_iterator i = name2.begin(); i != name2.end(); ++i)
std::cout << *i << ' ';
for (std::vector<string>::const_iterator i = description2.begin(); i != description2.end(); ++i)
std::cout << *i << ' ';
for (std::vector<string>::const_iterator i = initialLocation2.begin(); i != initialLocation2.end(); ++i)
std::cout << *i << ' ';
解决方案
#include <cstddef> // std::size_t
#include <cctype> // std::isspace()
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
bool is_empty(std::string const &str)
{
for (auto const &ch : str)
if (!std::isspace(static_cast<char unsigned>(ch)))
return false;
return true;
}
int main()
{
std::cout << "Enter image file name: ";
std::string filename;
std::getline(std::cin, filename); // at least on Windows paths containing whitespace
// are valid.
std::ifstream obj_file{ filename }; // define variables as close to where they're used
// as possible and use the ctors for initialization.
if (!obj_file.is_open()) { // *)
std::cerr << "Couldn't open \"" << filename << "\" for reading :(\n\n";
return EXIT_FAILURE;
}
std::vector<std::string> name;
std::vector<std::string> description;
std::vector<std::string> initial_location;
std::string line;
std::vector<std::string> *destinations[] = { &name, &description, &initial_location };
for (std::size_t i{}; std::getline(obj_file, line); ++i) {
if (is_empty(line)) { // if line only consists of whitespace
--i;
continue; // skip it.
}
destinations[i % std::size(destinations)]->push_back(line);
}
for (auto const &s : name)
std::cout << s << '\n';
for (auto const &s : description)
std::cout << s << '\n';
for (auto const &s : initial_location)
std::cout << s << '\n';
}
... initial_location
s 看起来像整数,虽然。
*) 如果发生不好的事情,最好提前退出。代替
if (obj_file) {
// do stuff
}
else {
// exit
}
-->
if(!obj_file)
// exit
// do stuff
使您的代码更易于阅读,并消除了大多数部分的一级缩进。
推荐阅读
- django - Django:如何将值从列表模板传递到视图,静态 url
- c# - 如何在 C# 中以编程方式将 WCF 类库项目安装为 Windows 服务
- google-cloud-platform - 为什么我的 GCP 负载均衡器证书在配置时卡住了?
- android - 在 Android Studio 3.2.1 的依赖解析中包含配置后,无法更改配置的依赖关系
- c# - WinForms 字体对话框样式被 WPF 应用程序样式覆盖
- python - Python:如何获取 IF 语句以从用户输入中识别关键字?
- javascript - 如果不选择选择框需要更改样式
- laravel - Laravel Nova自定义工具创建:vue组件不刷新
- jquery - 用于不允许仅数字或仅特殊字符的 Jquery 正则表达式
- google-cloud-dataflow - 如何在 Apache Beam 步骤中并行化 HTTP 请求?