M
Michael Easterly
What is a good way to read a text file and read each line, then assign
data to variables? I have this so far,
#include <iostream>
#include <fstream>
using namespace std;
string BATCHNUMBER;
string BIS ="100";
string DEPARTMENT = "04";
string DESCRIPTION;
string DOCUMENTDATE;
string DOCUMENT;
string DUEDATE;
string FUND = "31Y";
string SCREEN = "31";
void main()
{
ifstream OpenFile;
OpenFile.open("youth.txt");
if(!OpenFile)
{
cout << "Error opening file! Aborting..." << endl;
exit(1);
}
else
cout << "File successfully opened..." << endl << endl;
cout << "Batch Number: ";
cin >> BATCHNUMBER;
cout << "Description: ";
cin >> DESCRIPTION;
cout << "Document Date: ";
cin >> DOCUMENTDATE;
cout << "Document: ";
cin >> DOCUMENT;
cout << "Due Date: ";
cin >> DUEDATE;
char line[50];
string vender[7];
while(!OpenFile.eof())
{
OpenFile.getline(line,50);
cout << line << endl;
}
OpenFile.close();
}
For example, if I want positions 1-7 to be assigned to a string
vender, then positions 10-12 to be assigned to a different string how
is that accomplished?
Here is a sample of the data file:
Y0TH004 210 100 50 5
Y0TH005 220 25 10 5
Y0TH008 200 50 60 55 1000
I have tested this and can read the file. After I get everything
loaded into variables, I will have to output to some other file to
format the data the way that is needed for an import to another
application.
Thanks for you help in advance.
data to variables? I have this so far,
#include <iostream>
#include <fstream>
using namespace std;
string BATCHNUMBER;
string BIS ="100";
string DEPARTMENT = "04";
string DESCRIPTION;
string DOCUMENTDATE;
string DOCUMENT;
string DUEDATE;
string FUND = "31Y";
string SCREEN = "31";
void main()
{
ifstream OpenFile;
OpenFile.open("youth.txt");
if(!OpenFile)
{
cout << "Error opening file! Aborting..." << endl;
exit(1);
}
else
cout << "File successfully opened..." << endl << endl;
cout << "Batch Number: ";
cin >> BATCHNUMBER;
cout << "Description: ";
cin >> DESCRIPTION;
cout << "Document Date: ";
cin >> DOCUMENTDATE;
cout << "Document: ";
cin >> DOCUMENT;
cout << "Due Date: ";
cin >> DUEDATE;
char line[50];
string vender[7];
while(!OpenFile.eof())
{
OpenFile.getline(line,50);
cout << line << endl;
}
OpenFile.close();
}
For example, if I want positions 1-7 to be assigned to a string
vender, then positions 10-12 to be assigned to a different string how
is that accomplished?
Here is a sample of the data file:
Y0TH004 210 100 50 5
Y0TH005 220 25 10 5
Y0TH008 200 50 60 55 1000
I have tested this and can read the file. After I get everything
loaded into variables, I will have to output to some other file to
format the data the way that is needed for an import to another
application.
Thanks for you help in advance.