473,398 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Reading/Writing Vector of Structs to/from Tab Delimited File

jwwicks
19
Hello All,

This is a student assignment. So I don't want the complete answer just a hint or maybe a bumb on the head cause I'm doing it the wrong way. Assume I haven't done anything braindead like not include a header etc... I can post the whole code if you like/need it but I'm trying to spare the forum :)

Got a product structure...
Expand|Select|Wrap|Line Numbers
  1. struct product {
  2.     string id;
  3.     string description;
  4.     int quantity;
  5.     double wholesale;
  6.     double retail;
  7.     Date* date_added;
  8. };
  9.  
Got a vector of products and a file for the data...

Expand|Select|Wrap|Line Numbers
  1. vector<product> Inventory;
  2. ifstream inFile; //Input data file
  3. ofstream outFile; //Output report file
  4.  
Saved all the products to a tab delimited file, one product to each line using for_each...

Expand|Select|Wrap|Line Numbers
  1.  
  2. for_each(Inventory.begin(), Inventory.end(), save_product(&outFile));
  3.  
  4. class save_product:public std::unary_function<product, bool>
  5. {
  6. private:
  7.     ostream* m_os;
  8. public:
  9.     save_product( ostream* os ){ m_os = os; }
  10.     bool operator()( const product& element )
  11.     {
  12.         bool ret_val = false;
  13.         // save element in tab delimited format
  14.         *m_os << element.id.c_str() 
  15.             << "\t" << element.description.c_str() 
  16.             << "\t" << element.quantity 
  17.             << "\t" << element.wholesale 
  18.             << "\t" << element.retail 
  19.             << "\t" << element.date_added->get(2) // write date in month/day/year format
  20.             << "/" << element.date_added->get(1) 
  21.             << "/" << element.date_added->get(3) 
  22.             << "\r\n";
  23.         if(m_os)
  24.             ret_val = true;
  25.         return ret_val;
  26.     }
  27. };
  28.  
No problem so far. The file is as I want it. But getting the data back into the vector is causing me some problems. I've seen the use of the copy algorithm for copying from a fstream to a vector but I need to tell the iterator to use tabs to delimit the text.... I wrote a functor load_product but what algorithm could I use to read the file in with...

Expand|Select|Wrap|Line Numbers
  1.  
  2. // Copy doesn't quite do what I want with data...
  3. copy( istream::iterator<T>(inFile), istream::iterator<T>(), back_inserter(Inventory) );
  4.  
  5. class load_product:public std::unary_function<product, bool>
  6. {
  7. private:
  8.     istream* m_is;
  9. public:
  10.     load_product( istream* is ){ m_is = is; }
  11.     bool operator()( product& element )
  12.     {
  13.         bool ret_val = false;
  14.         char buffer[maxStringSize];
  15.         stringstream ioConv;
  16.         unsigned int fieldCnt = 0;
  17.  
  18.         while(m_is && fieldCnt < REC_FIELD_COUNT )
  19.         {
  20.             m_is->getline(buffer,(streamsize)maxStringSize, '\t');
  21.             if(m_is)
  22.             {
  23.                 switch( fieldCnt )
  24.                 {
  25.                     case 0: element.id = buffer; break;
  26.                     case 1: element.description = buffer; break;
  27.                     case 2: ioConv >> buffer; ioConv << element.quantity; break;
  28.                     case 3: ioConv >> buffer; ioConv << element.retail; break;
  29.                     case 4: ioConv >> buffer; ioConv << element.wholesale; break;
  30.                     default:
  31.                         element.date_added->set(buffer, "/");
  32.                         break;
  33.                 }
  34.             }
  35.             fieldCnt++;
  36.             if( fieldCnt == REC_FIELD_COUNT && m_is ) ret_val = true;
  37.         }
  38.  
  39.         return ret_val;
  40.     }
  41. };
  42.  
  43.  
I can do this manually with a while sure but I was hoping for a more elegant/STL way of doing it...

Thanks for any guidance...
John
Oct 19 '07 #1
1 7783
jwwicks
19
Hello All,

Expand|Select|Wrap|Line Numbers
  1.  
  2. // Copy doesn't quite do what I want with data...
  3. copy( istream::iterator<T>(inFile), istream::iterator<T>(), back_inserter(Inventory) );
  4.  
  5. class load_product:public std::unary_function<product, bool>
  6. {
  7. private:
  8. istream* m_is;
  9. public:
  10. load_product( istream* is ){ m_is = is; }
  11. bool operator()( product& element )
  12. {
  13. bool ret_val = false;
  14. char buffer[maxStringSize];
  15. stringstream ioConv;
  16. unsigned int fieldCnt = 0;
  17.  
  18. while(m_is && fieldCnt < REC_FIELD_COUNT )
  19. {
  20. m_is->getline(buffer,(streamsize)maxStringSize, '\t');
  21. if(m_is)
  22. {
  23. switch( fieldCnt )
  24. {
  25. case 0: element.id = buffer; break;
  26. case 1: element.description = buffer; break;
  27. case 2: ioConv >> buffer; ioConv << element.quantity; break;
  28. case 3: ioConv >> buffer; ioConv << element.retail; break;
  29. case 4: ioConv >> buffer; ioConv << element.wholesale; break;
  30. default:
  31. element.date_added->set(buffer, "/");
  32. break;
  33. }
  34. }
  35. fieldCnt++;
  36. if( fieldCnt == REC_FIELD_COUNT && m_is ) ret_val = true;
  37. }
  38.  
  39. return ret_val;
  40. }
  41. };
  42.  
  43.  
Thanks for any guidance...
John
I think I may have found the answer on another thread...
http://www.thescripts.com/forum/thread436124.html

It involves the post about a Proxy for the istream_iterator...

John
Oct 19 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Dot Kom | last post by:
I've got this text file full of lines like: A*1King Richard Dale*3Welton Orchard Rd*4Petersburg*5257-1234Î the columns are defined by the *#, so that line would break apart like: A*1King...
7
by: Santah | last post by:
hi I'm new to C++ and I'm currently working on Visual C++ 6.0 I'm trying to open a text file, and read some data from it part of the text file looks like this: --------
5
by: Ram Laxman | last post by:
Hi all, The below code doesnot print the string reading from the file? error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct...
19
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much...
4
by: Mark Stijnman | last post by:
A while ago I posted a question about how to get operator behave differently for reading and writing. I basically wanted to make a vector that can be queried about whether it is modified recently...
1
by: ungvichian | last post by:
So, right now I'm writing a program in VC++.Net with MFC, and one of the steps involves reading numeric values from a comma delimited file (like 4.56, 2.44, 3.453 etc.). The only methods I've been...
9
by: Alex Buell | last post by:
I have a small text file which consist of the following data: ]] And the code I've written is as follows: ]] The trouble is, I can't work out why it goes into an infinite loop reading the...
1
by: sam961 | last post by:
Hi, I am a beginner in C++ so I hope you can be patient on me since my purpose is to learn. I am now involved in a relatively complicated Data Analysis exercise. I have a series of frames...
6
by: =?Utf-8?B?UmljaA==?= | last post by:
'--this code works but only reads text into one column when contains multiple cols Dim ds1x As New DataSet Dim ConStr As String = _ "Provider=Microsoft.Jet.OLEDB.4.0;Data...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
jinu1996
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.