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:
  #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: 6
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: 6
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
Now serving number 42...
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Aug 2005
Location: San Antonio, Texas
Posts: 1,190 mvantuyl User rank is Major (30000 - 40000 Reputation Level)mvantuyl User rank is Major (30000 - 40000 Reputation Level)mvantuyl User rank is Major (30000 - 40000 Reputation Level)mvantuyl User rank is Major (30000 - 40000 Reputation Level)mvantuyl User rank is Major (30000 - 40000 Reputation Level)mvantuyl User rank is Major (30000 - 40000 Reputation Level)mvantuyl User rank is Major (30000 - 40000 Reputation Level)mvantuyl User rank is Major (30000 - 40000 Reputation Level)mvantuyl User rank is Major (30000 - 40000 Reputation Level)mvantuyl User rank is Major (30000 - 40000 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 3 Days 11 h 48 m 27 sec
Reputation Power: 368
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.
__________________
Physics is like sex. Sure, it may give some practical results, but that's not why we do it. ~ Richard Feynman

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: 6
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 online now
Contributing User
Dev Shed Specialist (4000 - 4499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 4,297 dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 3 Days 17 h 31 m 43 sec
Reputation Power: 1013
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: 6
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 online now
Contributing User
Dev Shed Specialist (4000 - 4499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 4,297 dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 3 Days 17 h 31 m 43 sec
Reputation Power: 1013
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
Crab
Click here for more information.
 
Join Date: Jun 2005
Posts: 5,032 sizablegrin User rank is General 36th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 36th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 36th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 36th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 36th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 36th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 36th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 36th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 36th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 36th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 36th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 36th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 36th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 36th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 36th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 36th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 1 Week 4 Days 11 h 16 m 10 sec
Reputation Power: 3388
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);
__________________
Antiquis temporibus, nati tibi similes in rupibus ventosissimis exponebantur ad necem. uno
Politically Incorrect DaWei on Pointers Grumpy on Exceptions

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: 6
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 online now
Contributing User
Dev Shed Specialist (4000 - 4499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 4,297 dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 3 Days 17 h 31 m 43 sec
Reputation Power: 1013
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
  #11  
Old June 8th, 2009, 05:43 PM
kacih46d kacih46d is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 2 kacih46d User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 25 m 34 sec
Reputation Power: 0
New Info

Hi,
did you get an answer to your question? I am trying to do the same thing you are without luck..help me.

Quote:
Originally Posted by eclipse150GT
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
  #12  
Old June 8th, 2009, 07:17 PM
sizablegrin's Avatar
sizablegrin sizablegrin is offline
Crab
Click here for more information.
 
Join Date: Jun 2005
Posts: 5,032 sizablegrin User rank is General 36th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 36th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 36th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 36th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 36th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 36th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 36th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 36th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 36th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 36th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 36th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 36th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 36th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 36th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 36th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 36th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 1 Week 4 Days 11 h 16 m 10 sec
Reputation Power: 3388
You should start your own thread, though you might reference this one by link if it seems appropriate. You should also show YOUR code; showing his does nothing for us. Further, no need to quote the whole thing - we have scroll bars!!!!1111oneeleven.

Be sure and read the "How to Post" thread before posting code, so you'll know to put it in [code] tags.

Reply With Quote
  #13  
Old June 10th, 2009, 10:35 PM
meaCulpa meaCulpa is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2009
Posts: 41 meaCulpa Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 8 h 47 m 35 sec
Reputation Power: 0
eclipse150GT, I think you are kind of on the right path but I think you are expecting too much from the getline function. The getline function reads a whole line from the file. After you have read the whole line you then need to parse it yourself.

Code:
 vector<char *> dataFromFile;
	char *cstr;

	if(file.is_open())
	{
		do
		{
			cstr = (char *)malloc(256);
			file.getline(cstr, 256);
			dataFromFile.push_back(cstr);
		}while(!file.eof());
		cout << "File successfully read" << endl;
	}	
	else
		cout << "Failed to open file" << endl;


The above code would read all the data from your text file. You will now have a vector that contains the five lines from lab4.txt. The entries in the vector will now contain the following lines:

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

Obviously you can't just use this data yet as it is not suitable for your purposes. What you need to do next is tokenize each line to get at the individual pieces of data in the string. Luckily there's a function which you can use to do this called strtok.

An example of how to do this is below:

Code:

vector<char *> tokens; //store all the tokens in this vector
char *pChar; 

pChar = strtok(dataFromFile[0], "-");
while (pChar != NULL)
{
  tokens.push_back(pChar);
  pChar = strtok (NULL, "-");
}

//now that you have divided the first string into 
//different parts you can convert them to the appropiate
//data and store them in entry

	entry.part_num = atoi(tokens[0]);
	entry.unit_price = atof(tokens[1]);
	entry.quantity = atoi(tokens[2]);
	strcpy(entry.description, tokens[3]);


As it happens I recently posted a thread about tokenizing[URL = "http://forums.devshed.com/c-programming-42/splitting-up-a-string-608721.html">here.[/URL]. You should read the thread to get a better understanding of what you are doing.

Just so it makes more sense the whole function is here:

Code:
void read_file(char name[15], vector<OrderRecord> &order)
{
	fstream file(name, ios::in);           
    OrderRecord entry;

    vector<char *> dataFromFile;
	char *cstr;

	if(file.is_open())
	{
		do
		{
			cstr = (char *)malloc(256);
			file.getline(cstr, 256);
			dataFromFile.push_back(cstr);
		}while(!file.eof());
		cout << "File successfully read" << endl;
	}	
	else
		cout << "Failed to open file" << endl;

	vector<char *> tokens;
	char *pChar;

	for(int i = 0; i < dataFromFile.size(); i++)
	{
		pChar = strtok(dataFromFile[i], "-");
		while (pChar != NULL)
		{
			tokens.push_back(pChar);
			pChar = strtok (NULL, "-");
		}

		entry.part_num = atoi(tokens[0]);
		entry.unit_price = atof(tokens[1]);
		entry.quantity = atoi(tokens[2]);
		strcpy(entry.description, tokens[3]);
		tokens.clear();
		order.push_back(entry);
	}

             //free the memory allocated earlier
	for(int i = 0; i < dataFromFile.size(); i++)
	{
		free(dataFromFile[i]);
	}

             //close the file
	file.close();
}


You should be aware of this from the other thread and consider adding this to be more complete:

Quote:
Originally Posted by sizablegrin
Conditioning your loop solely on EOF is not good enough. What if you have a failure that isn't due to EOF? Test your input operation for success after you perform it and before you use the data. If you don't, you may use some data twice, or not at all. See the good (), bad (), fail (), and eof () methods; note particularly that you must clear any error that occurs before you can use the stream again.


Also on my ide(Visual C++ 2005 express edition) I get warnings that strtok is deprecated. You might need to find the more updated version of this function or write one yourself.

Reply With Quote
  #14  
Old June 10th, 2009, 11:37 PM
sizablegrin's Avatar
sizablegrin sizablegrin is offline
Crab
Click here for more information.
 
Join Date: Jun 2005
Posts: 5,032 sizablegrin User rank is General 36th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 36th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 36th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 36th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 36th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 36th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 36th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 36th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 36th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 36th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 36th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 36th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 36th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 36th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 36th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 36th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 1 Week 4 Days 11 h 16 m 10 sec
Reputation Power: 3388
Strtok is fine, despite its warts. I would suggest, however, since this is C++, that string streams might represent a better approach.
Comments on this post
Lux Perpetua agrees: istringstream is the way to go.

Reply With Quote
  #15  
Old June 11th, 2009, 02:20 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is online now
Banned ;)
Dev Shed God 7th Plane (8000 - 8499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 8,100 Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 1 Day 9 h 5 m 8 sec
Reputation Power: 2059
Be warned that strtok() alters the string that is passed to it. If you don't want this, try using strsep(). Since you're using C++, use a stringstream instead, or you could write a tokenizer in a few lines of code. Google for C++ tokenizer for help.
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne

Puzzle of the Month solved by Keath and KevinADC, superior perl programmers of the month

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!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

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





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT