473,508 Members | 2,342 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reading data from a text file

I have a small text file which consist of the following data:
[[[
-1 -1 -1 1
You are in a small square room.
0 -1 -1 -1
You are in a big round room.
]]]

And the code I've written is as follows:
[[[
Rooms getRoomDataRecords(string roomDataFile)
{
Rooms rooms;

ifstream ifs(roomDataFile.c_str());
if (!ifs.is_open())
{
cout << "Eh? Could not open " << roomDataFile << " for
reading." << endl;
return rooms;
}

while (!ifs.eof())
{
int n, w, e, s;
string line;

ifs >> n >> w >> e >> s;
cout << "1: " << n << " " << w << " " << e << " " <<
s<< endl;
getline(ifs, line);
cout << "2: " << line << endl;

rooms.push_back(Room(n, w, e, s, line));
}

ifs.close();
return rooms;
}
]]]

The trouble is, I can't work out why it goes into an infinite loop
reading the information from the text file!

Can anyone enlighten me as to what I am doing wrong?

--
http://www.munted.org.uk

"Honestly, what can I possibly say to get you into my bed?" - Anon.
Mar 17 '06 #1
9 2743
Hi,
I can see the below code is working fine...

char* pLine = new char[200];
ifstream DataFile("Data.txt");
while(!DataFile.eof())
{
DataFile.getline( pLine, 100 );
printf("%s\n",pLine);
}

Best regards,
Amal P

Mar 17 '06 #2
On 17 Mar 2006 03:31:48 -0800 "Amal P" <en********@gmail.com> waved a
wand and this message magically appeared:
Hi,
I can see the below code is working fine...

char* pLine = new char[200];
ifstream DataFile("Data.txt");
while(!DataFile.eof())
{
DataFile.getline( pLine, 100 );
printf("%s\n",pLine);
}


Yes, that is absolutely fine but I want to read in the digits from the
first line in the text file into four integer variables.

--
http://www.munted.org.uk

"Honestly, what can I possibly say to get you into my bed?" - Anon.
Mar 17 '06 #3
Alex Buell <al********@munted.org.uk> wrote:
I have a small text file which consist of the following data:
[[[
-1 -1 -1 1
You are in a small square room.
0 -1 -1 -1
You are in a big round room.
]]]

And the code I've written is as follows:
[[[
Rooms getRoomDataRecords(string roomDataFile)
{
Rooms rooms;

ifstream ifs(roomDataFile.c_str());
if (!ifs.is_open())
{
cout << "Eh? Could not open " << roomDataFile << " for
reading." << endl;
return rooms;
}

while (!ifs.eof())
{
int n, w, e, s;
string line;

ifs >> n >> w >> e >> s;
cout << "1: " << n << " " << w << " " << e << " " <<
s<< endl;
getline(ifs, line);
cout << "2: " << line << endl;

rooms.push_back(Room(n, w, e, s, line));
}

ifs.close();
return rooms;
}
]]]

The trouble is, I can't work out why it goes into an infinite loop
reading the information from the text file!

Can anyone enlighten me as to what I am doing wrong?


http://www.parashift.com/c++-faq-lite/input-output.html might help
you.

hth
--
jb

(reply address in rot13, unscramble first)
Mar 17 '06 #4
On Fri, 17 Mar 2006 09:55:59 +0000 Alex Buell
<al********@munted.org.uk> waved a wand and this message magically
appeared:
Can anyone enlighten me as to what I am doing wrong?


Not to worry, I solved it, there was a hidden bug which made it
harder to track down the problem (attempted to write to memory not
belonging to program) . Once solved, all was well.

--
http://www.munted.org.uk

"Honestly, what can I possibly say to get you into my bed?" - Anon.
Mar 17 '06 #5
In article
<20********************************@munted.org.uk> ,
al********@munted.org.uk says...
I have a small text file which consist of the following data:
[[[
-1 -1 -1 1
You are in a small square room.
0 -1 -1 -1
You are in a big round room.
]]]
I see elsethread that you've already solved your problem,
but thought I'd add a few points anyway.

Though you don't show its definition, you apparently have
a struct something like this:

struct Room {
int n, w, e, s;
string line;
};

From the looks of the rest of your code, that probably
also defines a ctor, but I'll ignore that since none of
the other code needs it the way I'm writing things. I'd
add a stream extraction operator to read in the data (if
the class/struct makes the data private, it'll probably
need to declare this as a friend):

