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. 1 2129
On 30 Jul 2004 23:36:49 -0700, ma740988 <ma******@pegasus.cc.ucf.edu>
wrote: 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?
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. //////////////////////////// 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?
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<<.
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.
Too sophisticated IMHO, what you have is fine. Thanks in advance.
john This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Vikram |
last post by:
Hi,
I don't remember if it happened previously, but nowadays
I'm having problem with using cin.getline function and
cin>> function simultaneously.
I have Visual Studio 6. If I use cin.getline...
|
by: vknid |
last post by:
Hello, I have a question. Its probably a very newbish question so please be
nice hehe. =D
I have been reading through C++ Programming Fundamentals, and have come a
crossed an example program...
|
by: Skywise |
last post by:
I keep getting the following error upon compiling:
c:\c++ files\programs\stellardebug\unitcode.h(677) : error C2664:
'class istream &__thiscall istream::getline(char *,int,char)' : cannot
convert...
|
by: KL |
last post by:
I am so lost. I am in a college course for C++, and first off let me
state I am not asking for anyone to do my assignment, just
clarification on what I seem to not be able to comprehend. I have a...
|
by: Amadeus W. M. |
last post by:
I'm trying to read a whole file as a single string, using the getline()
function, as in the example below. I can't tell what I'm doing wrong.
Tried g++ 3.2, 3.4 and 4.0. Thanks!
#include...
| |
by: jalkadir |
last post by:
I am trying to get character string from the user, to do that I use
getline(char_type*, streamsize), but I get a segmentation fault??!!
Can anyone give me a hand, what am I doing wrong?
--snip...
|
by: allspamgoeshere3 |
last post by:
Hi!
I'm having trouble reading text lines from a file describing the levels
for a simple game I'm creating.
Inside the function that reads the content of the file there is a loop
that basically...
|
by: Chen shuSheng |
last post by:
I have a code:
---------------------------
#include <iostream.h>
#include <stdlib.h>
int main()
{ int max=15;
char line;
getline(line,max);
system("PAUSE");
|
by: Mark P |
last post by:
Consider the following snippet of code to read lines from a text file:
ifstream file_stream( "some_file.txt");
string read_line;
while( file_stream)
{
getline( file_stream, read_line);
}
...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |