C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Dell PowerEdge Servers
  #1  
Old February 6th, 2008, 11:58 AM
eclipse150GT eclipse150GT is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 138 eclipse150GT User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 h 47 m 55 sec
Reputation Power: 4
Reading a file into a vector - c++

i have to make a c++ program which has a struct declared and makes a vector of that struct then allows the user to enter a file name and reads the data from that file into the program however i am running into to problems figuring out how to get the program to work here is what i have so far....


Code:
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;


struct OrderRecord
{
       int part_num;
       double unit_price;
       int quantity;
       char description[15];
};


void read_file(char name[15],
               vector<OrderRecord> &order)
{
               
               fstream file(name, ios::in);
               
               OrderRecord entry;
               
               do
               {
                           file.getline(entry.part_num, 6, '-');
                           file.getline(entry.unit_price, 5, '-');
                           file.getline(entry.quantity, 6, '-');
                           file.getline(entry.description, 15, '-');
                           order.push_back(entry);
               }while(!file.eof());
               
}
               




int main(int argc, char *argv[])
{
    
    vector<OrderRecord> order;
    char name[15];
    char choice;
    
    do
    {
         
         cout << "\n1. Enter a file name to read from" << endl;
         cout << "2. Print the Order Record" << endl;
         cout << "Q. Quit" << endl;
         cin >> choice;
         
         switch(choice)
         {
                       case '1':
                            
                            cout << "Please enter the name of a file: " << endl;
                            cin >> name;
                            
                            read_file(name, order);
                            break;
                            
                       case '2':
                            
                            //print_order(order);
                            break;
                            
                       case 'q':
                            
                            choice = 'Q';
                            
                       case 'Q':
                            
                            break;
                            
                       default:
                               
                            cout << "Invalid Choice" << endl;
                            
         }
         
    }while(choice != 'Q');
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}


samples input would be a file like.....

123456-2.25-10-widgets
987654-17.25-20-sprockets


any help would be appreciated

Reply With Quote
  #2  
Old February 6th, 2008, 01:29 PM
eclipse150GT eclipse150GT is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 138 eclipse150GT User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 h 47 m 55 sec
Reputation Power: 4
the only thing i am getting errors with is the file.getlines im not sure how to get those to work the way i need

Reply With Quote
  #3  
Old February 6th, 2008, 01:47 PM
mvantuyl's Avatar
mvantuyl mvantuyl is offline
Blithering idiot
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Aug 2005
Location: San Antonio, Texas
Posts: 1,157 mvantuyl User rank is Captain (20000 - 30000 Reputation Level)mvantuyl User rank is Captain (20000 - 30000 Reputation Level)mvantuyl User rank is Captain (20000 - 30000 Reputation Level)mvantuyl User rank is Captain (20000 - 30000 Reputation Level)mvantuyl User rank is Captain (20000 - 30000 Reputation Level)mvantuyl User rank is Captain (20000 - 30000 Reputation Level)mvantuyl User rank is Captain (20000 - 30000 Reputation Level)mvantuyl User rank is Captain (20000 - 30000 Reputation Level)mvantuyl User rank is Captain (20000 - 30000 Reputation Level)  Folding Points: 48973 Folding Title: Beginner FolderFolding Points: 48973 Folding Title: Beginner FolderFolding Points: 48973 Folding Title: Beginner Folder
Time spent in forums: 3 Weeks 2 Days 22 h 25 m 26 sec
Reputation Power: 284
Send a message via ICQ to mvantuyl
The getline function requires a char * as its first parameter. part_num and quantity are ints and unit_price is a double.
__________________
Frodo failed - Bush has the ring

How To Ask Questions The Smart Way by Eric S. Raymond

Reply With Quote
  #4  
Old February 6th, 2008, 02:04 PM
eclipse150GT eclipse150GT is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 138 eclipse150GT User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 h 47 m 55 sec
Reputation Power: 4
so then how would i get it to read everything in properly?

Reply With Quote
  #5  
Old February 6th, 2008, 02:27 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,704 dwise1_aol User rank is Major (30000 - 40000 Reputation Level)dwise1_aol User rank is Major (30000 - 40000 Reputation Level)dwise1_aol User rank is Major (30000 - 40000 Reputation Level)dwise1_aol User rank is Major (30000 - 40000 Reputation Level)dwise1_aol User rank is Major (30000 - 40000 Reputation Level)dwise1_aol User rank is Major (30000 - 40000 Reputation Level)dwise1_aol User rank is Major (30000 - 40000 Reputation Level)dwise1_aol User rank is Major (30000 - 40000 Reputation Level)dwise1_aol User rank is Major (30000 - 40000 Reputation Level)dwise1_aol User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 4 Weeks 1 h 53 m 33 sec
Reputation Power: 319
getline means get a line of text. So get a line of text. Then parse that line to extract your data.

Reply With Quote
  #6  
Old February 6th, 2008, 02:39 PM
eclipse150GT eclipse150GT is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 138 eclipse150GT User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 h 47 m 55 sec
Reputation Power: 4
then what would i need to put as the first arguement for the getline? and then after that how would i parse the data like i need and would i still need a - between every piece of info

Reply With Quote
  #7  
Old February 6th, 2008, 03:17 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,704 dwise1_aol User rank is Major (30000 - 40000 Reputation Level)dwise1_aol User rank is Major (30000 - 40000 Reputation Level)dwise1_aol User rank is Major (30000 - 40000 Reputation Level)dwise1_aol User rank is Major (30000 - 40000 Reputation Level)dwise1_aol User rank is Major (30000 - 40000 Reputation Level)dwise1_aol User rank is Major (30000 - 40000 Reputation Level)dwise1_aol User rank is Major (30000 - 40000 Reputation Level)dwise1_aol User rank is Major (30000 - 40000 Reputation Level)dwise1_aol User rank is Major (30000 - 40000 Reputation Level)dwise1_aol User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 4 Weeks 1 h 53 m 33 sec
Reputation Power: 319
The first argument of getline is a char*. That means it expects a C-style string. That means you need to give it the name of the char array that you want that string to go into. Think maybe that might be what you should give it?

After that you just identify which characters in that string belong to what fields, then extract them and convert them to the appropriate data types for your struct's fields.

BTW, if this is a C++ assignment that has you using vectors, shouldn't you also be using the basic string class instead of C-style strings? Though that still wouldn't change the need to feed getline a C-style string.

Reply With Quote
  #8  
Old February 6th, 2008, 03:42 PM
sizablegrin's Avatar
sizablegrin sizablegrin is offline
Stubborn ol' L'User
Click here for more information.
 
Join Date: Jun 2005
Posts: 2,740 sizablegrin User rank is General 3rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 3rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 3rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 3rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 3rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 3rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 3rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 3rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 3rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 3rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 3rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 3rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 3rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 3rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 3rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 3rd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 4 h 14 m 44 sec
Reputation Power: 1193
There's another form of getline designed for the string class. You'll need to include <string>, of course.

string::getline (input stream, target string, delimiters);
__________________
The population in my hometown has been stable for 50 years. Every time a woman gets pregnant, a man leaves.

Reply With Quote
  #9  
Old February 7th, 2008, 02:56 PM
eclipse150GT eclipse150GT is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 138 eclipse150GT User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 h 47 m 55 sec
Reputation Power: 4
ok i got it compiling now but when i run it i type in the file name and it sits there for a second and then it close the window because of an error. here is my code

Code:
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;


struct OrderRecord
{
       int part_num;
       double unit_price;
       int quantity;
       char description[15];
};


void read_file(char name[15],
               vector<OrderRecord> &order)
{
               
               fstream file(name, ios::in);
               char part[6];
               char price[5];
               char quant[6];
               char desc[15];
               
               OrderRecord entry;
               
               do
               {
                           file.getline(part, 6, '-');
                           file.getline(price, 5, '-');
                           file.getline(quant, 6, '-');
                           file.getline(entry.description, 15, '-');
                           
                           entry.part_num = atoi(part);
                           entry.unit_price = atof(price);
                           entry.quantity = atoi(quant);
                           
                           
                           order.push_back(entry);
               }while(!file.eof());
               
}


