|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
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
|
|
#3
|
||||
|
||||
|
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 |
|
#4
|
|||
|
|||
|
so then how would i get it to read everything in properly?
|
|
#5
|
||||
|
||||
|
getline means get a line of text. So get a line of text. Then parse that line to extract your data.
|
|
#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
|
|
#7
|
||||
|
||||
|
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. |
|
#8
|
||||
|
||||
|
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. |
|
#9
|
|||
|
|||
|
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? |
|
#10
|
||||
|
||||
|
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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Reading a file into a vector - c++ |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|