473,385 Members | 1,474 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,385 software developers and data experts.

Problems parsing a flat text file with pointers.. sort of named.conf layout..

Hello,

I am having some problems understanding (most likely), parsing a text
file. I would like to parse a file like:

block1 {
stuff;
...
stuffN;
};

Just like in BIND config. (Silly project but important to me)

I would not like to use string.h functions such as strstr(), strtok()
(maybe make use of strncpy, strncat if I have to though..).

You will all probably balk at my general logic when looking at the
code, so perhaps someone might suggest a better methodology for parsing
files? (apart from lex/yacc).

I have the following code, but it doesn't seem to get out of the first
block. Would anyone mind sifting through the rubbish code and helping
me out? (i'll post at bottom of this post).

Thanks a million, 2 virtual pints of guinness for you all!

Hugh.
------ begin code -------

#include <stdio.h>

#define MAX 512

int main(int argc, char **argv)
{
int i,j,ctr,in_block=0;
char line[MAX];
FILE *fp;

if((fp=fopen(argv[1],"r+")) == NULL) {
fprintf(stderr,"Unable to open %s\n",argv[1]);
exit(-1);
}

while(fgets(line,MAX,fp) != NULL) {
for(i=0;i<=MAX;i++) {
if(line[i] == '\0' || line[i] == '#') {
i++;
continue;
}

if(line[i] == '{') {
in_block=1;
printf("Entering block\n");
j=i;
ctr++;
while(line[j] != '}') {
if(line[j] == '\0' || line[j] =='#')
continue;
printf("--> within block\n");
j++;

} in_block=0;
i += (j-i);
printf("Leaving block\n\n");

}
}
}
return 0;
}
------- end code --------

Nov 14 '05 #1
4 1703
Each APRD Chain may contain a variable number of entries (APRDs). The APRD
entry shall be physically continuous, locked in memory, and Qword-aligned in
physical address space. The information in the APRD entry is derived by the
host, and describes the physical addresses corresponding to the logical
buffer address in the original I/O request. There can be several APRDs to
describe a transfer buffer because some processors fragment physical memory
by the use of paging registers.

Nov 14 '05 #2
Hi,

I'm pretty sure that's not intended for this post....

Mercier Beaucoupoo

Nov 14 '05 #3
"Hugh" <hu***@kiaro.net> wrote:
I am having some problems understanding (most likely), parsing a text
file. I would like to parse a file like:

block1 {
stuff;
...
stuffN;
};

Just like in BIND config. (Silly project but important to me)

I would not like to use string.h functions such as strstr(), strtok()
(maybe make use of strncpy, strncat if I have to though..).
Whyever not? BTW, strncpy() is rarely the right function to use.
strncat() can do the job more efficiently nearly everywhere.
#include <stdio.h>

#define MAX 512

int main(int argc, char **argv)
{
int i,j,ctr,in_block=0;
Tabstops on Usenet. Ow.
char line[MAX];
FILE *fp;

if((fp=fopen(argv[1],"r+")) == NULL) {
fprintf(stderr,"Unable to open %s\n",argv[1]);
exit(-1);
This is an odd status to return to the OS.
if(line[i] == '{') { while(line[j] != '}') {
And what happens when the '}' is not on the same line as the '{'?
fgets() only ever reads one line.
i += (j-i);


That's just i=j writ large.

Richard
Nov 14 '05 #4
Hugh wrote:

I am having some problems understanding (most likely), parsing a
text file. I would like to parse a file like:

block1 {
stuff;
...
stuffN;
};

Just like in BIND config. (Silly project but important to me)

I would not like to use string.h functions such as strstr(), strtok()
(maybe make use of strncpy, strncat if I have to though..).

You will all probably balk at my general logic when looking at the
code, so perhaps someone might suggest a better methodology for
parsing files? (apart from lex/yacc).


First, get hold of your lines. If you don't want to worry about
line length etc. just use ggets (or fggets) as follows:

#include "ggets.h"
....

char *ln;
int err;

....
while (0 == (err = fggets(&ln, fp)) {
/* process the line */
free(ln); /* assuming it hasn't been saved */
}
/* the file has been processed or an error encountered */
if (err > 0) puts("Ran out of memory, truncating");

You can get the source code module (portable) at:

<http://cbfalconer.home.att.net/download/ggets.zip>

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #5

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

Similar topics

1
by: .d.hos | last post by:
ok, fairly new to python, relatively familiar w/ ms-sql. here's my issue: my .py script parses the contents of a (tab delim.) flat file, then attempts to insert the information into the db. I've...
3
by: Aaron Walker | last post by:
At the beginning of my program, I open a config file and load the contents into a structure (please disregard the non-portable sockaddr_in struct as it is irrelevant to the problem): struct...
9
by: BMarsh | last post by:
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...
3
by: Ali Sahin | last post by:
Hi there, I'd like to transform a XML-File to PDF. The XML-File ist build like followed: <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> <?xml-stylesheet type="text/xsl"...
26
by: SL33PY | last post by:
Hi, I'm having a problem parsing strings (comming from a flat text input file) to doubles. the code: currentImportDetail.Result = CType(line.Substring(7, 8).Trim(" "), System.Double) What...
0
by: DAnne | last post by:
Hi, I'm very new to xslt and this is my first time posting to a Forum so please forgive me if I transgress any protocols. I have to do a tally report. This report is divided up into sections....
12
by: Klaus Alexander Seistrup | last post by:
Hi group, I am new to xgawk (and seemingly to xml also), and I've been struggling all afternoon to have xgawką parsing an XHTML file containing a hCard˛, without luck. I wonder if you guys...
15
by: lxyone | last post by:
Using a flat file containing table names, fields, values whats the best way of creating html pages? I want control over the html pages ie 1. layout 2. what data to show 3. what controls to...
1
by: lxyone | last post by:
Using a flat file containing table names, fields, values whats the best way of creating forms / html pages? I want control over the html pages ie 1. layout 2. what data to show 3. what...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.