std::istream &operator>>(std::istream &is, room &r) {
is >> r.n >> r.w >> r.e >> r.s;
std::getline(is, r.line);
return is;
}

Then you can copy your data from the file into a vector
with code something like this (skipping error handling
for the moment):

typedef std::vector<Room> Rooms;

Rooms getRoomDataRecords(std::string roomDataFile) {
Rooms rooms;
ifstream ifs(roomDataFile.c_str());
std::copy(
std::istream_iterator<Room>(ifs),
std::istream_iterator<Room>(),
std::back_inserter(rooms));
return rooms;
}

A couple of basic ideas here: first and foremost, this
keeps all knowledge of the file format associated
directly with the struct/class that represents a record
from the file instead of having that knowledge spread
throughout the code in general.

Second, using the algorithm from the standard library to
copy the data not only clarifies what we're really doing,
but eliminates quite a few kinds of bugs, simply because
we're no longer writing the code that could contain those
bugs.

[ ... ]
The trouble is, I can't work out why it goes into an infinite loop
reading the information from the text file!


One way to avoid that is to avoid writing a loop of your
own at all. Just so you realize, almost any loop of the
form:

while (!x.eof())

is nearly guaranteed to be wrong.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Mar 20 '06 #6
On Sun, 19 Mar 2006 21:40:20 -0700 Jerry Coffin <jc*****@taeus.com>
waved a wand and this message magically appeared:
A couple of basic ideas here: first and foremost, this
keeps all knowledge of the file format associated
directly with the struct/class that represents a record
from the file instead of having that knowledge spread
throughout the code in general.

Second, using the algorithm from the standard library to
copy the data not only clarifies what we're really doing,
but eliminates quite a few kinds of bugs, simply because
we're no longer writing the code that could contain those
bugs.
Excellent, the code does look a lot better with these changes. I
particularly like the solution using copy. How would that work
with typedef Room* Rooms instead, using new and delete?
One way to avoid that is to avoid writing a loop of your
own at all. Just so you realize, almost any loop of the
form:

while (!x.eof())

is nearly guaranteed to be wrong.


Yeah, I figured that out the other day. Thanks for your input.

--
http://www.munted.org.uk

"Honestly, what can I possibly say to get you into my bed?" - Anon.
Mar 20 '06 #7
In article
<20********************************@munted.org.uk> ,
al********@munted.org.uk says...

[ ... ]
Excellent, the code does look a lot better with these changes. I
particularly like the solution using copy. How would that work
with typedef Room* Rooms instead, using new and delete?