void print_order(vector<OrderRecord> &order)
{
     double subtotal = 0.0;
     double total = 0.0;
     
     cout << "\n\nItem\tDescription\tPrice\tQuantity\tSubtotal" << endl;
     
     vector<OrderRecord>::iterator iter;
     
     for(iter = order.begin();iter < order.end(); iter++)
     {
              subtotal = iter->unit_price * iter->quantity;
              total += subtotal;
              
              cout << iter->part_num << "\t" << iter->description << "\t" << iter->unit_price
                   << "\t" << iter->quantity << "\t" << subtotal << endl;
                   
     }
     
     cout << "-------------------------------------------------------" << endl;
     cout << "Invoice Total: " << total << endl;
     
     
}               




int main(int argc, char *argv[])
{
    
    vector<OrderRecord> order;
    char name[15];
    char choice;
    
    do
    {
         
         cout << "\n1. Enter a file name to read from" << endl;
         cout << "2. Print the Order Record" << endl;
         cout << "Q. Quit" << endl;
         cin >> choice;
         
         switch(choice)
         {
                       case '1':
                            
                            cout << "Please enter the name of a file: " << endl;
                            cin >> name;
                            
                            read_file(name, order);
                            break;
                            
                       case '2':
                            
                            print_order(order);
                            break;
                            
                       case 'q':
                            
                            choice = 'Q';
                            
                       case 'Q':
                            
                            break;
                            
                       default:
                               
                            cout << "Invalid Choice" << endl;
                            
         }
         
    }while(choice != 'Q');
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}



and here is what the input file looks like which is name lab4.txt

123456-2.75-25-widgets-
248567-7.25-15-sprockets-
452684-19.00-30-motherboard-
412685-1.50.-100-bolts-
751425-3.75-50-screws-


any suggestions?

Reply With Quote
  #10  
Old February 7th, 2008, 03:05 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,704 dwise1_aol User rank is Major (30000 - 40000 Reputation Level)dwise1_aol User rank is Major (30000 - 40000 Reputation Level)dwise1_aol User rank is Major (30000 - 40000 Reputation Level)dwise1_aol User rank is Major (30000 - 40000 Reputation Level)dwise1_aol User rank is Major (30000 - 40000 Reputation Level)dwise1_aol User rank is Major (30000 - 40000 Reputation Level)dwise1_aol User rank is Major (30000 - 40000 Reputation Level)dwise1_aol User rank is Major (30000 - 40000 Reputation Level)dwise1_aol User rank is Major (30000 - 40000 Reputation Level)dwise1_aol User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 4 Weeks 1 h 53 m 33 sec
Reputation Power: 319
So what is the error that it gives you? My crystal ball is in the shop and you'd have to get within arm's length for me to perform a mind-meld.


Though I noticed here:
Code:
               fstream file(name, ios::in);
               char part[6];
               char price[5];
               char quant[6];
               char desc[15];
               
               OrderRecord entry;
               
               do
               {
                           file.getline(part, 6, '-');
                           file.getline(price, 5, '-');
                           file.getline(quant, 6, '-');
                           file.getline(entry.description, 15, '-');
 

You're trying to squeeze 7- and 6-character sequences into 6- and 5-character buffers. That just will not do! When working with C-style strings, you must declare your buffers to be big enough to hold the string that you intend to store in it. That must include one more character space for the null-terminator. Remember, a string that's six characters long will take up seven bytes in memory.

PS
Why are you calling the getline() method each time? getline() reads in an entire line, at least up to the count you give it. Since all the fields for a given item are on a single line, why do you keep skipping to the next lines before you're even done with that item?

As we have advised you already, do one getline() to read in a single item, then extract that item's data out of its fields.

Last edited by dwise1_aol : February 7th, 2008 at 03:15 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Reading a file into a vector - c++


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |