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...)

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.

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

Comments
useless post, just like all your others

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.

This article has been dead for over six months. Start a new discussion instead.