Thread: Help with printing .txt from an array

  1. #1
    Registered User
    Join Date
    Apr 2014
    Location
    New York
    Posts
    52

    Help with printing .txt from an array

    Hello all, I'm having a problem with the code below. What I'm trying to do is take a .txt file, put the digits and words into separate arrays, and then print it to the screen. The printf in the 'while' loop works fine. However the printf in the 'for' loop does not. I am able to print the digits just fine, but the only words I get is 'six', which is printed 6 times. I can't figure out why. Any sage advice would be nice. (the .txt is in the comments)

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    /* 1 one
       2 two
       3 three
       4 four
       5 five
       6 six
    */
    int main()
    {
        FILE *myfile;
        char line[121];
        char *item;
        int reccount=0;
        int temp_num[10];
        char *temp_name[10];
        int x;
        myfile=fopen("C:\\Users\\eric\\Desktop\\c_code\\number_exp.txt","r");
        while(fgets(line,120,myfile))
        {
           printf("%s",line);
           item=strtok(line," ");
           temp_num[reccount]=atoi(item);
           item=strtok(NULL," ");
           temp_name[reccount]=item;
           reccount++;
        }
        fclose(myfile);
        for(x=0;x<reccount;x++)
          printf("%i %s",temp_num[x],temp_name[x]);
        return 0;
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    your temp_name is array of 10 pointers
    all pointers point to the same buffer, so when printing - they will show more or less the same thing

    Use array of strings and use strcpy to make a copy of current name into this array
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Apr 2014
    Location
    New York
    Posts
    52
    Hi vart, thanks for taking the time to help me. I think I've done as you suggested, as noted in the revised code below. However I'm getting another set of strange results. The 'while' loop is still printing fine. However the 'for' loop isn't working still. The numbers print, but the words are prefixed with the first letter of each word. I've commented it below the main program. I've tried various declarations, referencing, and dereferencing of pointers, and getting rid of them all together to no avail. I'm certain I'm doing something wrong, but what?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    /* 1 one
       2 two
       3 three
       4 four
       5 five
       6 six
    */
    int main()
    {
        FILE *myfile;
        char line[121];
        char *item;
        int reccount=0;
        int temp_num[10];
        char temp_name[10];
        int x;
        myfile=fopen("C:\\Users\\eric\\Desktop\\c_code\\number_exp.txt","r");
        while(fgets(line,120,myfile))
        {
           printf("%s",line);
           item=strtok(line," ");
           temp_num[reccount]=atoi(item);
           item=strtok(NULL," ");
           strcpy(&temp_name[reccount],item);
           reccount++;
        }
        fclose(myfile);
        for(x=0;x<reccount;x++)
          printf("%i %s",temp_num[x],&temp_name[x]);
        return 0;
    }
    /* 1 one
       2 two
       3 three
       4 four
       5 five
       6 six
       1 ottffsix
       2 ttffsix
       3 tffsix
       4 ffsix
       5 fsix
       6 six
    */

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    now your temp_name is array of 10 chars...

    here is the sample of array of strings (dst) which is filled from array of pointers to string literals (in your case you will fill it with strings read from file)

    Code:
    #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>
    
    int main()
    {
        const char * src[] = {"one", "two", "three"};
        char dst[3][20] = {0}; /* array of 3 strings each can store the string upto 19 characters + nul-terminator */
    
        size_t i;
    
        for(i = 0; i < sizeof(src)/ sizeof(src[0]); i++)
        {
            if(strlen(src[i]) < sizeof(dst[i]))
               strcpy(dst[i],src[i]);
            else
               strcpy(dst[i], "line too long");
        }
    
        for(i = 0; i < sizeof(dst)/ sizeof(dst[0]); i++)
        {
            printf("String %u: <%s>\n", (unsigned int)i, dst[i]);
        }
    
    
        return 0;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    4
    Declare temp_name[10][10] so that you have an array of ten strings that are each up to 9 characters long. Get rid of the ampersands before temp_name when you are scanning and printing. If you are concerned with overflowing the strings, use strncpy() instead of strcpy().

  6. #6
    Registered User
    Join Date
    Apr 2014
    Location
    New York
    Posts
    52
    Thanks for the reply sd_sundevil, it worked perfectly. With what little experience I have, I know there is almost always more than one way of doing things. Varts example looks like it may be less prone to errors/ more secure. It's something I will study, as I know I haven't done so in my code yet.

    As important as the solution is, equally important is the reason why the code did what it did in the first place. I still don't understand that.
    Last edited by shaddock; 06-03-2015 at 05:39 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. printing a 2d array
    By ueg1990 in forum C Programming
    Replies: 2
    Last Post: 01-28-2012, 12:08 AM
  2. Need help printing 2D array
    By schwindy22 in forum C Programming
    Replies: 19
    Last Post: 11-26-2011, 01:57 PM
  3. Printing an array of strings until array == NULL
    By mikemhz in forum C Programming
    Replies: 10
    Last Post: 11-04-2011, 01:09 PM
  4. array printing
    By bazzano in forum C Programming
    Replies: 2
    Last Post: 03-27-2006, 06:55 AM
  5. printing to an array
    By zmerlinz in forum C Programming
    Replies: 12
    Last Post: 04-29-2003, 07:48 AM