954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Read contents of a .txt file

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

girl_pt
Newbie Poster
1 post since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

What language are you using?

kolosick.m188
Junior Poster in Training
72 posts since Jun 2009
Reputation Points: 16
Solved Threads: 16
 

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

MrNoob
Posting Whiz in Training
218 posts since May 2009
Reputation Points: 34
Solved Threads: 7
 

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, ";");
   }
}
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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

siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,486
Solved Threads: 140
 

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

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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

Anand Shanker
Newbie Poster
2 posts since Jun 2009
Reputation Points: -1
Solved Threads: 0
 

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.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You