Connecting Tech Pros Worldwide Forums | Help | Site Map

getline - 2 questions

ma740988
Guest
 
Posts: n/a
#1: Jul 22 '05
Consider:

ifstrem MyFile("extractMe.txt");
string Str;
getline(MyFile, Str);
getline above extracts the contents of MyFile and place into the
string object. Deduced using FROM/TO logic I could state getline's
first parameter supports "FROM". The second parameter supports "TO".

// later
istringstream Stream(Str);
const int MAX(100);
char buf[MAX];
Stream.getline(buf, MAX);

Here getline extracts up to MAX the contents of the Stream and place
into buf. Except, with regards to the first/second parameters my FROM
-> TO logic doesn't make sense.

When viewed from a parameter perspective, it appears to me that theres
differing views to getline. Yes/No?

//////////////////////////// Question 2 ////////////////////////////

For test purposes the contents of a file I'm interested in reading are
as follows:

Australia
5E56,7667230284,Langler,Tyson,31.2147,0.0004211736 1
2B97,7586701,Oneill,Zeke,553.429,0.007467305315606 5
France
// etc

The struct UserInfo (below) is used to describe the data. So now
Australia, represents the region, 5E56 represents the seller id,
7667230284 represents the phone number and so on.

struct UserInfo
{
string Region;
string SellerId;
double PhoneNum; //Phone number too large for an int.
string LastName;
string FirstName;
double TotalSales;
double AmountTotalSales;
};

#if 0
std::ostream& operator << ( std::ostream& Out, const
std::vector<UserInfo>& V )
{
std::vector<UserInfo>::const_iterator Pos = V.begin();
for( Pos; Pos != V.end(); ++Pos )
Out << Pos->AmountTotalSales; // Test

return Out;
}
#endif


int main()
{

ifstream File("exercise15.txt");
if (!File.is_open())
{
cerr << " error opening file ";
exit(1);
}

string Str;
vector<UserInfo> info;
UserInfo userInfo;
while (getline(File, Str))
{
std::istringstream Stream(Str);
string::size_type Idx = Str.find(',');
if (Idx == string::npos)
{
Stream >> userInfo.Region;
//cout << userInfo.Region << '\n';
}
else
{
char comma;

getline(Stream, userInfo.SellerId, ',');
Stream >> userInfo.PhoneNum >> comma;
getline(Stream, userInfo.LastName, ',');
getline(Stream, userInfo.FirstName, ',');
Stream >> userInfo.TotalSales >> comma;
Stream >> userInfo.AmountTotalSales >> comma;

#if 0
cout << userInfo.SellerId << '\n';
cout << userInfo.PhoneNum << '\n';
cout << userInfo.LastName << '\n';
cout << userInfo.FirstName << '\n';
cout << userInfo.TotalSales << '\n';
cout << userInfo.AmountTotalSales << '\n';
cout << '\n';
#endif
}
info.push_back(userInfo);
}
File.close();
}

Works. Now, I need to print the data in a specific format. i.e

Australia
-------------------------------------------------------------------------------
SellerId Phone Number Last Name First Name Total Sales Amount
Total
24150 (766)723-0284 Langler Tyson 31.2147
0.00042117361

The question. Would overloading the operator << be the most viable
approach?
Comments on my approach in extracting the data is also welcome.

Note: I've seen - for my level - some sophisticated approaches
(facets, locales, copying classic_tables etc ) to handling delimited
streams. I was opting to use one of them but I figured I'll try to
walk first before I run - so to speak.

Thanks in advance.

John Harrison
Guest
 
Posts: n/a
#2: Jul 22 '05

re: getline - 2 questions


On 30 Jul 2004 23:36:49 -0700, ma740988 <ma740988@pegasus.cc.ucf.edu>
wrote:
[color=blue]
> Consider:
>
> ifstrem MyFile("extractMe.txt");
> string Str;
> getline(MyFile, Str);
> getline above extracts the contents of MyFile and place into the
> string object. Deduced using FROM/TO logic I could state getline's
> first parameter supports "FROM". The second parameter supports "TO".
>
> // later
> istringstream Stream(Str);
> const int MAX(100);
> char buf[MAX];
> Stream.getline(buf, MAX);
>
> Here getline extracts up to MAX the contents of the Stream and place
> into buf. Except, with regards to the first/second parameters my FROM
> -> TO logic doesn't make sense.
>
> When viewed from a parameter perspective, it appears to me that theres
> differing views to getline. Yes/No?[/color]

