首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在读取文件c++时跳过空格

如何在读取文件c++时跳过空格
EN

Stack Overflow用户
提问于 2018-11-11 03:55:35
回答 1查看 860关注 0票数 0

好的,正如你所看到的,​在8和ROD之间有2个空行(或制表符)。我该如何跳过这一步,继续这个程序呢?我试着把每一行放到3个向量中(所以关键字,灯,和杆放入一个向量等)。以下是我的代码(但它没有跳过空行):

代码语言:javascript
运行
复制
#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 << ' ';
EN

回答 1

Stack Overflow用户

发布于 2018-11-11 04:30:15

代码语言:javascript
运行
复制
#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看起来像整数。

*)如果发生了不好的事情,最好提前退出。而不是

代码语言:javascript
运行
复制
if (obj_file) {
    // do stuff
}
else {
    // exit
}

-->

代码语言:javascript
运行
复制
if(!obj_file)
    // exit

// do stuff

使您的代码更容易阅读,并为大多数部分取消了一级缩进。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53242849

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档