LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-16-2011, 01:18 AM   #1
akshay_satish
Member
 
Registered: May 2011
Posts: 63

Rep: Reputation: Disabled
trouble reading from a file


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.
 
Old 10-16-2011, 04:22 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Do you want to post the code you do have? Then we can make suggestions on how to adapt that
 
Old 10-16-2011, 05:17 AM   #3
akshay_satish
Member
 
Registered: May 2011
Posts: 63

Original Poster
Rep: Reputation: Disabled
this doesnt make much of sense for my requirement, but... yet to improvise on it

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;
}

Last edited by akshay_satish; 10-16-2011 at 07:19 AM.
 
Old 10-16-2011, 05:31 AM   #4
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
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.
 
Old 10-16-2011, 05:44 AM   #5
akshay_satish
Member
 
Registered: May 2011
Posts: 63

Original Poster
Rep: Reputation: Disabled
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.
 
Old 10-17-2011, 11:26 AM   #6
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
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?

Hope this helps,

Last edited by Snark1994; 10-17-2011 at 11:35 AM.
 
Old 10-20-2011, 12:45 AM   #7
akshay_satish
Member
 
Registered: May 2011
Posts: 63

Original Poster
Rep: Reputation: Disabled
Thank you very much for the reply.
This is my compiling code: works to a certain extent

Code:
#include<iostream>
#include<fstream>
#include<string>
using namespace std;

int main()
{
string line, totalLine = "scsi1: ";
ifstream myfile("scsiInfo");
long begin, end;
if(myfile.is_open())
{
begin = myfile.tellg();
myfile.seekg(0, ios::end);
end = myfile.tellg();

myfile.seekg(0, ios::beg);

getline(myfile, line);
line.clear();
        while(!myfile.eof())
        {
          getline(myfile, line);
          totalLine += line + ", ";
        }

size_t found1;
found1=totalLine.find('\t');
while(found1 != string::npos)
{
totalLine = totalLine.erase(found1, 2);
found1 = totalLine.find('\t', found1);
}

cout <<totalLine<< endl<< "Size is : "<< (end - begin)<< " bytes."<<endl;
myfile.close();
}
else cout<<"Unable to open file"<<endl;
return 0;
}
here my i/p file from which I have to read:
Code:
Attached devices:
Unit: a1 channel:12 abcd
qwerty: 1234asdf                     revision:1
ver: nine                            rev:2
o/p-->
Code:
scsi1: Unit: a1 channel:12 abcd, qwerty: 1234asdf revision:1, ver: nine rev:2, ,
o/p i want is->
Code:
scsi1: Unit: a1 channel:12 abcd, qwerty: 1234asdf revision:1, ver: nine rev:2

Last edited by akshay_satish; 10-20-2011 at 08:16 AM.
 
Old 10-21-2011, 05:00 AM   #8
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Okay, there are two problems:

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,
 
Old 10-24-2011, 07:35 AM   #9
akshay_satish
Member
 
Registered: May 2011
Posts: 63

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Snark1994 View Post
Okay, there are two problems:

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..
 
Old 10-24-2011, 07:39 AM   #10
akshay_satish
Member
 
Registered: May 2011
Posts: 63

Original Poster
Rep: Reputation: Disabled
Smile

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..
 
Old 10-25-2011, 04:19 AM   #11
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
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?
 
Old 10-25-2011, 04:39 AM   #12
akshay_satish
Member
 
Registered: May 2011
Posts: 63

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Snark1994 View Post
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?
 
Old 10-26-2011, 05:19 AM   #13
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
I still don't understand what you mean by "validate those fields". Can you give an example?
 
Old 10-26-2011, 11:52 PM   #14
akshay_satish
Member
 
Registered: May 2011
Posts: 63

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Snark1994 View Post
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.
 
Old 10-28-2011, 05:22 AM   #15
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
So... If your input were:

Code:
Attached devices:
Unit: a1 channel:12 abcd
qwerty: 1234asdf                     revision:1
ver:                                 rev:2
you would want your output to be

Code:
scsi1: Unit: a1 channel:12 abcd, qwerty: 1234asdf revision:1, ver: rev:2
?

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...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Trouble reading btmp brownsman Linux - Security 3 09-01-2010 11:34 AM
Trouble reading from serial port. rkjnsn Programming 13 12-04-2009 11:57 PM
Having trouble reading from serial port glo Linux - Newbie 0 02-09-2007 10:42 AM
Trouble Reading Newgroups Setheck Linux - Software 1 07-05-2005 06:18 PM
Having trouble reading a floppy cestor Linux - General 1 05-29-2002 11:47 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration