read a line from file and split it

This is a discussion on read a line from file and split it within the C Programming forums, part of the General Programming Boards category; hi i have this problem i read lines from a txt but i want to store each word into a ...

  1. #1
    quo
    quo is offline
    Registered User
    Join Date
    May 2011
    Posts
    116

    read a line from file and split it

    hi
    i have this problem i read lines from a txt but i want to store each word into a char*.i use fgets for the line but i cant think of a way to separate the words..

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    Your first task is to think of characters that separate words.

    Your second task is to write code to find those characters in what fgets() returns.

    Your third task is to use that information to copy all the substrings into an array of substrings.

    And you're done.

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Or you can just use strtok() and you can use the rest of the time you just saved making yourself a nice cup of tea
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    quo
    quo is offline
    Registered User
    Join Date
    May 2011
    Posts
    116
    Quote Originally Posted by claudiu View Post
    Or you can just use strtok() and you can use the rest of the time you just saved making yourself a nice cup of tea
    thanks for the answer but i'm new to programming and never used strtok before so can you tell me what should i do to store each word into a pointer?
    i use the following code:

    fgets (string , 100 , file);
    y = string; //y is char*
    strtok(y," ");

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quo View Post
    thanks for the answer but i'm new to programming and never used strtok before so can you tell me what should i do to store each word into a pointer?
    Your first step would be to look up strtok() in your compiler's library documentation. You might also want to look at scanf() while you're at it.

    Neither are especially hard to use but they're not the world's simplest functions either.

    So work it out on paper, following the description, first. Then when you think you understand it, tap in some trial code and see what happens.

    Finally, if you get stuck --real stuck, not lazy stuck-- post up your code and we'll see what we can do to help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read file line by line and interpret them
    By sombrancelha in forum C Programming
    Replies: 8
    Last Post: 03-17-2011, 09:48 AM
  2. Read text file line by line and write lines to other files
    By magische_vogel in forum C Programming
    Replies: 10
    Last Post: 01-23-2011, 09:51 AM
  3. Read line by line from file with EOL different that OS EOL
    By hanniball in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2010, 09:06 AM
  4. How do I read file line by line?
    By Blurr in forum C Programming
    Replies: 1
    Last Post: 09-22-2001, 12:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21