Results 1 to 7 of 7

Thread: need help while reading strings from file

  1. #1
    Join Date
    Oct 2007
    Location
    Zagreb, Croatia, Europe
    Beans
    513
    Distro
    Ubuntu 9.10 Karmic Koala

    need help while reading strings from file

    ok I need to make a C program that will read strings from a text file and put it in a memory as a array of strings.

    file look like
    aaa bbb ccc ddd ....
    111 222 333 444 ....

    I want to read only first line, but the problem is that first line can contain form 1 to 30 different strings.

    Can anybody make a code form me or even detailed pseudocode?
    Dear god, I would like to file a bug report.

    increase Firefox startup speed and speed of Smart Location Bar:
    http://ubuntuforums.org/showthread.php?t=1088094

  2. #2
    Join Date
    Apr 2009
    Beans
    24

    Re: need help while reading strings from file

    Quote Originally Posted by TheLions View Post
    ok I need to make a C program that will read strings from a text file and put it in a memory as a array of strings.

    file look like
    aaa bbb ccc ddd ....
    111 222 333 444 ....

    I want to read only first line, but the problem is that first line can contain form 1 to 30 different strings.

    Can anybody make a code form me or even detailed pseudocode?
    you could do something with a fgetc and check for (\r)\n to stop getting chars (finding end of line) and check for whitespace to separate words

  3. #3
    Join Date
    Sep 2008
    Beans
    68

    Re: need help while reading strings from file

    each string is 3 letters long and they are separated by a single space?
    Live Fast - Code Young. Soft engineering student's blog

    Ask me for anything but time. -Napoleon Bonaparte
    Ask me for anything but time to load Vista. -Me

  4. #4
    Join Date
    Oct 2007
    Location
    Zagreb, Croatia, Europe
    Beans
    513
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: need help while reading strings from file

    Quote Originally Posted by BugFixBug View Post
    you could do something with a fgetc and check for (\r)\n to stop getting chars (finding end of line) and check for whitespace to separate words
    Can you please elaborate how to do that?
    I spent 3 hours messing with scanf and loops and I didn't succeeded.


    Quote Originally Posted by MeLight View Post
    each string is 3 letters long and they are separated by a single space?
    No string have from 1 to 20 chars, and yes they are separated by single space.
    Dear god, I would like to file a bug report.

    increase Firefox startup speed and speed of Smart Location Bar:
    http://ubuntuforums.org/showthread.php?t=1088094

  5. #5
    Join Date
    May 2006
    Beans
    1,790

    Re: need help while reading strings from file

    Quote Originally Posted by TheLions View Post
    Can you please elaborate how to do that?
    I spent 3 hours messing with scanf and loops and I didn't succeeded.




    No string have from 1 to 20 chars, and yes they are separated by single space.
    You could look at 'strtok' too.

  6. #6
    Join Date
    Apr 2009
    Beans
    24

    Re: need help while reading strings from file

    Quote Originally Posted by TheLions View Post
    Can you please elaborate how to do that?
    I spent 3 hours messing with scanf and loops and I didn't succeeded.
    i dont have the time to make a ready to go script but ill give you a rough example

    Code:
    for ( /* get char with fgetc and save it // in for *** long as not = \n )
    {
        if (this is a alphabetical char or number)
        {
            if (!inword)
            {
              j++; // start a new word
            }
            // save the char in a array[j][i] = c
        }
        else if (inword)
        {
             inword = false;
             // append \0 to the current string// array[j][i] = '\0'
        }
        else
        {
             // just discard this char // dont do i++
        }
    }
    hope it will help

    google fgetc that might help aswell

  7. #7
    Join Date
    Oct 2007
    Location
    Zagreb, Croatia, Europe
    Beans
    513
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: need help while reading strings from file

    Quote Originally Posted by Arndt View Post
    You could look at 'strtok' too.
    Quote Originally Posted by BugFixBug View Post
    i dont have the time to make a ready to go script but ill give you a rough example

    Code:
    for ( /* get char with fgetc and save it // in for *** long as not = \n )
    {
        if (this is a alphabetical char or number)
        {
            if (!inword)
            {
              j++; // start a new word
            }
            // save the char in a array[j][i] = c
        }
        else if (inword)
        {
             inword = false;
             // append \0 to the current string// array[j][i] = '\0'
        }
        else
        {
             // just discard this char // dont do i++
        }
    }
    hope it will help

    google fgetc that might help aswell

    Thank you!
    Dear god, I would like to file a bug report.

    increase Firefox startup speed and speed of Smart Location Bar:
    http://ubuntuforums.org/showthread.php?t=1088094

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •