Need help reading a file

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2007
Posts: 1
Reputation: Back door daddy is an unknown quantity at this point 
Solved Threads: 0
Back door daddy Back door daddy is offline Offline
Newbie Poster

Need help reading a file

 
0
  #1
Nov 5th, 2007
I need to prompt the user for a txt file and display what the text in it. Our example is
January
Fishing line
132 spools 24 spools
Fish hooks
97 packages 45 packages
Sinkers
123 packages 37 packages
Fish nets
12 ea. 5 ea.
Tackle box
75 ea. 15 ea.
Fishing poles
135 ea. 18 ea.

The first line of the data file contains the month for which the report is being generated.
Then follows the description of a store item on the next line. Then follows how much inventory was present at the start of the month along with the quantity description.
Then follows how many of these items where sold during the month along with the quantity description. Then make it display like this
MONTH: January

ITEM BEGIN QTY UNITS SOLD ENDING QTY %SOLD
-------------- --------- ---------- ---------- -----
Fishing line 132 spools 24 108 18.18
Fish hooks 97 packages 45 52 46.39
Sinkers 123 packages 37 86 30.08
Fish nets 12 ea. 5 7 41.67

I can do the calculations and everything but im so confused on how to read the info from a file this is what i have so far and im totally confused on how to read each part from the file and then assign it to a variable.
This is what i got so far:

#include <fstream>
#include <iostream>
#include <string>
#include <cstdlib>

#define cls system("cls")

using namespace std;



string getInputFileName();


int main ()
{
ifstream inFile;
string fileName;

fileName = getInputFileName();

inFile.open(fileName.c_str());

if (!inFile)
{
cerr << "Unable to open file " << fileName << endl;
exit(1);
}




inFile.close();
return 0;
}
string getInputFileName()
{
string fName;

cout << "Please enter the fully qualified name of the " << endl
<< "input text file (i.e. including the path): ";
cin >> fName;
cout << endl;

return fName;
}

after i know how to read in the file i can do the rest, i just need some help or a hint on how to do that! Thank you.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,596
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 712
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Need help reading a file

 
0
  #2
Nov 5th, 2007
Reading from a file is just like reading from the keyboard. Let's say you want to read the first two records, you would do it from cin like this (if you were me):
  1. std::string month;
  2.  
  3. // Read the month
  4. if ( getline ( std::cin, month ) ) {
  5. std::cout<< month <<"\n\n"
  6. << report_header <<'\n'
  7. << report_separator <<'\n';
  8.  
  9. std::string product;
  10. std::string details;
  11.  
  12. // Assume paired lines denote a record
  13. while ( getline ( std::cin, product ) ) {
  14. if ( !getline ( std::cin, details ) )
  15. break;
  16.  
  17. std::cout<< format_record ( product, details ) <<'\n';
  18. }
  19. }
To read from a file, just use the file stream object instead of std::cin. It's just that simple.

format_record would create a detail line for your report. Really all you have to do in it is split the details string into words and do your calculations on the first and third word. You can ignore the last word because it's redundant:
  1. std::string format_record (
  2. std::string product, std::string details )
  3. {
  4. std::stringstream splitter ( details );
  5. std::string unit;
  6. int quantity;
  7. int sold;
  8.  
  9. if ( !( splitter>> quantity >> unit >> sold ) )
  10. return "";
  11.  
  12. // Do your calculations here
  13. // Use an output stringstream to format the results
  14.  
  15. return formatter.str();
  16. }
That should be more than enough to get you started. Keep in mind that you don't have to use the stringstream if your instructor doesn't allow it. You can read formatted input directly from the file instead of using getline. That's an alternative that I'll leave to you.
Last edited by Narue; Nov 5th, 2007 at 5:30 pm.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC