Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > reading from text file

Reply
Thread Tools

reading from text file

 
 
mahurshi@gmail.com
Guest
Posts: n/a
 
      09-07-2005
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

 
Reply With Quote
 
 
 
 
Steve
Guest
Posts: n/a
 
      09-07-2005
On 7/9/05 22:34, in article
. com, ""
<> wrote:

> 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?"

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      09-07-2005
wrote:
> 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
 
Reply With Quote
 
Sherm Pendley
Guest
Posts: n/a
 
      09-07-2005
writes:

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

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
 
Reply With Quote
 
mahurshi@gmail.com
Guest
Posts: n/a
 
      09-08-2005
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

 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      09-08-2005
wrote:
>
> 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.

--
Karl Heinz Buchegger

 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      09-08-2005

<> wrote in message
news: oups.com...
>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();


 
Reply With Quote
 
Alex Vinokur
Guest
Posts: n/a
 
      09-08-2005

<> wrote in message news: oups.com...
> 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...0fae8e5e065030

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

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

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





 
Reply With Quote
 
Alex Vinokur
Guest
Posts: n/a
 
      09-09-2005

Alex Vinokur wrote:

[snip]
> 1. To read file into string
> http://groups.google.com/group/perfo...0fae8e5e065030
>
> 2. To split string into vector of vectors (let vv be name of the vector).
> http://groups.google.com/group/perfo...c775cf7e3cdcf0

[snip]

Instead of two actions above you can read file to vector of vectors
directly:
http://groups.google.com/group/alt.s...a6823f110aa9cf

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading LAST line from text file without iterating through the file? Robin Wenger Java 191 03-26-2011 06:19 PM
Reading text file with wierd file extension? Lionel Python 22 02-03-2009 10:27 PM
UnauthorizedAccessException when reading XML files (no problem when reading other file-types) blabla120@gmx.net ASP .Net 0 09-15-2006 02:08 PM
reading from text file to excel file mail2atulmehta@yahoo.com C Programming 1 04-12-2005 07:50 PM
reading the DB vs. reading a text file...performance preference? Darrel ASP .Net 3 11-11-2004 02:27 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57