How do I obtain the column of my CSV File?
up vote
2
down vote
favorite
I'm trying to obtain the last column of my CSV file. I tried using getline and the stringstream but it doesn't get the last column only
stringstream lineStream(line);
string bit;
while (getline(inputFile, line))
{
stringstream lineStream(line);
bit = "";
getline(lineStream, bit, ',');
getline(lineStream, bit, 'n');
getline(inputFile, line);
stringVector.push_back(bit);
}
My CSV file:
5.1,3.5,1.4,0.2,no
4.9,3.0,1.4,0.2,yes
4.7,3.2,1.3,0.2,no
4.6,3.1,1.5,0.2,yes
5.0,3.6,1.4,0.2,no
5.4,3.9,1.7,0.4,yes
c++ csv
add a comment |
up vote
2
down vote
favorite
I'm trying to obtain the last column of my CSV file. I tried using getline and the stringstream but it doesn't get the last column only
stringstream lineStream(line);
string bit;
while (getline(inputFile, line))
{
stringstream lineStream(line);
bit = "";
getline(lineStream, bit, ',');
getline(lineStream, bit, 'n');
getline(inputFile, line);
stringVector.push_back(bit);
}
My CSV file:
5.1,3.5,1.4,0.2,no
4.9,3.0,1.4,0.2,yes
4.7,3.2,1.3,0.2,no
4.6,3.1,1.5,0.2,yes
5.0,3.6,1.4,0.2,no
5.4,3.9,1.7,0.4,yes
c++ csv
Several ways. You could just loop withgetline(lineStream, bit, ',')-- the last string read will be the last field. Or you could usestd::string::rfindto find the last comma andstd::string::substrto retrieve it from 1 after that position. This is kinda equivalent tostd::string::find_last_of, so that's an option. Or you could usestd::regex(overkill). Probably others. Take your pick.
– paddy
Nov 20 at 4:39
@paddy Sorry is it something like this with the while loop?while (getline(lineStream, bit, ',')) { getline(lineStream, bit); bit = ""; cout << line; }
– jake louie
Nov 20 at 4:46
Errrr no not really. See my answer for a more direct approach. Usinggetlinerepeatedly for this is overkill if you don't care about the other strings. Plus you're calling it an extra time inside the loop, pointlessly clearing the result, and outputting the entire line. That makes no sense.
– paddy
Nov 20 at 4:51
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I'm trying to obtain the last column of my CSV file. I tried using getline and the stringstream but it doesn't get the last column only
stringstream lineStream(line);
string bit;
while (getline(inputFile, line))
{
stringstream lineStream(line);
bit = "";
getline(lineStream, bit, ',');
getline(lineStream, bit, 'n');
getline(inputFile, line);
stringVector.push_back(bit);
}
My CSV file:
5.1,3.5,1.4,0.2,no
4.9,3.0,1.4,0.2,yes
4.7,3.2,1.3,0.2,no
4.6,3.1,1.5,0.2,yes
5.0,3.6,1.4,0.2,no
5.4,3.9,1.7,0.4,yes
c++ csv
I'm trying to obtain the last column of my CSV file. I tried using getline and the stringstream but it doesn't get the last column only
stringstream lineStream(line);
string bit;
while (getline(inputFile, line))
{
stringstream lineStream(line);
bit = "";
getline(lineStream, bit, ',');
getline(lineStream, bit, 'n');
getline(inputFile, line);
stringVector.push_back(bit);
}
My CSV file:
5.1,3.5,1.4,0.2,no
4.9,3.0,1.4,0.2,yes
4.7,3.2,1.3,0.2,no
4.6,3.1,1.5,0.2,yes
5.0,3.6,1.4,0.2,no
5.4,3.9,1.7,0.4,yes
c++ csv
c++ csv
edited Nov 20 at 9:24
asked Nov 20 at 4:33
jake louie
133
133
Several ways. You could just loop withgetline(lineStream, bit, ',')-- the last string read will be the last field. Or you could usestd::string::rfindto find the last comma andstd::string::substrto retrieve it from 1 after that position. This is kinda equivalent tostd::string::find_last_of, so that's an option. Or you could usestd::regex(overkill). Probably others. Take your pick.
– paddy
Nov 20 at 4:39
@paddy Sorry is it something like this with the while loop?while (getline(lineStream, bit, ',')) { getline(lineStream, bit); bit = ""; cout << line; }
– jake louie
Nov 20 at 4:46
Errrr no not really. See my answer for a more direct approach. Usinggetlinerepeatedly for this is overkill if you don't care about the other strings. Plus you're calling it an extra time inside the loop, pointlessly clearing the result, and outputting the entire line. That makes no sense.
– paddy
Nov 20 at 4:51
add a comment |
Several ways. You could just loop withgetline(lineStream, bit, ',')-- the last string read will be the last field. Or you could usestd::string::rfindto find the last comma andstd::string::substrto retrieve it from 1 after that position. This is kinda equivalent tostd::string::find_last_of, so that's an option. Or you could usestd::regex(overkill). Probably others. Take your pick.
– paddy
Nov 20 at 4:39
@paddy Sorry is it something like this with the while loop?while (getline(lineStream, bit, ',')) { getline(lineStream, bit); bit = ""; cout << line; }
– jake louie
Nov 20 at 4:46
Errrr no not really. See my answer for a more direct approach. Usinggetlinerepeatedly for this is overkill if you don't care about the other strings. Plus you're calling it an extra time inside the loop, pointlessly clearing the result, and outputting the entire line. That makes no sense.
– paddy
Nov 20 at 4:51
Several ways. You could just loop with
getline(lineStream, bit, ',') -- the last string read will be the last field. Or you could use std::string::rfind to find the last comma and std::string::substr to retrieve it from 1 after that position. This is kinda equivalent to std::string::find_last_of, so that's an option. Or you could use std::regex (overkill). Probably others. Take your pick.– paddy
Nov 20 at 4:39
Several ways. You could just loop with
getline(lineStream, bit, ',') -- the last string read will be the last field. Or you could use std::string::rfind to find the last comma and std::string::substr to retrieve it from 1 after that position. This is kinda equivalent to std::string::find_last_of, so that's an option. Or you could use std::regex (overkill). Probably others. Take your pick.– paddy
Nov 20 at 4:39
@paddy Sorry is it something like this with the while loop?
while (getline(lineStream, bit, ',')) { getline(lineStream, bit); bit = ""; cout << line; }– jake louie
Nov 20 at 4:46
@paddy Sorry is it something like this with the while loop?
while (getline(lineStream, bit, ',')) { getline(lineStream, bit); bit = ""; cout << line; }– jake louie
Nov 20 at 4:46
Errrr no not really. See my answer for a more direct approach. Using
getline repeatedly for this is overkill if you don't care about the other strings. Plus you're calling it an extra time inside the loop, pointlessly clearing the result, and outputting the entire line. That makes no sense.– paddy
Nov 20 at 4:51
Errrr no not really. See my answer for a more direct approach. Using
getline repeatedly for this is overkill if you don't care about the other strings. Plus you're calling it an extra time inside the loop, pointlessly clearing the result, and outputting the entire line. That makes no sense.– paddy
Nov 20 at 4:51
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
Probably the simplest approach is to use std::string::rfind as follows:
while (std::getline(inputFile, line))
{
// Find position after last comma, extract the string following it and
// add to the vector. If no comma found and non-empty line, treat as
// special case and add that too.
std::string::size_type pos = line.rfind(',');
if (pos != std::string::npos)
stringVector.push_back(line.substr(pos + 1));
else if (!line.empty())
stringVector.push_back(line);
}
Hi @paddy Sorry for the late comments, but when I tried to run this part of the code it didn't work on Linux g++ compiler, but worked on visual studios 2017. Is there something that I need to add such as a library?
– jake louie
Nov 24 at 11:41
Read the error message. If you don't understand the error message, post it. Saying something "didn't work" is not how computer programmers speak, as it is meaningless.
– paddy
Nov 25 at 20:21
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%2f53386287%2fhow-do-i-obtain-the-column-of-my-csv-file%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
2
down vote
accepted
Probably the simplest approach is to use std::string::rfind as follows:
while (std::getline(inputFile, line))
{
// Find position after last comma, extract the string following it and
// add to the vector. If no comma found and non-empty line, treat as
// special case and add that too.
std::string::size_type pos = line.rfind(',');
if (pos != std::string::npos)
stringVector.push_back(line.substr(pos + 1));
else if (!line.empty())
stringVector.push_back(line);
}
Hi @paddy Sorry for the late comments, but when I tried to run this part of the code it didn't work on Linux g++ compiler, but worked on visual studios 2017. Is there something that I need to add such as a library?
– jake louie
Nov 24 at 11:41
Read the error message. If you don't understand the error message, post it. Saying something "didn't work" is not how computer programmers speak, as it is meaningless.
– paddy
Nov 25 at 20:21
add a comment |
up vote
2
down vote
accepted
Probably the simplest approach is to use std::string::rfind as follows:
while (std::getline(inputFile, line))
{
// Find position after last comma, extract the string following it and
// add to the vector. If no comma found and non-empty line, treat as
// special case and add that too.
std::string::size_type pos = line.rfind(',');
if (pos != std::string::npos)
stringVector.push_back(line.substr(pos + 1));
else if (!line.empty())
stringVector.push_back(line);
}
Hi @paddy Sorry for the late comments, but when I tried to run this part of the code it didn't work on Linux g++ compiler, but worked on visual studios 2017. Is there something that I need to add such as a library?
– jake louie
Nov 24 at 11:41
Read the error message. If you don't understand the error message, post it. Saying something "didn't work" is not how computer programmers speak, as it is meaningless.
– paddy
Nov 25 at 20:21
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Probably the simplest approach is to use std::string::rfind as follows:
while (std::getline(inputFile, line))
{
// Find position after last comma, extract the string following it and
// add to the vector. If no comma found and non-empty line, treat as
// special case and add that too.
std::string::size_type pos = line.rfind(',');
if (pos != std::string::npos)
stringVector.push_back(line.substr(pos + 1));
else if (!line.empty())
stringVector.push_back(line);
}
Probably the simplest approach is to use std::string::rfind as follows:
while (std::getline(inputFile, line))
{
// Find position after last comma, extract the string following it and
// add to the vector. If no comma found and non-empty line, treat as
// special case and add that too.
std::string::size_type pos = line.rfind(',');
if (pos != std::string::npos)
stringVector.push_back(line.substr(pos + 1));
else if (!line.empty())
stringVector.push_back(line);
}
answered Nov 20 at 4:49
paddy
42.5k53076
42.5k53076
Hi @paddy Sorry for the late comments, but when I tried to run this part of the code it didn't work on Linux g++ compiler, but worked on visual studios 2017. Is there something that I need to add such as a library?
– jake louie
Nov 24 at 11:41
Read the error message. If you don't understand the error message, post it. Saying something "didn't work" is not how computer programmers speak, as it is meaningless.
– paddy
Nov 25 at 20:21
add a comment |
Hi @paddy Sorry for the late comments, but when I tried to run this part of the code it didn't work on Linux g++ compiler, but worked on visual studios 2017. Is there something that I need to add such as a library?
– jake louie
Nov 24 at 11:41
Read the error message. If you don't understand the error message, post it. Saying something "didn't work" is not how computer programmers speak, as it is meaningless.
– paddy
Nov 25 at 20:21
Hi @paddy Sorry for the late comments, but when I tried to run this part of the code it didn't work on Linux g++ compiler, but worked on visual studios 2017. Is there something that I need to add such as a library?
– jake louie
Nov 24 at 11:41
Hi @paddy Sorry for the late comments, but when I tried to run this part of the code it didn't work on Linux g++ compiler, but worked on visual studios 2017. Is there something that I need to add such as a library?
– jake louie
Nov 24 at 11:41
Read the error message. If you don't understand the error message, post it. Saying something "didn't work" is not how computer programmers speak, as it is meaningless.
– paddy
Nov 25 at 20:21
Read the error message. If you don't understand the error message, post it. Saying something "didn't work" is not how computer programmers speak, as it is meaningless.
– paddy
Nov 25 at 20:21
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%2f53386287%2fhow-do-i-obtain-the-column-of-my-csv-file%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
Several ways. You could just loop with
getline(lineStream, bit, ',')-- the last string read will be the last field. Or you could usestd::string::rfindto find the last comma andstd::string::substrto retrieve it from 1 after that position. This is kinda equivalent tostd::string::find_last_of, so that's an option. Or you could usestd::regex(overkill). Probably others. Take your pick.– paddy
Nov 20 at 4:39
@paddy Sorry is it something like this with the while loop?
while (getline(lineStream, bit, ',')) { getline(lineStream, bit); bit = ""; cout << line; }– jake louie
Nov 20 at 4:46
Errrr no not really. See my answer for a more direct approach. Using
getlinerepeatedly for this is overkill if you don't care about the other strings. Plus you're calling it an extra time inside the loop, pointlessly clearing the result, and outputting the entire line. That makes no sense.– paddy
Nov 20 at 4:51