简体   繁体   中英

How to skip blank spaces when reading in a file c++

Here is the codeshare link of the exact input file: https://codeshare.io/5DBkgY

Ok, as you can see, ​there are 2 blank lines, (or tabs) between 8 and ROD. How would I skip that and continue with the program? I am trying to put each line into 3 vectors (so keys, lamp, and rod into one vector etc). Here is my code (but it does not skip the blank line).:

#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 look like integers, though.

*) Better early exit if something bad happens. Instead of

if (obj_file) {
    // do stuff
}
else {
    // exit
}

-->

if(!obj_file)
    // exit

// do stuff

makes your code easier to read and takes away one level of indentation for the most parts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM