Re: reading strings from file



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[i],array2[i])) 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[i], &array2[i]);
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
.



Relevant Pages

  • Re: K&R2 Secition 5.9 - major blunders
    ... Would changing 'point to a' to 'point into a' twenty element array be ... > arrays of pointers is to store character strings of diverse ... comparison between what was really happening (arrays of pointers to ... pointer to a string(this probably would confuse beginners)" and ...
    (comp.lang.c)
  • Re: initializing table of strings
    ... >sense to set strings in decleration. ... The fact that it is an array of pointers to const char does not matter ...
    (alt.comp.lang.learn.c-cpp)
  • Re: passing a string to a C++ function
    ... go beyong and array of integers. ... You are right I should revisit pointers since it's been about 7 years since ... > memory management. ... You can't use standatd nul-terminated strings ...
    (microsoft.public.vc.language)
  • Re: Removing duplicates from an array of pointers
    ... > I want to remove duplicate strings from an array of pointers to ... The pointers point to the contents of a file which is read ... An array of pointers points to the ... check against the reference string again, as it maybe a duplicate as well. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: A taxonomy of types
    ... however, elsewhere in my project (off in the dynamic typesystem, ...), I ... (since I am using NULL-terminated strings), and so I have used U+10FFFF ... remember, C also has things like arrays, funtion pointers, nestable ... int RIL_TypeSmallIntP; ...
    (comp.lang.misc)