473,662 Members | 2,454 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

parsing strings / within a filename

Hi all,

I have the following piece of code which i've been wrestling with for
sometime, which I attribute, still, to my lack of understanding of
pointers. (this is probably rining alarm bells atm for you seasoned
pro's...).

I am trying to parse a file with the following format:

hostname {
file1;
file2;
file3;
};

Now, i could get this from the BIND source code (OT: named.conf
parsing..) but I really need to crack this myself...

I have the following code:
load_config(cha r *fn)
{
FILE *fp;
char *host;
char *buf;
int inblock=0;

buf=malloc(1024 );
host=malloc(32) ;

if((fp=fopen(fn ,"r+")) < 0)
{
fatal("fopen"); // my own wrapper..
}

memset(buf,'\0' ,1024);
while(fgets(buf ,sizeof(buf),fp )) {
buf[strlen(buf)-1]='\0';
if(*buf == '{') {
inblock=1;
printf("%s",buf );
buf++;
}
}

}

This occasionally segfaults depending on wether I use an array for buf
or a pointer..

I don't even get up to the left brace.. I basically need to gobble up
the next three files, which I think I can do with fgets, blast the
newline and strtok with the semi-colon no problem, but I can't seem to
move through the file!!

Thanks in advance for any help you can provide

Cheers

Bryan.

Nov 14 '05 #1
9 1990
BMarsh wrote:

Hi all,

I have the following piece of code which i've been wrestling with for
sometime, which I attribute, still, to my lack of understanding of
pointers.
Correct attribution. Well done.
(this is probably rining alarm bells atm for you seasoned
pro's...).
Yawn. :-)

<snip>
I have the following code:

load_config(cha r *fn)
{
FILE *fp;
char *host;
char *buf;
int inblock=0;

buf=malloc(1024 );
host=malloc(32) ;
Check that these don't fail.

if(buf == NULL) fatal("buf malloc");
if(host == NULL) fatal("host malloc");

if((fp=fopen(fn ,"r+")) < 0)
{
fatal("fopen"); // my own wrapper..
}

memset(buf,'\0' ,1024);
while(fgets(buf ,sizeof(buf),fp )) {
Hold on there, pardnuh. buf is a pointer, not an array. So sizeof(buf)
is going to give you the size of a pointer, which isn't what you want
here. You want the amount of memory you allocated, to which buf points.
That is, 1024.
buf[strlen(buf)-1]='\0';
if(*buf == '{') {
inblock=1;
printf("%s",buf );
buf++;


Ouch. Don't you move that pointer! Find another way around.
Nov 14 '05 #2
Hi Infobahn..

Danke fuer ihren hilfe. :)

I was of the impression that I could move pointers like so? Can I not
move through that string? Perhaps incrementing the dereferenced pointer
will work? Argh I need to re-pickup herbert schildt's book..

Thanks again

bry.

Nov 14 '05 #3
Ok - the 2nd coming.

I have the following, and it segfaults...arg h...

BTW - MAXBUF is defined as 1024

load_config(cha r *fn)
{
FILE *fp;
char *host;
char *buf;
int inblock=0;

if((host=malloc (MAXBUF)) == NULL)
fatal("malloc") ;

if((fbuf=malloc (MAXBUF)) == NULL)
fatal("malloc") ;

if((fp=fopen(fi lename,"r+")) < 0) {
fatal("fopen");
}

memset(buf,'\0' ,MAXBUF);
while(fgets(buf ,MAXBUF,fp)) {
fbuf[strlen(buf) -1]='\0';
while(*buf) {
if(*buf == '{') {
inblock=1;
while(*buf != ';') {
*buf++ = *host++;
}
} *buf++;
}
}
printf("host: %s\n",host);

return 0;
}

GDB says the *buf++ = *host++; line is the problem - but I am
essentially doing what strcpy does..

Thanks again... :-)

Bryan.

Nov 14 '05 #4
BMarsh wrote:
Ok - the 2nd coming.

I have the following, and it segfaults...arg h...

BTW - MAXBUF is defined as 1024

load_config(cha r *fn)
{
FILE *fp;
char *host;
char *buf;
int inblock=0;

if((host=malloc (MAXBUF)) == NULL)
fatal("malloc") ;

if((fbuf=malloc (MAXBUF)) == NULL)
fatal("malloc") ;

if((fp=fopen(fi lename,"r+")) < 0) {
fatal("fopen");
}

memset(buf,'\0' ,MAXBUF);
while(fgets(buf ,MAXBUF,fp)) {
fbuf[strlen(buf) -1]='\0';
while(*buf) {
if(*buf == '{') {
inblock=1;
while(*buf != ';') {
*buf++ = *host++;
}
} *buf++;
}
}
printf("host: %s\n",host);

return 0;
}

GDB says the *buf++ = *host++; line is the problem - but I am
essentially doing what strcpy does..

Thanks again... :-)

Bryan.

Here is what you are doing as an example
1024 bytes < ^ buf points here

Read a 512 byte line
1024 bytes < ^buf points here

Read a 1000 byte line
1024 bytes <

^ buf points here

Buf now points outside of the region allocated. The last read overwrote
whatever was after the region allocated by the original malloc. You are
telling fgets that the size of buf is MAXBUF, and so it is from the
original value of buf. But you are advancing buf through the buffer,
but still telling fgets to read up to MAXBUF bytes. The size of the
region allocated by buf has not changed. If you need it to grow, you
could keep calling "realloc" to make it bigger.

Perhaps I'm misreading the above. Are you reading IN TOTAL more than
1024 bytes in all calls to fgets? If so, that is the problem...

This line is also not very defensive:

while(*buf != ';') {

If you are missing a ; you will go until you find one, reading outside
of your allocated space and writing outside your allocated space in .
Perhaps you mean:

while (*buf && *buf != ';')

And then the real problem is probably that

*buf++ = *host++;

Means to be

*host++ = *buf++

The malloc'd memory pointed to by host is never initialized in the
function above, likely contains garbage.

-David
Nov 14 '05 #5
Thanks for all your help, I give up, i'm going to use flex. :-)

Cheers

Bryan.

Nov 14 '05 #6
BMarsh wrote:
Hi Infobahn..

Danke fuer ihren hilfe. :)
It's helpful if you actually quote sufficient of what you are replying
to that we know what you are talking about. I think CBFalconer has
instructions in his sig for how to do something sensible with Google.
I was of the impression that I could move pointers like so? Can I not
move through that string? Perhaps incrementing the dereferenced pointer
will work?
Yes, you can. However, when you have a malloc'd block you need to keep a
record of where it starts. Your code in question was:

| buf=malloc(1024 );
| host=malloc(32) ;
|
| if((fp=fopen(fn ,"r+")) < 0)
| {
| fatal("fopen"); // my own wrapper..
| }
|
| memset(buf,'\0' ,1024);
| while(fgets(buf ,sizeof(buf),fp )) {

What will the value of buf be when you get here second time since you
incrememt it bellow?

| buf[strlen(buf)-1]='\0';
| if(*buf == '{') {
| inblock=1;
| printf("%s",buf );
| buf++;
| }
| }

Also, when it comes to freeing the memory later you need the pointer
returned by malloc, realloc or calloc since that is what you pass to free.
Argh I need to re-pickup herbert schildt's book..


No, you need incinerate that book before it does any further harm.
Search this group for references to it and you will find links to
reviews showing just how many errors it contains. I'm not talking about
typos you might spot, but errors in Schildt's understanding of C that
will mislead you.

I would recomend the FAQ for comp.lang.c (google will find it for you)
and K&R2.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #7
BMarsh wrote:

I was of the impression that I could move pointers like so? Can I
not move through that string? Perhaps incrementing the dereferenced
pointer will work? Argh I need to re-pickup herbert schildt's book..


Lacking any quotes, I have no idea what "like so" means. However
it is highly likely that you have misconceptions due to reading
anything from Schildt, which is generally considered to be
extremely smelly BullSchildt here, and best dried out in the sun
and then burnt.

--
"I conclude that there are two ways of constructing a software
design: One way is to make it so simple that there are obviously
no deficiencies and the other way is to make it so complicated
that there are no obvious deficiencies." -- C. A. R. Hoare
Nov 14 '05 #8
On 21 Mar 2005 06:37:42 -0800, "BMarsh" <b.*****@gmx.ne t> wrote in
comp.lang.c:

Nobody seems to have pointed out...

Hi all,

I have the following piece of code which i've been wrestling with for
sometime, which I attribute, still, to my lack of understanding of
pointers. (this is probably rining alarm bells atm for you seasoned
pro's...).

I am trying to parse a file with the following format:

hostname {
file1;
file2;
file3;
};

Now, i could get this from the BIND source code (OT: named.conf
parsing..) but I really need to crack this myself...

I have the following code:
load_config(cha r *fn)
{
FILE *fp;
char *host;
char *buf;
int inblock=0;

buf=malloc(1024 );
host=malloc(32) ;

if((fp=fopen(fn ,"r+")) < 0)


What the heck is this!?!

The object fp is a pointer to FILE. It is not an integer or any type
of arithmetic variable. There is something very, very wrong with your
compiler if it even compiles this. It is a constraint violation.

fopen() returns NULL or 0 on failure, and something not zero on
success. And the only valid comparison of a pointer with a scalar
value is for equality or inequality with NULL or 0.

The test should be:

if ((fp = fopen(fn, "r+") == 0)

What compiler are you using that accepted the code as you wrote it
without a diagnostic? It is badly non-conforming.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #9
Hello all,

Thanks for your replies - All of the string pointer information I
learned years ago was from Schildt, yes... :-(

I think I might have run out of toilet paper - how convenient.....
FAO: Jack Klein - The compiler is gcc 3.4.x on Slackware Linux. I have
been thinking about developing my code on AIX anyway - that compiler
seems to be super strict from what I have seen when compiling certain
packages, etc...

Thanks again - i'll go and bash my head against a brick wall for a
while then fire up amazon - And as you all lambast Schildt's book, do
you have any recommendations apart from K&R2? (I've got that).

Thanks once again

Bry

Nov 14 '05 #10

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

Similar topics

8
9436
by: Gerrit Holl | last post by:
Posted with permission from the author. I have some comments on this PEP, see the (coming) followup to this message. PEP: 321 Title: Date/Time Parsing and Formatting Version: $Revision: 1.3 $ Last-Modified: $Date: 2003/10/28 19:48:44 $ Author: A.M. Kuchling <amk@amk.ca> Status: Draft Type: Standards Track
4
3821
by: Fuzzyman | last post by:
There have been a couple of config file 'systems' announced recently, that focus on building more powerful and complex configuration files. ConfigObj is a module to enable you to much more *simply* access config files. This is version 3, which is a big overhaul. It extends ConfigObj to reading config files with sections and various other simplifications. I find ConfigObj extremely easy to use and use it for reading config files and data...
9
3202
by: {AGUT2}=IWIK= | last post by:
Hello all, It's my fisrt post here and I am feeling a little stupid here, so go easy.. :) (Oh, and I've spent _hours_ searching...) I am desperately trying to read in an ASCII "stereolithography" file (*.STL) into my program. This has the following syntax... Begin STL Snippet **********
2
3946
by: Cigdem | last post by:
Hello, I am trying to parse the XML files that the user selects(XML files are on anoher OS400 system called "wkdis3"). But i am permenantly getting that error: Directory0: \\wkdis3\ROOT\home Canonicalpath-Directory4: \\wkdis3\ROOT\home\bwe\ You selected the file named AAA.XML getXmlAlgorithmDocument(): IOException Not logged in
4
2648
by: ralphNOSPAM | last post by:
Is there a function or otherwise some way to pull out the target text within an XML tag? For example, in the XML tag below, I want to pull out 'CALIFORNIA'. <txtNameUSState>CALIFORNIA</txtNameUSState>
29
4247
by: zoltan | last post by:
Hi, The scenario is like this : struct ns_rr { const u_char* rdata; }; The rdata field contains some fields such as :
17
2781
by: Mark | last post by:
I must create a routine that finds tokens in small, arbitrary VB code snippets. For example, it might have to find all occurrences of {Formula} I was thinking that using regular expressions might be a neat way to solve this, but I am new to them. Can anyone give me a hint here? The catch is, it must only find tokens that are not quoted and not commented; examples follow
3
2541
by: Chris | last post by:
Hi everyone, I'm trying to parse through the contents of some text files with regular expressions, but am new to regular expressions and how to use them in VB.net. I'm pretty sure that the regular expressions are correct as I got them from regexlib.com and tested them in the Regulator and Expresso. The problem is I tested this function with a file that contains a string
12
1317
by: Martin Drautzburg | last post by:
I would like to validate sql strings, which are spread all over the code, i.e. I run ("prepare") them against a database to see if it happy with the statements. Spelling errors in sql have been a major pain for me. The statements will not be assembled from smaller pieces, but they will not neccessarily be defined at module level. I could live with class level, but method level would be best. And I definitely don't want to parse the...
0
8432
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
8343
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,...
0
8856
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
8633
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
7365
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
6185
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
4179
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
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1747
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.