storing strings in arrays

This is a discussion on storing strings in arrays within the C Programming forums, part of the General Programming Boards category; Hi , I have one txt file: list1_UT.txt It contains following image names & UT: reduced_obj_r_3.fits 21:08:37 reduced_obj_r_5.fits 21:16:20 reduced_obj_r_9.fits ...

  1. #1
    smp
    smp is offline
    Registered User
    Join Date
    Dec 2008
    Posts
    3

    storing strings in arrays

    Hi ,


    I have one txt file: list1_UT.txt
    It contains following image names & UT:

    reduced_obj_r_3.fits 21:08:37
    reduced_obj_r_5.fits 21:16:20
    reduced_obj_r_9.fits 21:25:40
    reduced_obj_r_10.fits 21:34:02
    reduced_obj_r_11.fits 21:35:01
    reduced_obj_r_12.fits 21:36:00
    reduced_obj_r_13.fits 21:37:00
    reduced_obj_r_14.fits 21:38:00
    reduced_obj_r_15.fits 21:46:25
    reduced_obj_r_16.fits 21:47:24
    reduced_obj_r_17.fits 21:48:23
    reduced_obj_r_18.fits 21:49:24
    reduced_obj_r_19.fits 21:50:22
    reduced_obj_r_21.fits 22:00:18
    reduced_obj_r_22.fits 22:01:17
    reduced_obj_r_23.fits 22:02:14
    reduced_obj_r_24.fits 22:03:19
    reduced_obj_r_25.fits 22:13:35
    reduced_obj_r_30.fits 22:26:58
    reduced_obj_r_33.fits 22:30:14
    reduced_obj_r_34.fits 22:31:13
    reduced_obj_r_41.fits 22:57:26

    What I want to do is this:

    1. Read this file: use two variables to read two columns(str1,str2), line by line.
    2. Assign these variables to two different arrays (img_L1[ ] and time_L1[ ] )
    3. Access these two different arrays whenever I want and from any starting point i.e. e.g. from element 13 onwards.


    This is segment of my code:

    ----------------------------------------------
    Code:
    char *img_L1[22];
      char *time_L1[22];
    
      char str1[25], str2[10];
      char s1[25], s2[10];
    
                 :
                 :
    
     fp_image_and_ut = fopen("list1_UT.txt","r");
    
       while(!feof(fp_image_and_ut))
         {
    
            fscanf(fp_image_and_ut,"%s %s",str1,str2);
    
            strcpy (s1,str1);
            strcpy (s2, str2);
    
            img_L1[i] = s1;
            time_L1 [i] = s2;
    
            printf("%s\n", img_L1[i]);         //line A
            i++;
    
         }
    
        printf("\n");
    
        for(i=0;i<22;i++)
         {
             puts(img_L1[i]);                  // line B
         }
    
                :
                :
      flcose(fp_image_and_ut);
    --------------------------------------------------------

    For line A in code, o/p is as expected i.e. as follows

    reduced_obj_r_3.fits
    reduced_obj_r_5.fits
    reduced_obj_r_9.fits
    reduced_obj_r_10.fits
    reduced_obj_r_11.fits
    reduced_obj_r_12.fits
    reduced_obj_r_13.fits
    reduced_obj_r_14.fits
    reduced_obj_r_15.fits
    reduced_obj_r_16.fits
    reduced_obj_r_17.fits
    reduced_obj_r_18.fits
    reduced_obj_r_19.fits
    reduced_obj_r_21.fits
    reduced_obj_r_22.fits
    reduced_obj_r_23.fits
    reduced_obj_r_24.fits
    reduced_obj_r_25.fits
    reduced_obj_r_30.fits
    reduced_obj_r_33.fits
    reduced_obj_r_34.fits
    reduced_obj_r_41.fits
    reduced_obj_r_41.fits (this repeation of previous name can be removed afterwards)


    For line B in code, o/p is as follows:

    reduced_obj_r_41.fits
    reduced_obj_r_41.fits
    reduced_obj_r_41.fits
    reduced_obj_r_41.fits
    reduced_obj_r_41.fits
    reduced_obj_r_41.fits
    reduced_obj_r_41.fits
    reduced_obj_r_41.fits
    reduced_obj_r_41.fits
    reduced_obj_r_41.fits
    reduced_obj_r_41.fits
    reduced_obj_r_41.fits
    reduced_obj_r_41.fits
    reduced_obj_r_41.fits
    reduced_obj_r_41.fits
    reduced_obj_r_41.fits
    reduced_obj_r_41.fits
    reduced_obj_r_41.fits
    reduced_obj_r_41.fits
    reduced_obj_r_41.fits
    reduced_obj_r_41.fits
    reduced_obj_r_41.fits


    I want o/p for line B same as line A.

    Please help.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    1. do not use feof to control loop - read FAQ. Use the return value of scanf instead
    2. check the return values before using it (for example fopen could fail)
    3. provide different starage conainer for each string, for now you writing all strings into the s1 buffer overwriing with the new string the old one. You have 2 choices - make an array of strings
    char V1[MAX_STRING_NUM][MAX_LENGTH]
    or leave an array of pointers as you have now - but allocate the storage space fo each sring wih malloc before copying it.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    smp
    smp is offline
    Registered User
    Join Date
    Dec 2008
    Posts
    3

    hi

    hi

    Please run my code. Then it will be clear what I want to say exactly.

    There is no problem in reading the file.

    Problem is in storing the two strings i.e. s1 and s2 into two different arrays and then retrieving these two arrays whenever I want.

    I am using a pointer to list arrays i.e. strings, namely
    char *img_L1[ ] to store first column of my txt file.

    I am using a pointer to a list of arrays i.e. strings, namely
    char *time_L1[ ] to store second column of txt file.


    regards

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Here are your errors:
    1) What you want is this:
    Code:
    strcpy(img_L1[i], s1);
    //NOT
    img_L1[i] = s1
    Do you see the difference? strcpy copies the values of the array s1 to a NEW array that img_L1[i] points. The second will make img_L1[i] POINT to s1. So s1 and img_L1[i] will point to the same memory.
    So you change that memory. img_L1[i] still points to the same changed memory.
    Do you get the difference?? You are not using pointer logic correctly.

    2) If you fix the above you will get an error if you don't allocate memory. That is why you have to use char img_L1[22][25], where 22 the number of lines and 25 the maxiumum number of characters a string has (that you use for s1).

    You can also do for (int i = 0; i < 22; ++i) imgL1[i] = malloc(25); to dynamically allocate memory, but that is worse in your case.

    3) read the faq as vart said and you will see why you get reduced_obj_r_41.fits twice.

    EDIT: I forgot the most important part! You get the same thing because all pointers of img_L1 point to the same location. s1, which after the loop has the value of the last element
    Last edited by C_ntua; 12-16-2008 at 04:19 AM.

  5. #5
    smp
    smp is offline
    Registered User
    Join Date
    Dec 2008
    Posts
    3

    hi

    Hi,

    It worked as per your suggestions.
    Thank you so much.



    Code:
    so,   
          img_L1[i][j];
    and
          *img_L1[i];
    are same ?

    Regards.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by smp View Post
    Code:
    so,   
          img_L1[i][j];
    and
          *img_L1[i];
    are same ?

    Regards.
    only when i = j = 0

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    > Please run my code. Then it will be clear what I want to say exactly.
    > There is no problem in reading the file.
    1. You've edited the code so badly that it won't compile, nevermind run.
    2. We don't need to compile or run things just to spot bugs. Just because you haven't noticed there's a problem doesn't mean there isn't a problem.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing strings in a linked list
    By dws90 in forum C Programming
    Replies: 1
    Last Post: 02-21-2009, 06:06 PM
  2. storing strings by enum/s
    By l2u in forum C++ Programming
    Replies: 2
    Last Post: 10-07-2008, 08:03 AM
  3. Logic help with strings and arrays.
    By logithx in forum C Programming
    Replies: 2
    Last Post: 09-12-2008, 09:31 AM
  4. Arrays of Strings. With functions/memcpy?
    By Kleid-0 in forum C Programming
    Replies: 7
    Last Post: 01-11-2005, 06:56 PM
  5. Storing strings in 2d char arrays problem
    By rainmanddw in forum C++ Programming
    Replies: 5
    Last Post: 10-22-2003, 05:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21