How to skip blank spaces when reading in a file c++
up vote
0
down vote
favorite
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 << ' ';
c++ c++11
add a comment |
up vote
0
down vote
favorite
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 << ' ';
c++ c++11
6
Why is iostream::eof inside a loop condition considered wrong?
– Swordfish
Nov 10 at 19:56
btw, your code won't compile.
– Swordfish
Nov 10 at 20:18
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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 << ' ';
c++ c++11
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 << ' ';
c++ c++11
c++ c++11
asked Nov 10 at 19:55
K T
112
112
6
Why is iostream::eof inside a loop condition considered wrong?
– Swordfish
Nov 10 at 19:56
btw, your code won't compile.
– Swordfish
Nov 10 at 20:18
add a comment |
6
Why is iostream::eof inside a loop condition considered wrong?
– Swordfish
Nov 10 at 19:56
btw, your code won't compile.
– Swordfish
Nov 10 at 20:18
6
6
Why is iostream::eof inside a loop condition considered wrong?
– Swordfish
Nov 10 at 19:56
Why is iostream::eof inside a loop condition considered wrong?
– Swordfish
Nov 10 at 19:56
btw, your code won't compile.
– Swordfish
Nov 10 at 20:18
btw, your code won't compile.
– Swordfish
Nov 10 at 20:18
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
#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 :(nn";
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_locations 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.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242849%2fhow-to-skip-blank-spaces-when-reading-in-a-file-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
#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 :(nn";
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_locations 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.
add a comment |
up vote
0
down vote
#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 :(nn";
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_locations 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.
add a comment |
up vote
0
down vote
up vote
0
down vote
#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 :(nn";
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_locations 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.
#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 :(nn";
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_locations 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.
edited Nov 10 at 20:37
answered Nov 10 at 20:30
Swordfish
1
1
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242849%2fhow-to-skip-blank-spaces-when-reading-in-a-file-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
6
Why is iostream::eof inside a loop condition considered wrong?
– Swordfish
Nov 10 at 19:56
btw, your code won't compile.
– Swordfish
Nov 10 at 20:18