reading from text file

M

mahurshi

i have a simple question guys

i have to read a file that looks more or less like this


AND 1 2 3
OR 2 3 4
INV 5 2
BUF 7 1
AND 1 2

INPUT 1 2 3 4 -1
OUTPUT 5 7 -1


(i just made up some random numbers but i guess that gives an idea of
the input file format)

onw what i want to do is read this input into a structure (string name,
int array of inputs, int one output .. the value of these ints being
the values next to the logic gate)

so for example

name = AND, inputs[0] = 1, input[1] = 2, output = 3


my question is ... how do i parse this file ? (i can do the rest
myself)

i know how to read the whole line using getline .. but it is making
things too complicated for me as the input has both characters and
integers and sometimes the number of integers is varying (e.g. INV is
followed by only 2 numbers).. and I have to somehow split these fields.

i can write this entire stuff in perl in about 3 or so lines (using =~
pattern matching) but i'd really appreciate it if someone can give me
an idea on how i can parse this file easily.


thanks
 
S

Steve

i can write this entire stuff in perl in about 3 or so lines (using =~
pattern matching) but i'd really appreciate it if someone can give me
an idea on how i can parse this file easily.

Read up on the std::istream operator >>

--
Regards,
Steve

"...which means he created the heaven and the earth... in the DARK! How good
is that?"
 
V

Victor Bazarov

i have to read a file that looks more or less like this


AND 1 2 3
[..]

onw what i want to do is read this input into a structure (string name,
int array of inputs, int one output .. the value of these ints being
the values next to the logic gate)

so for example

name = AND, inputs[0] = 1, input[1] = 2, output = 3


my question is ... how do i parse this file ? (i can do the rest
myself)

Read a line into a string. Define an istringstream from that string.
Read another string and then integer numbers from that istringstream
until the end of the istringstream. The string will have your word,
the number will contain the running thing. You can branch based on
the word as soon as you have it. If the number of integers is not
known, read the first, then if more, push it into a vector<int> and
read the other. Repeat until empty. You will have a vector<int> with
all integers less the last one, and you will have just read the last
number. Or just keep pushing and then extract the last one...

V
 
S

Sherm Pendley

i can write this entire stuff in perl in about 3 or so lines (using =~
pattern matching)

In addition to what others have said, if you're interested in using regular
expressions and pattern matching, you might have a look at the regex classes
included with boost:

<http://www.boost.org>

Those classes aren't part of the official standard yet, but they're on the
list of additions being discussed for future versions of the standard.

sherm--
 
M

mahurshi

Thanks for the info guys.

I am gonna look up and see if I get get somewhere with this, otherwise
I will just bail out into using perl :) (my prof believes it is v.
easy in c/c++ ... well, we'll see)

And if I have questions, I will post them here (gimme a couple of days
to figure eveyrthing out)

Thanks
 
K

Karl Heinz Buchegger

Thanks for the info guys.

I am gonna look up and see if I get get somewhere with this, otherwise
I will just bail out into using perl :) (my prof believes it is v.
easy in c/c++ ... well, we'll see)

It *can* be made very easy and it *can* be made very complicated.
It all depends on what level you want to guard your program
against file format errors.

A simple solution would be:

open file
check for file open errors

std::string type;
int Input[20];
int Output;

while( file >> type ) {

if( type == "AND" || type == "OR" )
file >> Input[0] >> Input[1] >> Output;

if( type == "INV" )
file >> Input[0] >> Output;

...
}

The problem with this code is, that it lacks any sort of handling
input format errors. As long as the file is perfect, it works perfectly.
At the moment someone made a mistake in the file, it all screws up.
 
J

Jim Langston

i have a simple question guys

i have to read a file that looks more or less like this


AND 1 2 3
OR 2 3 4
INV 5 2
BUF 7 1
AND 1 2

INPUT 1 2 3 4 -1
OUTPUT 5 7 -1


(i just made up some random numbers but i guess that gives an idea of
the input file format)

onw what i want to do is read this input into a structure (string name,
int array of inputs, int one output .. the value of these ints being
the values next to the logic gate)

so for example

name = AND, inputs[0] = 1, input[1] = 2, output = 3


my question is ... how do i parse this file ? (i can do the rest
myself)

Maybe this will help:
//////////////////////
// Particle Streams //
//////////////////////
int PSNumber = 0;
std::ifstream PSFile("data/PS.DAT");
if (PSFile.is_open())
{
while (! PSFile.eof() )
{
CPSData PSBuffer;
std::string FileName;
if ( PSFile >> PSBuffer.x >> PSBuffer.y >> PSBuffer.z >> FileName )
{
FileName = "data/" + FileName;
PSBuffer.PSNumber = PSNumber;
Client.PSData.push_back( PSBuffer );
jCreate_PS_From_File(PSNumber++, const_cast<char*>(FileName.c_str()));
}
}
}
PSFile.close();
 
A

Alex Vinokur

i have a simple question guys

i have to read a file that looks more or less like this


AND 1 2 3
OR 2 3 4
INV 5 2
BUF 7 1
AND 1 2

INPUT 1 2 3 4 -1
OUTPUT 5 7 -1


(i just made up some random numbers but i guess that gives an idea of
the input file format)

onw what i want to do is read this input into a structure (string name,
int array of inputs, int one output .. the value of these ints being
the values next to the logic gate)

so for example

name = AND, inputs[0] = 1, input[1] = 2, output = 3


my question is ... how do i parse this file ? (i can do the rest
myself)
[snip]

Somehing like this.

1. To read file into string
http://groups.google.com/group/perfo/msg/530fae8e5e065030

2. To split string into vector of vectors (let vv be name of the vector).
http://groups.google.com/group/perfo/msg/f3c775cf7e3cdcf0

3. for (int i = 0; i < vv.size(); i++)
{
name = vv[0];
for (int j = 1; j < vv.size() - 1; j++) input [j-1] = atoi(vv[j]);
output = atoi(vv[vv.size() - 1]);
}
 
A

Alex Vinokur

Alex Vinokur wrote:

[snip]
1. To read file into string
http://groups.google.com/group/perfo/msg/530fae8e5e065030

2. To split string into vector of vectors (let vv be name of the vector).
http://groups.google.com/group/perfo/msg/f3c775cf7e3cdcf0
[snip]

Instead of two actions above you can read file to vector of vectors
directly:
http://groups.google.com/group/alt.sources/msg/5ba6823f110aa9cf

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn
 

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

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top