Parsing a string with numbers (reading a graph's adjacency list) 
Author Message
 Parsing a string with numbers (reading a graph's adjacency list)

I've got a problem that I hope you guys can give me a hand...

I've got a file that keeps record of a graph's adjacency list, it's format
is something like:

(#of Vertexes) (#of Edges)
adjacencies for 1st vertex
adjacencies for 2st vertex
adjacencies for 3st vertex
adjacencies for 4st vertex
.... (and so on)

where the labels for vertexes are integers, so I'd like to parse this whole
thing and put it in a structure that would look like:

struct Graph_List {
    int V, E;
    int **adj;

Quote:
}

My problem is to find an efficient way to do that, something better than
using fgets() to get the lines from the file and then splitting it in the
spaces and using atoi() to convert stuff to integers.

Any suggestions???

Thank you all,
Fernando Jorge
Federal University of Rio de Janeiro
Computer Science Department
Rio de Janeiro, Brazil



Fri, 29 Mar 2002 03:00:00 GMT  
 Parsing a string with numbers (reading a graph's adjacency list)


Quote:
>I've got a problem that I hope you guys can give me a hand...

>My problem is to find an efficient way to do that, something better than
>using fgets() to get the lines from the file and then splitting it in the
>spaces and using atoi() to convert stuff to integers.

>Any suggestions???

Yes, do the split with sscanf().

--
HS



Fri, 29 Mar 2002 03:00:00 GMT  
 Parsing a string with numbers (reading a graph's adjacency list)

Quote:

> I've got a problem that I hope you guys can give me a hand...

> I've got a file that keeps record of a graph's adjacency list, it's format
> is something like:

> (#of Vertexes) (#of Edges)
> adjacencies for 1st vertex
> adjacencies for 2st vertex
> adjacencies for 3st vertex
> adjacencies for 4st vertex
> .... (and so on)

> where the labels for vertexes are integers, so I'd like to parse this whole
> thing and put it in a structure that would look like:

> struct Graph_List {
>     int V, E;
>     int **adj;
> }

I'd change this to read like this:

class Grapn_List {
    int V;
    int E;
    list<int> adj;

Quote:
};

and create the iostream (or strstream) operators for this class.  Every
time I've done this in the past, I've been happy with the result.  Every
time I haven't, I've spent hours drowning in sscanf's before I gave up
on my "faster" solution and did the right thing.

Just MO though (not MHO because I'm not usually H)

Charles



Fri, 29 Mar 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Parsing array of scientific format numbers from a string to doubles

2. Adjacency linked list example..?

3. Adjacency Lists in C

4. Adjacency Lists in C

5. how to read a string containg XML and parse it

6. DateTime.Parse error when parsing from a string

7. How do I parse a string composed of a number with '%' sign

8. Newbie: Reading numbers out of a string.

9. reading numbers from a string buffer

10. How to Read tab delimited numbers and strings from file

11. Need Help with ADJACENCY LISTS FOR GRAPHS...Please help!! Urgent!!

12. How to read file while it's opened as a file number in VB

 

 
Powered by phpBB® Forum Software