Assigning a portion of a getline to a variable

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.
 
M

Mike Wahler

Michael Easterly said:
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";

Convention is to only use all upper case identifiers
for macros.
void main()

int main()
{
ifstream OpenFile;
OpenFile.open("youth.txt");
if(!OpenFile)
{
cout << "Error opening file! Aborting..." << endl;
exit(1);

IMO better would be:

return EXIT_FAILURE; /* needs #incluce said:
}
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())

eof() does not return true until *after* a failed read attempt.
{
OpenFile.getline(line,50);
cout << line << endl;
}
OpenFile.close();

The ifstream destructor will automatically close the
file for you. This statement is redundant (but harmless).
}

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

You can address specific positions in a std::string with its
[] operator, and you can get subsequences out of a std::string
with its 'substr()' member functions. BUT:
Your data does not appear to be column-aligned, but simply
separated by whitespace. So extracting particular columns
won't work.
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.

The following extracts each line as a 'record', stores each
whitespace-separated string into a 'field' of each record,
and outputs the data to cout (separating 'fields' with spaces,
and 'records' with newlines:


#include <cstdlib>
#include <fstream>
#include <iostream>
#include <istream>
#include <ostream>
#include <sstream>
#include <string>

class Record
{
std::string line;
std::string batchnumber;
std::string description;
std::string documentdate;
std::string document;
std::string duedate;

friend std::istream& operator>>(std::istream& is, Record& r)
{
return is >> r.line}

friend std::eek:stream& operator<<(std::eek:stream& os, const Record& r)
{
return os << r.line << ' '
<< r.batchnumber << ' '
<< r.description << ' '
<< r.documentdate << ' '
<< r.document << ' '
<< r.duedate;
}
};

const int status[] = {EXIT_SUCCESS, EXIT_FAILURE};

int main()
{
std::string filename("data.txt");
std::ifstream input(filename.c_str());
bool err(!input);

if(!err)
{
std::string line;

while(std::getline(input, line))
{
std::istringstream iss(line);
Record rec;
iss >> rec;
std::cout << rec << '\n';
}

if(err = (!input && !input.eof()))
cerr << "Error reading file '" << filename << "'\n";
}
else
cerr << "Cannot open file '" << filename << "'\n";

return status[err];
}

-Mike
 
D

David Harmon

On 28 May 2004 11:20:27 -0700 in comp.lang.c++, (e-mail address removed)
(Michael Easterly) wrote,
What is a good way to read a text file and read each line, then assign
data to variables?

There is no single answer to that, it depends on many things such as how
complex your data format is, and what kind of error recovery you
require. But,
while(!OpenFile.eof())
{

that isn't part of it. Don't loop on eof(), instead read until reading
fails. Then check eof() to see if that was the reason, if you still
care.

std::string line;
while(std::getline(openfile, line)
{
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

OK, your fields appear to be delimited by whitespace. That makes things
easy, since that's the default behavior for stream formatting
operator>>(). You could do something like:

while(std::getline(openfile, line)
{
std::istringstream parse(line);
parse >> f1 >> f2 >> f3 >> f4;
if(parse.good())
// do something with the data.
}

For more complicated input format, one of the first things I think of
these days is the regex library from http://www.boost.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top