Adding words from a text file to a vector c++
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I am trying to add each word from a file to a vector but if I make the size of the vector (500) and I only have 20 words in the file. The size of the vector is still considered 500. How do I fix this?
Am I doing this a bad way? Could this be made simpler?
void loadFile(string fileName)
{
vector<string> fileContents(500);
int p = 0;
ifstream file;
file.open(fileName);
if (!file.is_open()) return;
string word;
while (file >> word)
{
fileContents[p] = word;
p++;
}
for (int i = 0; i < fileContents.size(); i++)
{
cout << fileContents[i] << endl;
}
}
c++ list c++11 vector
add a comment |
I am trying to add each word from a file to a vector but if I make the size of the vector (500) and I only have 20 words in the file. The size of the vector is still considered 500. How do I fix this?
Am I doing this a bad way? Could this be made simpler?
void loadFile(string fileName)
{
vector<string> fileContents(500);
int p = 0;
ifstream file;
file.open(fileName);
if (!file.is_open()) return;
string word;
while (file >> word)
{
fileContents[p] = word;
p++;
}
for (int i = 0; i < fileContents.size(); i++)
{
cout << fileContents[i] << endl;
}
}
c++ list c++11 vector
1
You should usefileContents.push_back(word);
instead offileContents[p] = word;
also changevector<string> fileContents(500);
tovector<string> fileContents;
and get rid ofp
– drescherjm
Nov 27 '18 at 0:03
@drescherjm I tried that but when it goes to print out the file contents it doesn't print out anything? EDIT: NVM. I typed the command wrong.. that worked.. I thought I tried that but I guess I typed it wrong in the first place
– jake
Nov 27 '18 at 0:06
1
@jake Did you get rid of(500)
?
– 0x499602D2
Nov 27 '18 at 0:06
It works now. I think I might have left that in last time I tried that, and maybe that's why it wasn't working. @0x499602D2
– jake
Nov 27 '18 at 0:07
add a comment |
I am trying to add each word from a file to a vector but if I make the size of the vector (500) and I only have 20 words in the file. The size of the vector is still considered 500. How do I fix this?
Am I doing this a bad way? Could this be made simpler?
void loadFile(string fileName)
{
vector<string> fileContents(500);
int p = 0;
ifstream file;
file.open(fileName);
if (!file.is_open()) return;
string word;
while (file >> word)
{
fileContents[p] = word;
p++;
}
for (int i = 0; i < fileContents.size(); i++)
{
cout << fileContents[i] << endl;
}
}
c++ list c++11 vector
I am trying to add each word from a file to a vector but if I make the size of the vector (500) and I only have 20 words in the file. The size of the vector is still considered 500. How do I fix this?
Am I doing this a bad way? Could this be made simpler?
void loadFile(string fileName)
{
vector<string> fileContents(500);
int p = 0;
ifstream file;
file.open(fileName);
if (!file.is_open()) return;
string word;
while (file >> word)
{
fileContents[p] = word;
p++;
}
for (int i = 0; i < fileContents.size(); i++)
{
cout << fileContents[i] << endl;
}
}
c++ list c++11 vector
c++ list c++11 vector
asked Nov 27 '18 at 0:01
jakejake
235
235
1
You should usefileContents.push_back(word);
instead offileContents[p] = word;
also changevector<string> fileContents(500);
tovector<string> fileContents;
and get rid ofp
– drescherjm
Nov 27 '18 at 0:03
@drescherjm I tried that but when it goes to print out the file contents it doesn't print out anything? EDIT: NVM. I typed the command wrong.. that worked.. I thought I tried that but I guess I typed it wrong in the first place
– jake
Nov 27 '18 at 0:06
1
@jake Did you get rid of(500)
?
– 0x499602D2
Nov 27 '18 at 0:06
It works now. I think I might have left that in last time I tried that, and maybe that's why it wasn't working. @0x499602D2
– jake
Nov 27 '18 at 0:07
add a comment |
1
You should usefileContents.push_back(word);
instead offileContents[p] = word;
also changevector<string> fileContents(500);
tovector<string> fileContents;
and get rid ofp
– drescherjm
Nov 27 '18 at 0:03
@drescherjm I tried that but when it goes to print out the file contents it doesn't print out anything? EDIT: NVM. I typed the command wrong.. that worked.. I thought I tried that but I guess I typed it wrong in the first place
– jake
Nov 27 '18 at 0:06
1
@jake Did you get rid of(500)
?
– 0x499602D2
Nov 27 '18 at 0:06
It works now. I think I might have left that in last time I tried that, and maybe that's why it wasn't working. @0x499602D2
– jake
Nov 27 '18 at 0:07
1
1
You should use
fileContents.push_back(word);
instead of fileContents[p] = word;
also change vector<string> fileContents(500);
to vector<string> fileContents;
and get rid of p
– drescherjm
Nov 27 '18 at 0:03
You should use
fileContents.push_back(word);
instead of fileContents[p] = word;
also change vector<string> fileContents(500);
to vector<string> fileContents;
and get rid of p
– drescherjm
Nov 27 '18 at 0:03
@drescherjm I tried that but when it goes to print out the file contents it doesn't print out anything? EDIT: NVM. I typed the command wrong.. that worked.. I thought I tried that but I guess I typed it wrong in the first place
– jake
Nov 27 '18 at 0:06
@drescherjm I tried that but when it goes to print out the file contents it doesn't print out anything? EDIT: NVM. I typed the command wrong.. that worked.. I thought I tried that but I guess I typed it wrong in the first place
– jake
Nov 27 '18 at 0:06
1
1
@jake Did you get rid of
(500)
?– 0x499602D2
Nov 27 '18 at 0:06
@jake Did you get rid of
(500)
?– 0x499602D2
Nov 27 '18 at 0:06
It works now. I think I might have left that in last time I tried that, and maybe that's why it wasn't working. @0x499602D2
– jake
Nov 27 '18 at 0:07
It works now. I think I might have left that in last time I tried that, and maybe that's why it wasn't working. @0x499602D2
– jake
Nov 27 '18 at 0:07
add a comment |
2 Answers
2
active
oldest
votes
You could also use a more direct approach, copying immediately from the input stream.
std::vector<std::string> loadFile(std::string fileName) {
std::ifstream file(fileName);
assert(file);
std::vector<std::string> fileContents;
std::copy(std::istream_iterator<std::string>(file),
std::istream_iterator<std::string>(),
std::back_inserter(fileContents));
return fileContents;
}
add a comment |
@drescherjm in the comments gave me the correct answer.
void loadFile(string fileName)
{
vector<string> fileContents;
ifstream file;
file.open(fileName);
if (!file.is_open()) return;
string word;
while (file >> word)
{
fileContents.push_back(word);
}
for (int i = 0; i < fileContents.size(); i++)
{
cout << fileContents[i] << endl;
}
}
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',
autoActivateHeartbeat: false,
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%2f53490887%2fadding-words-from-a-text-file-to-a-vector-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could also use a more direct approach, copying immediately from the input stream.
std::vector<std::string> loadFile(std::string fileName) {
std::ifstream file(fileName);
assert(file);
std::vector<std::string> fileContents;
std::copy(std::istream_iterator<std::string>(file),
std::istream_iterator<std::string>(),
std::back_inserter(fileContents));
return fileContents;
}
add a comment |
You could also use a more direct approach, copying immediately from the input stream.
std::vector<std::string> loadFile(std::string fileName) {
std::ifstream file(fileName);
assert(file);
std::vector<std::string> fileContents;
std::copy(std::istream_iterator<std::string>(file),
std::istream_iterator<std::string>(),
std::back_inserter(fileContents));
return fileContents;
}
add a comment |
You could also use a more direct approach, copying immediately from the input stream.
std::vector<std::string> loadFile(std::string fileName) {
std::ifstream file(fileName);
assert(file);
std::vector<std::string> fileContents;
std::copy(std::istream_iterator<std::string>(file),
std::istream_iterator<std::string>(),
std::back_inserter(fileContents));
return fileContents;
}
You could also use a more direct approach, copying immediately from the input stream.
std::vector<std::string> loadFile(std::string fileName) {
std::ifstream file(fileName);
assert(file);
std::vector<std::string> fileContents;
std::copy(std::istream_iterator<std::string>(file),
std::istream_iterator<std::string>(),
std::back_inserter(fileContents));
return fileContents;
}
answered Nov 27 '18 at 4:12
KostaKosta
1,771323
1,771323
add a comment |
add a comment |
@drescherjm in the comments gave me the correct answer.
void loadFile(string fileName)
{
vector<string> fileContents;
ifstream file;
file.open(fileName);
if (!file.is_open()) return;
string word;
while (file >> word)
{
fileContents.push_back(word);
}
for (int i = 0; i < fileContents.size(); i++)
{
cout << fileContents[i] << endl;
}
}
add a comment |
@drescherjm in the comments gave me the correct answer.
void loadFile(string fileName)
{
vector<string> fileContents;
ifstream file;
file.open(fileName);
if (!file.is_open()) return;
string word;
while (file >> word)
{
fileContents.push_back(word);
}
for (int i = 0; i < fileContents.size(); i++)
{
cout << fileContents[i] << endl;
}
}
add a comment |
@drescherjm in the comments gave me the correct answer.
void loadFile(string fileName)
{
vector<string> fileContents;
ifstream file;
file.open(fileName);
if (!file.is_open()) return;
string word;
while (file >> word)
{
fileContents.push_back(word);
}
for (int i = 0; i < fileContents.size(); i++)
{
cout << fileContents[i] << endl;
}
}
@drescherjm in the comments gave me the correct answer.
void loadFile(string fileName)
{
vector<string> fileContents;
ifstream file;
file.open(fileName);
if (!file.is_open()) return;
string word;
while (file >> word)
{
fileContents.push_back(word);
}
for (int i = 0; i < fileContents.size(); i++)
{
cout << fileContents[i] << endl;
}
}
answered Nov 27 '18 at 0:08
jakejake
235
235
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.
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%2f53490887%2fadding-words-from-a-text-file-to-a-vector-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
1
You should use
fileContents.push_back(word);
instead offileContents[p] = word;
also changevector<string> fileContents(500);
tovector<string> fileContents;
and get rid ofp
– drescherjm
Nov 27 '18 at 0:03
@drescherjm I tried that but when it goes to print out the file contents it doesn't print out anything? EDIT: NVM. I typed the command wrong.. that worked.. I thought I tried that but I guess I typed it wrong in the first place
– jake
Nov 27 '18 at 0:06
1
@jake Did you get rid of
(500)
?– 0x499602D2
Nov 27 '18 at 0:06
It works now. I think I might have left that in last time I tried that, and maybe that's why it wasn't working. @0x499602D2
– jake
Nov 27 '18 at 0:07