473,747 Members | 3,002 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problems parsing a file

Hello, I'm creating a small utility for an online game. It involves
parsing a text file of "tradesskil l recipes" and inserting these
recipes in a gui tree widget (similar to gui file browsers if you know
what I mean).
Here's an example of a recipe as it appears in the text file:
* Cashew Pie (lvl 39, 5h 3 min, + max power)
- Candied Cashew (level 30)
-- Cashew
-- Refined Honey (level 30)
--- Raw Honey
--- Liquid
- Dough
- Refined Honey (level 30)
-- Raw Honey
-- Liquid

* denotes new recipe (a new root node in the tree)
-/--/--- denotes the depth of the child node.
One node per line.

The gui code is out of scope for the ng, but the parser is written in
standard C++, and I'm having some problems with it. The code:
#include <cassert>
#include <cctype>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <stdexcept>
#include <string>

using std::cerr;
using std::cout;
using std::endl;
using std::ifstream;
using std::isspace;
using std::runtime_er ror;
using std::string;

enum depth_t { NO_DEPTH = 0, FIRST, SECOND, THIRD };

static depth_t get_depth(const string &);

int
main()
{
ifstream food_recipes_fi le("recipes_foo d.txt");

if(!food_recipe s_file)
{
cerr << "Failed to open food recipe file." << endl;

return EXIT_FAILURE;
}

string line;
string recipe_root_nod e;
depth_t previous_depth = NO_DEPTH;

while(getline(f ood_recipes_fil e, line))
{
if(line[0] == '*')
{
assert(line[1] == ' ');

recipe_root_nod e = line.substr(2, line.length());
cout << "Inserting new recipe " << line << endl;

previous_depth = NO_DEPTH;
}
else if(isspace(line[0]))
; /* Simply skip lines beginning with a blank. */
else /* We're not dealing with a new recipe */
{
assert(line[0] == '-');

depth_t depth = get_depth(line) ;

cout << line << " at depth " << endl;
}
}

food_recipes_fi le.close();

return EXIT_SUCCESS;
}

/* get_depth() assumes the first char in line is a - */
static depth_t
get_depth(const string& line)
{
if(line[1] == ' ')
return FIRST;

/* If the second char is not a ' ', it must be a '-'. */
assert(line[1] == '-');

if(line[2] == ' ')
return SECOND;

/* If the third char is not a ' ', it must be a '-'. */
assert(line[2] == '-');

if(line[3] == ' ')
return THIRD;

throw runtime_error(" Couldn't identify depth!");
}

This compiles without warning on my compiler (with warning level set to
max, all extension turned off), but the output is really messed up:
$ ./create_tree.exe
Inserting new recipe * Cashew Pie (lvl 39, 5h 3 min, + max power)
at depth Cashew (level 30)
at depth
at depth Honey (level 30)
at depth ney
at depth
at depth
at depth Honey (level 30)
at depth ey
at depth

Any obvious errors?

Thanks for any replies!

/ E

Oct 19 '05 #1
3 1796

Eric Lilja wrote:
[snip]

Well, turns out the recipe file was in dos style line breaks and the
executable was expecting unix style line breaks. I converted the text
file to unix style line breaks and everything works. But I might be
back with further questions. =)

/ E

Oct 19 '05 #2

"Eric Lilja" <mi********@gma il.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
Hello, I'm creating a small utility for an online game. It involves
parsing a text file of "tradesskil l recipes" and inserting these
recipes in a gui tree widget (similar to gui file browsers if you know
what I mean).
Here's an example of a recipe as it appears in the text file:
* Cashew Pie (lvl 39, 5h 3 min, + max power)
- Candied Cashew (level 30)
-- Cashew
-- Refined Honey (level 30)
--- Raw Honey
--- Liquid
- Dough
- Refined Honey (level 30)
-- Raw Honey
-- Liquid

* denotes new recipe (a new root node in the tree)
-/--/--- denotes the depth of the child node.
One node per line.

The gui code is out of scope for the ng, but the parser is written in
standard C++, and I'm having some problems with it. The code:
#include <cassert>
#include <cctype>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <stdexcept>
#include <string>

using std::cerr;
using std::cout;
using std::endl;
using std::ifstream;
using std::isspace;
using std::runtime_er ror;
using std::string;

enum depth_t { NO_DEPTH = 0, FIRST, SECOND, THIRD };

static depth_t get_depth(const string &);

