Character Arrays



Help for C/C++ for MVS, OS/390 C/C++, z/OS C/C++ and C/C++ Productivity Tools for OS/390

Character Arrays

Postby tonysam » Fri May 03, 2013 5:53 pm

Want to read a sequential file into an array so it can be searched. I want to move individual array rows into a structure so I can look at individual field values. The move to the structure appears to work but I can't seem to reference the individual fields. If I print the structure I see the entire row, if I print a field I get garbage. I've attached the source. Any help would be appreciated.

cpgm.txt
You do not have the required permissions to view the files attached to this post.
tonysam
 
Posts: 3
Joined: Fri Feb 22, 2013 8:44 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Character Arrays

Postby NicC » Fri May 03, 2013 9:27 pm

Please do not post attachements. Some people will not download them, some people are not allowed to download them and some people do not bother with posts that hide things away in attachments.
The problem I have is that people can explain things quickly but I can only comprehend slowly.
Regards
Nic
NicC
Global moderator
 
Posts: 3025
Joined: Sun Jul 04, 2010 12:13 am
Location: Pushing up the daisies (almost)
Has thanked: 4 times
Been thanked: 136 times

Re: Character Arrays

Postby enrico-sorichetti » Sat May 04, 2013 12:03 am

why not start with a simpler program ...

and add complexity a bit at the time

for example start from something like

 ****** ***************************** Top of Data ******************************
 000001 #include <stdio.h>
 000002 #include <string.h>
 000003 #include <stdlib.h>
 000004 int l, k;
 000005 FILE *f;
 000006 char r[81];
 000007
 000008 int main(int argc, char **argv)
 000009 {
 000010     f = fopen("dd:INFILE",
 000011               "rb,type=record,recfm=fb,lrecl=80");
 000012     if ( !f )
 000013     {
 000014        perror("INFILE open error!\n");
 000015        exit(8);
 000016     }
 000017     while ( 1 )
 000018     {
 000019         l = fread( r, 1, 80, f) ;
 000020         if ( l == 0 )
 000021            break;
 000022         k +=1;
 000023         printf("%s\n",r);
 000024     }
 000025     fclose(f);
 000026     printf("records read %d\n",k);
 000027     return (0);
 000028 }
 ****** **************************** Bottom of Data ****************************
cheers
enrico
When I tell somebody to RTFM or STFW I usually have the page open in another tab/window of my browser,
so that I am sure that the information requested can be reached with a very small effort
enrico-sorichetti
Global moderator
 
Posts: 2994
Joined: Fri Apr 18, 2008 11:25 pm
Has thanked: 0 time
Been thanked: 164 times

Re: Character Arrays

Postby enrico-sorichetti » Sat May 04, 2013 1:23 pm

this is a snippet working according ( somehow ) to Your logic

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int k = 20;
int i = 0 ;
 
FILE *f;
int l;
char b[80];
 
char **rt;
struct rs
 {
    char seqn[6] ;
    char wsp1[1] ;
    char tail[73] ;
 } r;
 
char wseqn[7] ;
char wtail[74] ;
 
int main(int argc, char **argv)
{
    f = fopen("dd:INFILE",
              "rb,type=record,recfm=fb,lrecl=80");
    if ( !f )
    {
       perror("INFILE open error!\n");
       exit(8);
    }
    rt = (char **) malloc(k  * sizeof(char *));
    if ( !rt )
    {
       perror("error allocating rt!\n");
       exit(8);
    }
 
    while ( ( i < k  ) && !feof(f) )
    {
        l = fread( b, 1, 80, f) ;
        if ( l == 0 )
           break;
        printf("record-number/array-index %d/%d\n", i+1, i);
        printf("the I/O buffer >>%s<<\n", b);
        rt[i] =  (char *) malloc(80 * sizeof(char *));
        if ( !rt[i]  )
        {
            perror("error allocating rt entry !\n");
            exit(8);
        }
        memcpy(&rt[i], &b, 80);
        printf("the array      >>%s<<\n", &rt[i]);
 
        memcpy(&r,&rt[i],80);
        strncpy(wseqn, r.seqn, 6);
        printf("the <struct>   >>%s<<\n", wseqn);
 
        i +=1;
    }
    fclose(f);
    printf("records read %d\n",i);
    return (0);
}


but it would be better to start exercising with arrays of structures...

and NOTE that Your original code did not take into account the NULL terminator for strings
cheers
enrico
When I tell somebody to RTFM or STFW I usually have the page open in another tab/window of my browser,
so that I am sure that the information requested can be reached with a very small effort
enrico-sorichetti
Global moderator
 
Posts: 2994
Joined: Fri Apr 18, 2008 11:25 pm
Has thanked: 0 time
Been thanked: 164 times

Re: Character Arrays

Postby enrico-sorichetti » Mon May 06, 2013 12:27 pm

disregard my previous snippet...
every time I work with pointers, arrays structures,
it takes me two days to remember the frigging logic

this should work better

the code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
 
#ifndef VCOPY
#define VCOPY(_V_) \
        strncpy(w ## _V_ , r. ## _V_, sizeof(r. ## _V_))
#endif
 
int k = 10;
int i = 0 ;
 
FILE *f;
int l;
 
char **rt = NULL;
char  *rb = NULL;
struct rs
{
    char seqn[6] ;
    char wsp1[1] ;
    char tail[73] ;
} r;
 
char wseqn[7] ;
char wtail[74] ;
 
int main(int argc, char **argv)
{

    /* array of pointers */
    rt = (char **) malloc(k * sizeof(char *));
    if ( !rt )
    {
       perror("error allocating rt!\n");
       exit(8);
    }
 
    f = fopen("dd:INFILE",
              "rb,type=record,recfm=fb,lrecl=80");
    if ( !f )
    {
       perror("INFILE open error!\n");
       exit(8);
    }
 
    while ( ( i < k ) && !feof(f) )
    {
        /* input buffer to be retained */
        rb  = (char *) malloc(80 * sizeof(char *));
        if ( !rb  )
        {
            perror("error allocating rb!\n");
            exit(8);
        }
        l = fread( rb, 1, 80, f) ;
        if ( l == 0 )
           break;
 
        /* save the input buffer address */
        rt[i] = rb;
        i +=1;
    }
 
    k = i;
    fclose(f);
    printf("records  (%d)\n",i);
 
    for ( i=0; i < k; i++)
    {
        memcpy(&r,rt[i],80);
        VCOPY(seqn);
        printf("<seqn>   >%s<\n", wseqn);
        VCOPY(tail);
        printf("<tail>   >%s<\n", wtail);
 
    }
 
    return (0);
}


the input file ( larger than the builtin max )
000100 AAAA
000200 AAAA
000300 AAAA
000400 AAAA
000500 AAAA
000600 AAAA
000700 AAAA
000800 AAAA
000900 AAAA
001000 AAAA
001100 AAAA
001200 AAAA
001300 AAAA
001400 AAAA
001500 AAAA


the result
 records  (10)
 <seqn>   >000100<
 <tail>   >AAAA                                                                     <
 <seqn>   >000200<
 <tail>   >AAAA                                                                     <
 <seqn>   >000300<
 <tail>   >AAAA                                                                     <
 <seqn>   >000400<
 <tail>   >AAAA                                                                     <
 <seqn>   >000500<
 <tail>   >AAAA                                                                     <
 <seqn>   >000600<
 <tail>   >AAAA                                                                     <
 <seqn>   >000700<
 <tail>   >AAAA                                                                     <
 <seqn>   >000800<
 <tail>   >AAAA                                                                     <
 <seqn>   >000900<
 <tail>   >AAAA                                                                     <
 <seqn>   >001000<
 <tail>   >AAAA                                                                     <


the input file ( smaller than the builtin max)

000100 AAAA
000200 AAAA
000300 AAAA
000400 AAAA
000500 AAAA
000600 AAAA


the result

 records  (6)
 <seqn>   >000100<
 <tail>   >AAAA                                                                     <
 <seqn>   >000200<
 <tail>   >AAAA                                                                     <
 <seqn>   >000300<
 <tail>   >AAAA                                                                     <
 <seqn>   >000400<
 <tail>   >AAAA                                                                     <
 <seqn>   >000500<
 <tail>   >AAAA                                                                     <
 <seqn>   >000600<
 <tail>   >AAAA                                                                     <
cheers
enrico
When I tell somebody to RTFM or STFW I usually have the page open in another tab/window of my browser,
so that I am sure that the information requested can be reached with a very small effort
enrico-sorichetti
Global moderator
 
Posts: 2994
Joined: Fri Apr 18, 2008 11:25 pm
Has thanked: 0 time
Been thanked: 164 times

Re: Character Arrays

Postby enrico-sorichetti » Mon May 13, 2013 7:40 pm

sometimes I wonder why we waste our time polishing, testing and posting solutions/suggestions
for morons who do not care to follow up and tell if the suggested solution was helpful :evil:
cheers
enrico
When I tell somebody to RTFM or STFW I usually have the page open in another tab/window of my browser,
so that I am sure that the information requested can be reached with a very small effort
enrico-sorichetti
Global moderator
 
Posts: 2994
Joined: Fri Apr 18, 2008 11:25 pm
Has thanked: 0 time
Been thanked: 164 times

Re: Character Arrays

Postby dick scherrer » Mon May 13, 2013 10:23 pm

Hi Enrico,

One thing that "gets me by" is knowing how many people other than the TS read these posts. This topic is up to 105 views.

fwiw.

d
User avatar
dick scherrer
Global moderator
 
Posts: 6268
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 93 times


Return to C, C++

 


  • Related topics
    Replies
    Views
    Last post