I'd have to dig the code back up to give specifics, but
in general I'd simply avoid that. A vector is basically a
smart front-end to hold a pointer (and maybe a little
more data with it, like the current allocation size and
amount of that space that's in use) along with code to
manage the allocated memory. Unless you have a fairly
specific reason to do so, you're better off using a
container than a raw pointer.

When/if you do decide that none of the existing
containers will work, your next step would usually be to
write a different container that still supports (at least
most of) the usual interface.

When you need to is soon enough to design your own
storage from the ground up though...

--
Later,
Jerry.

The universe is a figment of its own imagination.
Mar 21 '06 #8
On Mon, 20 Mar 2006 23:39:20 -0700 Jerry Coffin <jc*****@taeus.com>
waved a wand and this message magically appeared:
Excellent, the code does look a lot better with these changes. I
particularly like the solution using copy. How would that work
with typedef Room* Rooms instead, using new and delete?


I'd have to dig the code back up to give specifics, but
in general I'd simply avoid that. A vector is basically a
smart front-end to hold a pointer (and maybe a little
more data with it, like the current allocation size and
amount of that space that's in use) along with code to
manage the allocated memory. Unless you have a fairly
specific reason to do so, you're better off using a
container than a raw pointer.


I tried doing <Rooms *> but found it too hard to use properly. So I've
stuck with <Rooms>. I just implemented my command handler, but it could
do with improvements, so here it is (all input is assumed to be lower
case but I can always change that to handle upper case):

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>

#include "command.hpp"
#include "room.hpp"

using namespace std;

Command::Command(string input)
{
stringstream ss(input);
vector<string> tokens;
string buffer;

while (ss >> buffer)
tokens.push_back(buffer);

if (tokens.size() == 0)
{
commandVerb = unknown_verb;
return;
}

commandVerb = parseVerb(tokens.at(0));

if (tokens.size() > 1)
switch (commandVerb)
{
case go:
commandDirection = parseDirection
(tokens.at(1)); break;

default:
break;
}
else
switch (commandVerb)
{
case go:
commandDirection = unknown_direction;
break;

default:
break;
}
}

verb Command::getVerb() const
{
return commandVerb;
}

string Command::getNoun() const
{
return commandNoun;
}

direction Command::getDirection() const
{
return commandDirection;
}

verb Command::parseVerb(string verbString) const
{
if (verbString == "quit")
return quit;

if (verbString == "go")
return go;

return unknown_verb;
}

direction Command::parseDirection(string directionString) const
{
if (directionString == "north")
return north;

if (directionString == "west")
return west;

if (directionString == "east")
return east;

if (directionString == "south")
return south;

return unknown_direction;
}

In main part this is used:

getline(cin, s);

if (s.length() != 0)
{
Command command(s);

switch (command.getVerb())
{
case go:
if (command.getDirection() != unknown_direction)
if (room.canGo(command.getDirection()) !=
BLOCKED)
room = rooms.at (room.canGo
(command.getDirection ()));
else
cout << "Ouch! That way is blocked." <<
endl;
else cout << "Go where?!" << endl;
break;

case quit:
done = true;
break;

default:
cout << "Pardon?!" << endl;
break;
}

--
http://www.munted.org.uk

"Honestly, what can I possibly say to get you into my bed?" - Anon.
Mar 21 '06 #9
In article
<20********************************@munted.org.uk> ,
al********@munted.org.uk says...

[ ... ]
I tried doing <Rooms *> but found it too hard to use properly. So I've
stuck with <Rooms>. I just implemented my command handler, but it could
do with improvements, so here it is (all input is assumed to be lower
case but I can always change that to handle upper case):


Writing a parser ad hoc like this is more or less asking
for trouble. As long as it stays really simple, it's not
too terrible, but for anything vaguely similar to natural
language, it'll get ugly in a big hurry.

IMO, there are really two reasonable choices. One is to
use a separate generator for the language you're trying
to parse. The other is to write a recursive descent
parser.

Using a typical lex-like lexer generator, your lexer
would look something like this:

quit { return QUIT; }
go { return GO; }
north { return NORTH; }
south { return SOUTH; }
west { return WEST; }
east { return EAST; }

and using a typical yacc-like parser generator the
parser would look something like this:

%token QUIT GO NORTH SOUTH EAST WEST

%union {
enum commands command;
int dir;
};

%type <dir> direction
%type <commands> statements

%%

game: statements;

statements: go_stmt
| quit

go_stmt: GO direction
;

direction: NORTH | SOUTH | EAST | WEST
;

So far, this is really only intended to recognize whether
something is a correctly formatted command or not -- it
doesn't carry out any actions when it does recognize a
command. What you have for actions so far are pretty
trivial though -- basically just check whether you can go
a particular direction before moving that way.

A recursive descent parser is kind of similar in some
ways, but entirely different in others. The idea is
similar to the grammar you use with a parser generator,
but you implement each element with a function (or
functor):

class action {
enum A action;
enum D dir;
};

game() {
action a;
while (a = statement())
execute(a);
}

action statement() {
action ret;
ret.verb = get_token();
if (!is_action(ret.verb))
// error: didn't get a verb

if (ret.verb == GO) {
ret.dir = get_token();
if (!is_dir(ret.dir))
// error didn't get a direction.
}
// ...

This is different from your code in a couple of ways.
First of all, it separates the parsing from the
execution. Second, it's basic structure is built around
the grammar for the language -- the call tree of the
functions directly reflects the abstract syntax tree for
the grammar.

The advantage of using funtors is that you're often
generating a large number of functions that are quite
similar, so much of the commonality can often be moved
into a base class.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Mar 22 '06 #10

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

Similar topics

6
12700
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
7035
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...
1
6740
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...
4
3272
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...
7
6033
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
4
12767
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...
6
5243
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
11
3557
by: Freddy Coal | last post by:
Hi, I'm trying to read a binary file of 2411 Bytes, I would like load all the file in a String. I make this function for make that: '-------------------------- Public Shared Function...
13
3660
by: swetha | last post by:
HI Every1, I have a problem in reading a binary file. Actually i want a C program which reads in the data from a file which is in binary format and i want to update values in it. The file...
21
3015
by: Stephen.Schoenberger | last post by:
Hello, My C is a bit rusty (.NET programmer normally but need to do this in C) and I need to read in a text file that is setup as a table. The general form of the file is 00000000 USNIST00Z...
0
7229
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
7129
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
7398
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...
1
7061
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...
0
7502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5637
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5057
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3208
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
428
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.