[comp.lang.c] reading from a file.sorting

amigo@milton.u.washington.edu (The Friend) (04/03/91)

     I'm looking for a simple program that'll read ten words from a
string that has only got spaces between the words. I
had one example that had pointers - data read in via fscanf,
then the pointer was copied to another pointer array (each array
held one word). After the 10 words were read, the array was simply 
incremented backwards in a for loop to print out correctly.
     The problem? it didn't work. Guess I need more expierence. I've
got little resources on file opening unfortuntely. To start the program
I have:


     FILE *fp;
     static char *data;
     static char *data_array[11];
     int i;
     main(){
             fp=fopen("test","r");          
             for(i=0;i<10;i++){
                    fscanf(fp,"%s", data);
                   
          
          from there I get lost.. ya I have the data read - hopefully
word by word (?).. I can use strcpy() to copy between pointers (?),
the printout of the array after that is easy..

     
-- 

     --------------------------------------------------------------------- 
                         amigo@milton.u.washington.edu
     ---------------------------------------------------------------------

amigo@milton.u.washington.edu (The Friend) (04/03/91)

amigo@milton.u.washington.edu (The Friend) writes:


>     I'm looking for a simple program that'll read ten words from a
>string that has only got spaces between the words. I
>had one example that had pointers - data read in via fscanf,
>then the pointer was copied to another pointer array (each array
>held one word). After the 10 words were read, the array was simply 
>incremented backwards in a for loop to print out correctly.

>I have [REVISED]:


>     FILE *fp;
>     static char *data_array[11];
>     int i;
>     main(){
>             fp=fopen("test","r");          
>             for(i=0;i<10;i++){
>                    fscanf(fp,"%s", data_array[i]);
>                    printf("%d  %s",i,data_array[i]);   /* test to see 
                                             anything's put in data_array[] */
>                              }

     I'm missing something. I thought fscanf treats spaces as a _DELIM_ and
therefore each word should be put into data_array[]. Basically all that
I'm missing to complete this is what to do about fscanf not reading any
words in.

                                             Thanks..
                                                  Scott
-- 

     --------------------------------------------------------------------- 
                         amigo@milton.u.washington.edu
     ---------------------------------------------------------------------

d0np@jupiter.sun.csd.unb.ca (GEMMELL) (04/03/91)

>>     I'm looking for a simple program that'll read ten words from a
>>string that has only got spaces between the words. I
>>had one example that had pointers - data read in via fscanf,
>>then the pointer was copied to another pointer array (each array
>>held one word). After the 10 words were read, the array was simply 
>>incremented backwards in a for loop to print out correctly.

>>I have [REVISED]:


>>     FILE *fp;
>>     static char *data_array[11];
>>     int i;
>>     main(){
>>             fp=fopen("test","r");          
>>             for(i=0;i<10;i++){
>>                    fscanf(fp,"%s", data_array[i]);
>>                    printf("%d  %s",i,data_array[i]);   /* test to see 
                                             anything's put in data_array[] */
>>                              }


  One small problem with this code is that no space is allocated for the
strings.  fscanf expects a pointer to an array of char that is large enough
to hold the string and a \0 delimiter.  So either you would add a malloc 
somewheres or maybe set up the data_array to be a 2D array
  [11][MAXSTRINGSIZE] where is MAXSTRINGSIZE is your expected max string size.


             
             ------------------------------------------
             | \ \ \ \ \   /\                         |
             | / / / / /   ||                         |
             | \ \ \ \ \   || d0np@jupiter.csd.unb.ca |
             | / / / / /   ||    Michael G. Gemmell   |
             | \ \ \ \ \   ||                         |
             | / / / / / ------                       |
             | \ \ \ \ \   ()                         |
             | / / / / /   ()                         |
             ------------------------------------------

barnettr@snaggle.rtp.dg.com (Richard Barnette) (04/05/91)

In article <1991Apr3.100911.29842@milton.u.washington.edu> amigo@milton.u.washington.edu (The Friend) writes:
>>amigo@milton.u.washington.edu (The Friend) writes:
>>
>>
>       I'm looking for a simple program that'll read ten words from a
>  string that has only got spaces between the words. I
>  had one example that had pointers - data read in via fscanf,
>  then the pointer was copied to another pointer array (each array
>  held one word). After the 10 words were read, the array was simply 
>  incremented backwards in a for loop to print out correctly.
>>
>  I have [REVISED]:
>>
>>
>       FILE *fp;
>       static char *data_array[11];
>       int i;
>       main(){
>               fp=fopen("test","r");          
>               for(i=0;i<10;i++){
>                      fscanf(fp,"%s", data_array[i]);
>                      printf("%d  %s",i,data_array[i]);   /* test to see 
>>                                             anything's put in data_array[] */
>                                }
>>
    The pointers in data_array are all NULL; they don't point to anything.
In order save the data read by fscanf() you'll need to allocate space.
    Try this


       #include <string.h>

       FILE *fp;
       static char *data_array[11];
       int i;
       main(){
	       char data[32]; /* 31 letters in word + null byte */
               fp=fopen("test","r");
               for(i=0;i<10;i++){
                      fscanf(fp,"%s", data);
		      data_array[i] = strdup(data);
		      }

    strdup() is a standard library function declared in string.h

    Note also, data_array will hold 11 items, but the for loop will only
initialize 10 of them; the 11th is unused.

Richard Barnette      | Data General Corporation | obligatory (in)famous quote:
(919) 248-6225        | RTP, NC  27709	         |  You wascal wabbit!
Commercial Languages  | 62 T.W. Alexander Drive	 |  Wandering wizards won't
barnettr@dg-rtp.dg.com				 |  win! - /usr/lib/sendmail