I want to read the contents of a .txt file and the contents, for example, are:
1;london;7000;2000;3000;5000;
etc
i want to read them to a variable linked by adjacency lists
I need to know how to do this for a paper work and any help is welcome.
thx in advance...

(sorry for the not so god English, hop u understand nondless...)

Dani AI

Generated

The thread is on the right track: parse one record per line, split fields on semicolons, convert tokens to the right types, then insert them into a dynamically allocated adjacency-list structure. and were pointing in the usual direction; correctly reminds that opening the file in text mode normalizes newlines for you.

A compact, robust plan:

  • Read each full line (use POSIX getline where available; fall back to fgets with a resizable buffer if not).
  • Tokenize the line into semicolon-separated fields using a reentrant tokenizer or a simple pointer-scan (avoid non-reentrant strtok in code that might be extended or threaded).
  • Convert numeric tokens with strtol (check end-pointer for errors) and copy string tokens with strdup (or allocate with malloc).
  • Maintain a map from vertex key (id or name) to a vertex index; use a simple hash table (e.g., uthash) or a vector + linear search for small inputs.
  • Append edges by allocating edge nodes and linking them into each vertex's list.

Example data structures (C):

typedef struct Edge {
    int to;
    int weight;
    struct Edge *next;
} Edge;

typedef struct Vertex {
    int id;
    char *name;
    Edge *edges;   /* head of adjacency list */
} Vertex;

Practical tips and cautions:

  • Check every allocation and file I/O call for errors. Free duplicated strings and edge lists when done.
  • Handle trailing semicolons (they can create empty tokens) and malformed lines gracefully.
  • If names or fields can contain semicolons or quotes, consider using a small CSV/TSV parser instead of naive splitting.
  • If performance or lookups matter, use a hash map for name-to-index mapping rather than repeated linear scans.

This approach keeps parsing separate from graph construction, which makes the code easier to test and to adapt to slightly different input formats.

Recommended Answers

All 7 Replies

What language are you using?

c right ?
and you want to assign them to a buffer?

Reading that file is not difficult as long as the records are separated by '\n' or '\r\n', depending on the os. The code below will read each line then split it up with the semicolons as deliminators.

FILE* fp = fopen("file.txt","r");
char buf[255];
while( fgets(buf,sizeof(buf), fp) != NULL)
{
    char *ptr = strtok(buf,";");
    while( ptr != NULL)
    {
            // do something with the text
          ptr = strtok(NULL, ";");
   }
}

>separated by '\n' or '\r\n', depending on the os
Don't you know that C automatically translate '\n' into the native newline character when files are opened in text mode?
Read http://en.wikipedia.org/wiki/Newline#Newline_in_programming_languages 2nd point.

Hence you never need to bother what is the newline character of underlying implementation, just use '\n' all the way.

>separated by '\n' or '\r\n', depending on the os
Don't you know that C automatically translate '\n' into the native newline character when files are opened in text mode?
Read http://en.wikipedia.org/wiki/Newline#Newline_in_programming_languages 2nd point.

Hence you never need to bother what is the newline character of underlying implementation, just use '\n' all the way.

Yes of course I know that -- I was talking about what's on the disk, not what's in memory.

I want to read the contents of a .txt file and the contents, for example, are:
1;london;7000;2000;3000;5000;
etc
i want to read them to a variable linked by adjacency lists
I need to know how to do this for a paper work and any help is welcome.
thx in advance...

(sorry for the not so god English, hop u understand nondless...)

ok u see

commented: useless post, just like all your others -7

I want to read the contents of a .txt file and the contents, for example, are:
1;london;7000;2000;3000;5000;
etc
i want to read them to a variable linked by adjacency lists
I need to know how to do this for a paper work and any help is welcome.
thx in advance...

(sorry for the not so god English, hop u understand nondless...)

Might this snippet be adaptable? Replace the printf statements with whatever you need to do with the parsed tokens.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.