473,396 Members | 1,886 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

reading from text file

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

Sep 7 '05 #1
8 3429
On 7/9/05 22:34, in article
11**********************@g44g2000cwa.googlegroups. com, "ma******@gmail.com"
<ma******@gmail.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?"

Sep 7 '05 #2
ma******@gmail.com 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
Sep 7 '05 #3
ma******@gmail.com 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
Sep 7 '05 #4
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

Sep 8 '05 #5
ma******@gmail.com 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
kb******@gascad.at
Sep 8 '05 #6

<ma******@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr 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();
Sep 8 '05 #7

<ma******@gmail.com> wrote in message news:11**********************@g44g2000cwa.googlegr 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

Sep 8 '05 #8

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

Sep 9 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Suresh Kumaran | last post by:
Hi All, Does anybody know the sytax in VB.NET to write the contents of a multiline text box to a text file? Appreciate help. Suresh
1
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the...
19
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much...
0
by: Eric Lilja | last post by:
Hello, I have a text file that contains a number of entries describing a recipe. Each entry consists of a number of strings. Here's an example file with only one entry (recipe): Name=Maple Quill...
1
by: Magnus | last post by:
allrite folks, got some questions here... 1) LAY-OUT OF REPORTS How is it possible to fundamentaly change the lay-out/form of a report in access? I dont really know it that "difficult", but...
50
by: Michael Mair | last post by:
Cheerio, I would appreciate opinions on the following: Given the task to read a _complete_ text file into a string: What is the "best" way to do it? Handling the buffer is not the problem...
2
by: Sabin Finateanu | last post by:
Hi I'm having problem reading a file from my program and I think it's from a procedure I'm using but I don't see where I'm going wrong. Here is the code: public bool AllowUsage() { ...
4
by: dale zhang | last post by:
Hi, I am trying to save and read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp Right now, I saved images without any...
4
by: Amit Maheshwari | last post by:
I need to read text file having data either comma seperated or tab seperated or any custom seperator and convert into a DataSet in C# . I tried Microsoft Text Driver and Microsoft.Jet.OLEDB.4.0...
3
by: The Cool Giraffe | last post by:
Regarding the following code i have a problem. void read () { fstream file; ios::open_mode opMode = ios::in; file.open ("some.txt", opMode); char *ch = new char; vector <charv; while...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.