| | |
Need help reading a file
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2007
Posts: 1
Reputation:
Solved Threads: 0
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.
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.
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):
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:
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.
C++ Syntax (Toggle Plain Text)
std::string month; // Read the month if ( getline ( std::cin, month ) ) { std::cout<< month <<"\n\n" << report_header <<'\n' << report_separator <<'\n'; std::string product; std::string details; // Assume paired lines denote a record while ( getline ( std::cin, product ) ) { if ( !getline ( std::cin, details ) ) break; std::cout<< format_record ( product, details ) <<'\n'; } }
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)
std::string format_record ( std::string product, std::string details ) { std::stringstream splitter ( details ); std::string unit; int quantity; int sold; if ( !( splitter>> quantity >> unit >> sold ) ) return ""; // Do your calculations here // Use an output stringstream to format the results return formatter.str(); }
Last edited by Narue; Nov 5th, 2007 at 5:30 pm.
I'm here to prove you wrong.
![]() |
Similar Threads
- problems with reading random access line from a file (C++)
- Reading a file into a Parallel Array (C++)
- problems with reading in file (C++)
- First year assigment on reading file, sorting and outputting invoice (C++)
- Error Message Concerning Reading File From A Drive (C++)
- URGENT - Reading from txt file into a 2 dimension array (Java)
- reading a file into code (Java)
- reading and printing a file to screen in C (C)
Other Threads in the C++ Forum
- Previous Thread: Read integers from a file
- Next Thread: returning values from one exe to another exe.
| Thread Tools | Search this Thread |
api array beginner bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion count database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






