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

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.