int
main()
{
ifstream food_recipes_fi le("recipes_foo d.txt");

if(!food_recipe s_file)
{
cerr << "Failed to open food recipe file." << endl;

return EXIT_FAILURE;
}

string line;
string recipe_root_nod e;
depth_t previous_depth = NO_DEPTH;

while(getline(f ood_recipes_fil e, line))
{
if(line[0] == '*')
{
assert(line[1] == ' ');

recipe_root_nod e = line.substr(2, line.length());
cout << "Inserting new recipe " << line << endl;

previous_depth = NO_DEPTH;
}
else if(isspace(line[0]))
; /* Simply skip lines beginning with a blank. */
else /* We're not dealing with a new recipe */
{
assert(line[0] == '-');

depth_t depth = get_depth(line) ;

cout << line << " at depth " << endl;
}
}

food_recipes_fi le.close();

return EXIT_SUCCESS;
}

/* get_depth() assumes the first char in line is a - */
static depth_t
get_depth(const string& line)
{
if(line[1] == ' ')
return FIRST;

/* If the second char is not a ' ', it must be a '-'. */
assert(line[1] == '-');

if(line[2] == ' ')
return SECOND;

/* If the third char is not a ' ', it must be a '-'. */
assert(line[2] == '-');

if(line[3] == ' ')
return THIRD;

throw runtime_error(" Couldn't identify depth!");
}

This compiles without warning on my compiler (with warning level set to
max, all extension turned off), but the output is really messed up:
$ ./create_tree.exe
Inserting new recipe * Cashew Pie (lvl 39, 5h 3 min, + max power)
at depth Cashew (level 30)
at depth
at depth Honey (level 30)
at depth ney
at depth
at depth
at depth Honey (level 30)
at depth ey
at depth

Any obvious errors?

Thanks for any replies!


After fixing the one error that caused your code to
fail to compile:

using std::getline;

and creating an input file with your sample data,
it compiled and linked OK for me (MSVC 6.0 SP6),
and gave the following output:

Inserting new recipe * Cashew Pie (lvl 39, 5h 3 min, + max power)
- Candied Cashew (level 30) at depth
-- Cashew at depth
-- Refined Honey (level 30) at depth
--- Raw Honey at depth
--- Liquid at depth
- Dough at depth
- Refined Honey (level 30) at depth
-- Raw Honey at depth
-- Liquid at depth
(I don't know if it's part of what you called 'messed up',
but the reason there's nothing after 'at depth' is because
the code makes no attempt to output that value.)

-Mike
Oct 19 '05 #3

Mike Wahler wrote:
"Eric Lilja" <mi********@gma il.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
Hello, I'm creating a small utility for an online game. It involves
parsing a text file of "tradesskil l recipes" and inserting these
recipes in a gui tree widget (similar to gui file browsers if you know
what I mean).
Here's an example of a recipe as it appears in the text file:
* Cashew Pie (lvl 39, 5h 3 min, + max power)
- Candied Cashew (level 30)
-- Cashew
-- Refined Honey (level 30)
--- Raw Honey
--- Liquid
- Dough
- Refined Honey (level 30)
-- Raw Honey
-- Liquid

* denotes new recipe (a new root node in the tree)
-/--/--- denotes the depth of the child node.
One node per line.

The gui code is out of scope for the ng, but the parser is written in
standard C++, and I'm having some problems with it. The code:
#include <cassert>
#include <cctype>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <stdexcept>
#include <string>

using std::cerr;
using std::cout;
using std::endl;
using std::ifstream;
using std::isspace;
using std::runtime_er ror;
using std::string;

enum depth_t { NO_DEPTH = 0, FIRST, SECOND, THIRD };

static depth_t get_depth(const string &);

int
main()
{
ifstream food_recipes_fi le("recipes_foo d.txt");

if(!food_recipe s_file)
{
cerr << "Failed to open food recipe file." << endl;

return EXIT_FAILURE;
}

string line;
string recipe_root_nod e;
depth_t previous_depth = NO_DEPTH;

while(getline(f ood_recipes_fil e, line))
{
if(line[0] == '*')
{
assert(line[1] == ' ');

recipe_root_nod e = line.substr(2, line.length());
cout << "Inserting new recipe " << line << endl;

previous_depth = NO_DEPTH;
}
else if(isspace(line[0]))
; /* Simply skip lines beginning with a blank. */
else /* We're not dealing with a new recipe */
{
assert(line[0] == '-');

depth_t depth = get_depth(line) ;

cout << line << " at depth " << endl;
}
}

food_recipes_fi le.close();

return EXIT_SUCCESS;
}

