944,183 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1488
  • C++ RSS
Nov 5th, 2007
0

Need help reading a file

Expand Post »
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Back door daddy is offline Offline
1 posts
since Nov 2007
Nov 5th, 2007
0

Re: Need help reading a file

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):
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Read integers from a file
Next Thread in C++ Forum Timeline: returning values from one exe to another exe.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC