Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,271 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,457 people online right now. Registration is fast and FREE... Join Now!




parse function

 
Reply to this topicStart new topic

parse function

starter
3 Dec, 2006 - 09:45 AM
Post #1

New D.I.C Head
*

Joined: 3 Dec, 2006
Posts: 1


My Contributions
I have a problem with my homework.I couldn't parse my input file. The input file includes lines.I attach a simple input file.

There are spaces at the end of the lines. The lenght of the line is 110 characters. The problem is the spaces at the end of the lines. How can i parse the lines?

I used this parser but when the parse come to the end of the line it doesn't go next line.

void parse(char * line,char * tlist[])
{
* tlist = strtok(line, " ");

while(*tlist)
{
tlist++;
* tlist = strtok(NULL, " ");
}
}

Thanks...


Attached File(s)
Attached File  firm.txt ( 26.36k ) Number of downloads: 50
User is offlineProfile CardPM
+Quote Post

horace
RE: Parse Function
3 Dec, 2006 - 11:57 AM
Post #2

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 4 times
Dream Kudos: 50
My Contributions
QUOTE(starter @ 3 Dec, 2006 - 05:45 PM) *

I have a problem with my homework.I couldn't parse my input file. The input file includes lines.I attach a simple input file.

There are spaces at the end of the lines. The lenght of the line is 110 characters. The problem is the spaces at the end of the lines. How can i parse the lines?

I used this parser but when the parse come to the end of the line it doesn't go next line.

void parse(char * line,char * tlist[])
{
* tlist = strtok(line, " ");

while(*tlist)
{
tlist++;
* tlist = strtok(NULL, " ");
}
}

Thanks...

are you reading the file line by line, e.g.
CODE

    char line[120];
    char *tlist[100]={0};
    while (fgets(line, 120, in_file) != NULL)         /* read line */
       {
        printf("%s", line);                            /* write line */
        parse(line, tlist);                            /* get tokens */
        for (i=0;  tlist[i] != NULL; i++ )             /* print tokens */
           printf( "token is \"%s\"\n", tlist[i] );
       }

which should work - fgets() leaves the '\n' in line so it would be treated as a token is that the problem?

This post has been edited by horace: 3 Dec, 2006 - 11:58 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 07:59PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month