ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I need some suggestions here.
I have a file names abcd. Contents of the file are:
Raw File:
Unit: a1 channel:12 abcd
qwerty: 1234asdf revision:1
ver: nine rev:2
I need to read this into a string as a comma separated list;
eg:
a0: Unit: a1 channel:12 abcd, qwerty: 1234asdf, ver: nine
note that revision:1 and rev:2 are part of the final string and also note the prefix a0 in the final dest and also the Raw File: at the top of the file is not included in the final string;
As of now, I am able to read the entire contents into a string using ifstream and getline.. but yet to figure out how to add those commas. Thoughts pls.
Last edited by akshay_satish; 10-16-2011 at 07:37 AM.
Firstly, it's best if you use (CODE)(/CODE) tags around your code (use square brackets [] not parentheses (), I only used them so it didn't think I was trying to write code ).
Secondly, you said
Quote:
As of now, I am able to read the entire contents into a string using ifstream and getline.. but yet to figure out how to add those commas. Thoughts pls.
Doesn't really look like you're doing that currently you've still got pseudocode in your example. Take a look at this code example to see how to do the testing if your file has opened properly. You can then combine it with stringstream to put the commas in the correct places.
Thanks, Kinda new to these concepts,getting my grip on it... any snippets u can share? which u can think of looking at the scenario.
I've worked a little further on my code and corrected the above one, I can now read from the file and insert commas at the right places now(except after last line which I dont want to). Can anyone tell me if theres a way to include revision:1 and rev:2 from my final string separated by only one space. Those two are screwing up my o/p
Last edited by akshay_satish; 10-16-2011 at 07:37 AM.
You're getting closer, but you're still not posting compilable code. Well done for using the CODE tags, but if you also indented you'd be able to see where it's going wrong:
Code:
int main() {
ifstream myfile("abcd");
string line;
if(myfile.is_open()) {
while(!myfile.eof()) {
getline(myfile, line);
while(!myfile.eof()) {
totalLine += line + ", ";
getline(myfile, line);
}
cout<<totalLine;
myfile.close();
} else
cout << "unable to open file"<< endl;
}
You also need to add '#include's, 'using namepace std', and a bunch of variable declarations. If you post working code we'll be able to help you better 'cos we know what you're actually running, not what you think you might be running.
Finally, I don't quite understand your most recent question - do you mean you want to remove the "ver: nine" part of the file?
1) You're reading in a blank line at the end, which causes the second extra ","
2) You don't have a way of testing if you're at the last line of the file, so you don't know whether or not to add a "," at the end of the word you're adding.
(Also, I needed to change "totalLine.erase(found1,2)" into "totalLine.erase(found1,1)", but that could just be different whitespace formatting in the scsiInfo file)
The first one is easy to solve:
Code:
...
while(!myfile.eof()) {
getline(myfile, line);
if(line != ""){ // NEW! - only adds the line if not empty
totalLine += line + ", ";
}
}
...
The second one is trickier. In my opinion, the best way to do it is to add the "," before you add 'line'. This way you will only ever have a comma if there is another term to add. However, you need to stop it putting a comma before the first line you add. So:
Code:
...
bool firstLine = true; //NEW
while(!myfile.eof()) {
getline(myfile, line);
if(line != ""){
if(firstLine){ //NEW from here...
firstLine = false; //so next time we'll start adding ","
} else {
totalLine += ", ";
}
totalLine += line; //...up to here
}
}
...
1) You're reading in a blank line at the end, which causes the second extra ","
2) You don't have a way of testing if you're at the last line of the file, so you don't know whether or not to add a "," at the end of the word you're adding.
(Also, I needed to change "totalLine.erase(found1,2)" into "totalLine.erase(found1,1)", but that could just be different whitespace formatting in the scsiInfo file)
The first one is easy to solve:
Code:
...
while(!myfile.eof()) {
getline(myfile, line);
if(line != ""){ // NEW! - only adds the line if not empty
totalLine += line + ", ";
}
}
...
The second one is trickier. In my opinion, the best way to do it is to add the "," before you add 'line'. This way you will only ever have a comma if there is another term to add. However, you need to stop it putting a comma before the first line you add. So:
Code:
...
bool firstLine = true; //NEW
while(!myfile.eof()) {
getline(myfile, line);
if(line != ""){
if(firstLine){ //NEW from here...
firstLine = false; //so next time we'll start adding ","
} else {
totalLine += ", ";
}
totalLine += line; //...up to here
}
}
...
Hope this helps,
Hey thanks a bunch... I did a few mod's to this however as it did not work as expected. I removed the last line:
[CODE]
totalLine += line;
[CODE]
as that did not give me the right o/p and it kind of jumbled my output and did not put commas at the right place....At the end, I just go ahead and remove any trailing commas which works..
I would like to ask one last thing here. If you can see below:
Code:
Unit: a1 channel:12 abcd
qwerty: 1234asdf revision:1
ver: nine rev:2
You can see fields like Unit, channel, qwerty, revision, ver and rev. Now Is there any possible way I can validate them (i.e., even if one of the above menitoned fields are not present or dont have any value associated with them, then I just need to store and empty string in totalLine). I've got every other bit in my code except for this.. Any clues pls?
Appreciated..
Unless I've misunderstood your question, adding an empty string to totalLine
Code:
totalLine += ""
does absolutely nothing except perhaps use up some computer time (unless it's compiled out). Do you want to give me an example which doesn't work as you'd expect, along with expected and actual output?
Unless I've misunderstood your question, adding an empty string to totalLine
Code:
totalLine += ""
does absolutely nothing except perhaps use up some computer time (unless it's compiled out). Do you want to give me an example which doesn't work as you'd expect, along with expected and actual output?
Thanks, I got almost what I wanted. All I wanted to know is, if theres any way I can validate those fields i mentioned above if there are any?
I still don't understand what you mean by "validate those fields". Can you give an example?
Like i need to check for the existense of Unit, Channel and revision and if any of them are missing or even their contents then i need to store an empty string in totalLine.
Is the issue that (depending on the whitespace) you get extra spaces in between 'ver' and 'rev'? If so, you just need to loop through (like you did for the tab characters) and replace 2 spaces with 1 space. If not, I can't see the problem with your output, as that is what I get when I run the code on the input...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.