473,715 Members | 2,159 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 getRoomDataReco rds(string roomDataFile)
{
Rooms rooms;

ifstream ifs(roomDataFil e.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 2759
Hi,
I can see the below code is working fine...

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

Best regards,
Amal P

Mar 17 '06 #2
On 17 Mar 2006 03:31:48 -0800 "Amal P" <en********@gma il.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.getlin e( pLine, 100 );
printf("%s\n",p Line);
}


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********@mun ted.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 getRoomDataReco rds(string roomDataFile)
{
Rooms rooms;

ifstream ifs(roomDataFil e.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********@mun ted.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.or g.uk>,
al********@munt ed.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<Roo m> Rooms;

Rooms getRoomDataReco rds(std::string roomDataFile) {
Rooms rooms;
ifstream ifs(roomDataFil e.c_str());
std::copy(
std::istream_it erator<Room>(if s),
std::istream_it erator<Room>(),
std::back_inser ter(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.or g.uk>,
al********@munt ed.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.hp p"
#include "room.hpp"

using namespace std;

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

while (ss >> buffer)
tokens.push_bac k(buffer);

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

commandVerb = parseVerb(token s.at(0));

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

default:
break;
}
else
switch (commandVerb)
{
case go:
commandDirectio n = unknown_directi on;
break;

default:
break;
}
}

verb Command::getVer b() const
{
return commandVerb;
}

string Command::getNou n() const
{
return commandNoun;
}

direction Command::getDir ection() const
{
return commandDirectio n;
}

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

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

return unknown_verb;
}

direction Command::parseD irection(string directionString ) const
{
if (directionStrin g == "north")
return north;

if (directionStrin g == "west")
return west;

if (directionStrin g == "east")
return east;

if (directionStrin g == "south")
return south;

return unknown_directi on;
}

In main part this is used:

getline(cin, s);

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

switch (command.getVer b())
{
case go:
if (command.getDir ection() != unknown_directi on)
if (room.canGo(com mand.getDirecti on()) !=
BLOCKED)
room = rooms.at (room.canGo
(command.getDir ection ()));
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.or g.uk>,
al********@munt ed.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.di r))
// 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
12744
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
7054
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 file... Thank you to all of you who can help me with this one...
1
6757
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 listen up; Reports, the way I look at them, all present data downwards, in this way; TITLE data
4
3298
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 errors. After reading the ole object from db, I saved it to C: as file1.bmp and displayed on the web. But it can not be displayed. After I manually sent the file to wordpad, it shows
7
6062
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 populate a series of structures of specified variable composition. I have the structures created OK, but actually reading the files is giving me an error. Can I ask a simple question to start with: I'm trying to read the file using the...
4
12805
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 to read text file but could not get the data in correct format. All columns are not coming in dataset and rows are messing up. Suggestions please ???
6
5269
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
3590
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 Read_bin(ByVal ruta As String) Dim cadena As String = "" Dim dato As Array If File.Exists(ruta) = True Then
13
3705
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 consists of structures of type---- struct record { int acountnum; char name; float value;
21
3058
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 00000000_00 0 000 000 000 0000 000 I need to read the file line by line and eventually parse out each piece of the file and store in arrays that correspond to the specific
0
8715
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9093
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9039
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7966
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6641
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5964
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4472
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4734
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.