/* get_depth() assumes the first char in line is a - */
static depth_t
get_depth(const string& line)
{
if(line[1] == ' ')
return FIRST;

/* If the second char is not a ' ', it must be a '-'. */
assert(line[1] == '-');

if(line[2] == ' ')
return SECOND;

/* If the third char is not a ' ', it must be a '-'. */
assert(line[2] == '-');

if(line[3] == ' ')
return THIRD;

throw runtime_error(" Couldn't identify depth!");
}

This compiles without warning on my compiler (with warning level set to
max, all extension turned off), but the output is really messed up:
$ ./create_tree.exe
Inserting new recipe * Cashew Pie (lvl 39, 5h 3 min, + max power)
at depth Cashew (level 30)
at depth
at depth Honey (level 30)
at depth ney
at depth
at depth
at depth Honey (level 30)
at depth ey
at depth

Any obvious errors?

Thanks for any replies!
After fixing the one error that caused your code to
fail to compile:

using std::getline;


I was under the impression that I didn't have to qualify getline() in
this case because of Koenig lookup?

and creating an input file with your sample data,
it compiled and linked OK for me (MSVC 6.0 SP6),
and gave the following output:

Inserting new recipe * Cashew Pie (lvl 39, 5h 3 min, + max power)
- Candied Cashew (level 30) at depth
-- Cashew at depth
-- Refined Honey (level 30) at depth
--- Raw Honey at depth
--- Liquid at depth
- Dough at depth
- Refined Honey (level 30) at depth
-- Raw Honey at depth
-- Liquid at depth
(I don't know if it's part of what you called 'messed up',
but the reason there's nothing after 'at depth' is because
the code makes no attempt to output that value.)
Thanks for taking the time to test this, Mike, but did you read my
reply to myself? It was a silly line-break mess-up. =( I removed the
output of the depth variable when trying to track down the error.

-Mike


/ E

Oct 19 '05 #4

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

Similar topics

2
1619
by: Hari Prasad | last post by:
Hi, I am using XMLTool.java to convert an xml file into an xml document and I am parsing the xml file using Nodelist and Nodes. NodeList ls = getElementsByTagName("xxx"). Its working fine when I am running my application in windows. But when I am running the same application in unix, I am getting exceptions while parsing. Its giving null value and I cannot proceed further.
7
2206
by: Dennis Roberts | last post by:
I have a script to parse a dns querylog and generate some statistics. For a 750MB file a perl script using the same methods (splits) can parse the file in 3 minutes. My python script takes 25 minutes. It is enough of a difference that unless I can figure out what I did wrong or a better way of doing it I might not be able to use python (since most of what I do is parsing various logs). The main reason to try python is I had to look at...
12
9799
by: Javier | last post by:
Hello, I'm very new in this forum and as I have the following problem, the website is in http://new.vanara.com ---------------------------------------------------------------------------- -------------------------------------------- Here's how the site works: You should press a button in the rollover area in order to load a source file on an Iframe, this Iframe is actually hidden working as a Buffer. The body of this loaded Iframe is...
4
1726
by: Hugh | last post by:
Hello, I am having some problems understanding (most likely), parsing a text file. I would like to parse a file like: block1 { stuff; ... stuffN; };
21
2964
by: matvdl | last post by:
I have a system that was originally developed in asp - the pages are saved in SQL (there are over 10,000 pages) and saved to a temp directory in the server when requested by a client. I have updated this system and changed the pages that are saved to the server as aspx - everything works fine and pages can be served - but Its not impossible for a single client to request 100 plus pages in one session - as each page is requested it is...
15
3665
by: manstey | last post by:
Hi, I have a text file called a.txt: # comments I read it using this:
3
1606
by: i80and | last post by:
I'm working on a basic web spider, and I'm having problems with the urlparser. This is the effected function: ------------------------------ def FindLinks(Website): WebsiteLen = len(Website)+1 CurrentLink = '' i = 0 SpliceStart = 0 SpliceEnd = 0
3
4385
by: toton | last post by:
Hi, I have some ascii files, which are having some formatted text. I want to read some section only from the total file. For that what I am doing is indexing the sections (denoted by .START in the file) with the location. And for a particular section I parse only that section. The file is something like, .... DATAS
13
2833
by: charliefortune | last post by:
I am fetching some product feeds with PHP like this $merch = substr($key,1); $feed = file_get_contents($_POST); $fp = fopen("./feeds/feed".$merch.".txt","w+"); fwrite ($fp,$feed); fclose ($fp); and then parsing them with PHP's native parsing functions. This is succesful for most of the feeds, but a couple of them claim to be
0
8979
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9522
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9354
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9307
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,...
1
6790
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
4588
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
4860
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3296
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2203
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.