reading strings from file

Discussion in 'C Programming' started by Giff, Jan 2, 2006.

  1. Giff

    Giff Guest

    Hi

    I have this problem that I can't solve, I know it shouldn't be hard but
    I'm not a good coder and I'm going crazy tonight...

    I have a txt file that goes like this:

    string1 string2
    string3 string4

    I'd like to read it and store string1 and string in array1 and string2
    and 4 in array2

    can u please advice me the right way to do this? I'm using C/unix

    thanks a lot and I wish u all a great 2006


    --
    there are no numbers in my email address
     
    Giff, Jan 2, 2006
    #1
    1. Advertising

  2. Giff a écrit :
    > I have this problem that I can't solve, I know it shouldn't be hard
    > but I'm not a good coder and I'm going crazy tonight...
    >
    > I have a txt file that goes like this:
    >
    > string1 string2 string3 string4


    Do you meant:

    string1<blank>string2<EOL>
    string3<blank>string4<EOL>

    ?

    > I'd like to read it and store string1 and string in array1 and
    > string2 and 4 in array2


    You can use each line with fgets() and then extract the strings with
    sscanf() and "%s %s". Make sure that sscanf() returns 2.

    You can store the result in an array of strings with a fixed width (2D
    array of char) or allocate the required space (malloc()) to store the
    strings, and to store the addresses of the allocated blocks on an array
    of pointers to char. This array could also be dynamically allocated. All
    depends on the required flexibility.
    --
    A+

    Emmanuel Delahaye
     
    Emmanuel Delahaye, Jan 2, 2006
    #2
    1. Advertising

  3. Giff

    Giff Guest

    Emmanuel Delahaye ha scritto:


    > Do you meant:


    > string1<blank>string2<EOL>
    > string3<blank>string4<EOL>


    > ?


    exactly, thanks for replying


    > You can use each line with fgets() and then extract the strings with
    > sscanf() and "%s %s". Make sure that sscanf() returns 2.


    I was trying with fscanf, now I tried the way u suggested with no results:

    char *array1[2], *array2[2],buff[512];
    FILE *f;

    i=0;
    do {

    if ( fgets(buff,sizeof(buff),f)==NULL ) perror("fgets");
    if ( (i = sscanf(buff,"%s %s",array1,array2)) printf("%d\n",i);
    i++;
    }while(i<2);

    it opens the file correctly but then the sscanf returns 0, it gets stuck
    during the second iteraction of the do-while

    I'm sure I'm doing some really stupid mistakes, I'm not experienced,I've
    been coding the whole day and this should be working tomorrow

    thanks a lot



    --

    questo articolo e` stato inviato via web dal servizio gratuito
    http://www.newsland.it/news segnala gli abusi ad
     
    Giff, Jan 2, 2006
    #3
  4. Giff

    Daniel Rudy Guest

    At about the time of 1/2/2006 2:14 PM, Giff stated the following:

    > Hi
    >
    > I have this problem that I can't solve, I know it shouldn't be hard but
    > I'm not a good coder and I'm going crazy tonight...
    >
    > I have a txt file that goes like this:
    >
    > string1 string2
    > string3 string4
    >
    > I'd like to read it and store string1 and string in array1 and string2
    > and 4 in array2
    >
    > can u please advice me the right way to do this? I'm using C/unix
    >
    > thanks a lot and I wish u all a great 2006
    >
    >


    The only what that I can think of is to read in the lines one at a time
    using fgets and then using strsep to split the strings apart. Just out
    of curiosity, did you write this text file or was it provided from an
    outside source?

    --
    Daniel Rudy

    Email address has been base64 encoded to reduce spam
    Decode email address using b64decode or uudecode -m

    Why geeks like computers: look chat date touch grep make unzip
    strip view finger mount fcsk more fcsk yes spray umount sleep
     
    Daniel Rudy, Jan 2, 2006
    #4
  5. Giff <> schrieb:

    > char *array1[2], *array2[2],buff[512];
    > FILE *f;
    >
    > i=0;
    > do {
    >
    > if ( fgets(buff,sizeof(buff),f)==NULL ) perror("fgets");
    > if ( (i = sscanf(buff,"%s %s",array1,array2)) printf("%d\n",i);


    Where did you allocate memory for array1 and array2?

    > i++;
    > }while(i<2);
    >
    > I'm sure I'm doing some really stupid mistakes, I'm not experienced,I've
    > been coding the whole day and this should be working tomorrow


    If you are using pointers, you should have a look at malloc or calloc.

    Markus
     
    Markus Becker, Jan 2, 2006
    #5
  6. Giff

    Giff Guest


    > The only what that I can think of is to read in the lines one at a time
    > using fgets and then using strsep to split the strings apart. Just out
    > of curiosity, did you write this text file or was it provided from an
    > outside source?


    hi all, I made it work now! I'm not very good with array and pointers, I
    mixed them up a bit

    thanks a lot to everybody and again happy new year!



    --

    questo articolo e` stato inviato via web dal servizio gratuito
    http://www.newsland.it/news segnala gli abusi ad
     
    Giff, Jan 2, 2006
    #6
  7. Giff

    Daniel Rudy Guest

    At about the time of 1/2/2006 3:17 PM, Giff stated the following:

    > Emmanuel Delahaye ha scritto:
    >
    >
    >
    >>Do you meant:

    >
    >
    >>string1<blank>string2<EOL>
    >>string3<blank>string4<EOL>

    >
    >
    >>?

    >
    >
    > exactly, thanks for replying
    >
    >
    >
    >>You can use each line with fgets() and then extract the strings with
    >>sscanf() and "%s %s". Make sure that sscanf() returns 2.

    >
    >
    > I was trying with fscanf, now I tried the way u suggested with no results:
    >
    > char *array1[2], *array2[2],buff[512];
    > FILE *f;
    >
    > i=0;
    > do {
    >
    > if ( fgets(buff,sizeof(buff),f)==NULL ) perror("fgets");
    > if ( (i = sscanf(buff,"%s %s",array1,array2)) printf("%d\n",i);
    > i++;
    > }while(i<2);
    >
    > it opens the file correctly but then the sscanf returns 0, it gets stuck
    > during the second iteraction of the do-while
    >
    > I'm sure I'm doing some really stupid mistakes, I'm not experienced,I've
    > been coding the whole day and this should be working tomorrow
    >
    > thanks a lot
    >
    >
    >


    I would do something like this...

    #define MAX_ARRAY 2 /* number of strings to store */
    #define MAX_CHAR 64 /* storage size of each string */
    #define MAX_BUFF 512 /* input buffer size */

    char array1[MAX_ARRAY][MAX_CHAR];
    char array2[MAX_ARRAY][MAX_CHAR];
    char buff[MAX_BUFF];
    FILE *f;

    for (i = 0; i < MAX_ARRAY; ++i)
    {
    ptr = fgets(buff, sizeof(buff), f);
    if (ptr == NULL) perror("fgets");
    j = sscanf(buff, "%s %s", &array1, &array2);
    if (j != 2) perror("sscanf");
    }


    This is a guideline and it may or may not work correctly. The problem
    that I see with your code is that you are defining the arrays as a
    pointer. No storage has been allocated to hold the actual data. Your
    usage of sscanf with the pointers is correct as far as I can tell, but
    those pointers need to point to something. As it is, sscanf writes the
    strings to random locations in memory. I'm amazed that you didn't get a
    general protection fault or a segmentation fault when you ran this.

    Also, arrays of strings are a little strange because a single string is
    in itself an array of char, so to allocate an array of strings, you need
    a 2 dimensional array of char.


    --
    Daniel Rudy

    Email address has been base64 encoded to reduce spam
    Decode email address using b64decode or uudecode -m

    Why geeks like computers: look chat date touch grep make unzip
    strip view finger mount fcsk more fcsk yes spray umount sleep
     
    Daniel Rudy, Jan 2, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ram Laxman
    Replies:
    5
    Views:
    2,632
    David Harmon
    Feb 10, 2004
  2. Jamie

    Reading Unicode Strings from File

    Jamie, Nov 24, 2003, in forum: C Programming
    Replies:
    5
    Views:
    768
    Simon Biber
    Nov 27, 2003
  3. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    814
    Malcolm
    Jun 24, 2006
  4. arnuld
    Replies:
    16
    Views:
    764
    Jim Langston
    Sep 14, 2007
  5. Sabiyur
    Replies:
    6
    Views:
    498
    Sabiyur
    Dec 22, 2007
Loading...

Share This Page