Yes. I don't understand the from/to bit, but one version of getline works
on strings and the other works on char arrays. The char array version is a
member of istream, so

stream.getline(char_array, char_array_size);

the string version is a free function so

getline(stream, string);

I don't really understand why the string version is not a member function
however.
[color=blue]
>
> //////////////////////////// Question 2 ////////////////////////////
>
> For test purposes the contents of a file I'm interested in reading are
> as follows:
>
> Australia
> 5E56,7667230284,Langler,Tyson,31.2147,0.0004211736 1
> 2B97,7586701,Oneill,Zeke,553.429,0.007467305315606 5
> France
> // etc
>
> The struct UserInfo (below) is used to describe the data. So now
> Australia, represents the region, 5E56 represents the seller id,
> 7667230284 represents the phone number and so on.
>
> struct UserInfo
> {
> string Region;
> string SellerId;
> double PhoneNum; //Phone number too large for an int.
> string LastName;
> string FirstName;
> double TotalSales;
> double AmountTotalSales;
> };
>
> #if 0
> std::ostream& operator << ( std::ostream& Out, const
> std::vector<UserInfo>& V )
> {
> std::vector<UserInfo>::const_iterator Pos = V.begin();
> for( Pos; Pos != V.end(); ++Pos )
> Out << Pos->AmountTotalSales; // Test
>
> return Out;
> }
> #endif
>
>
> int main()
> {
>
> ifstream File("exercise15.txt");
> if (!File.is_open())
> {
> cerr << " error opening file ";
> exit(1);
> }
>
> string Str;
> vector<UserInfo> info;
> UserInfo userInfo;
> while (getline(File, Str))
> {
> std::istringstream Stream(Str);
> string::size_type Idx = Str.find(',');
> if (Idx == string::npos)
> {
> Stream >> userInfo.Region;
> //cout << userInfo.Region << '\n';
> }
> else
> {
> char comma;
>
> getline(Stream, userInfo.SellerId, ',');
> Stream >> userInfo.PhoneNum >> comma;
> getline(Stream, userInfo.LastName, ',');
> getline(Stream, userInfo.FirstName, ',');
> Stream >> userInfo.TotalSales >> comma;
> Stream >> userInfo.AmountTotalSales >> comma;
>
> #if 0
> cout << userInfo.SellerId << '\n';
> cout << userInfo.PhoneNum << '\n';
> cout << userInfo.LastName << '\n';
> cout << userInfo.FirstName << '\n';
> cout << userInfo.TotalSales << '\n';
> cout << userInfo.AmountTotalSales << '\n';
> cout << '\n';
> #endif
> }
> info.push_back(userInfo);
> }
> File.close();
> }
>
> Works. Now, I need to print the data in a specific format. i.e
>
> Australia
> -------------------------------------------------------------------------------
> SellerId Phone Number Last Name First Name Total Sales Amount
> Total
> 24150 (766)723-0284 Langler Tyson 31.2147
> 0.00042117361
>
> The question. Would overloading the operator << be the most viable
> approach?[/color]

It would be possible, but personally I wouldn't do it like that.
Overloading operator<< for a type implies to me that there is one
obviously best way to output that type and I don't think that is the case
here. But this is just a question of names, write a function to output one
UserInfo struct on one line if you like, just don't call it operator<<.
[color=blue]
> Comments on my approach in extracting the data is also welcome.
>
> Note: I've seen - for my level - some sophisticated approaches
> (facets, locales, copying classic_tables etc ) to handling delimited
> streams. I was opting to use one of them but I figured I'll try to
> walk first before I run - so to speak.[/color]

Too sophisticated IMHO, what you have is fine.
[color=blue]
>
> Thanks in advance.[/color]

john
Closed Thread