Need some help in writing a simple C program 
Author Message
 Need some help in writing a simple C program

Dear Harry,

     You need not invent a new bike. There are strtok() and strcpy()
in string.h
which can do all you need. You even don't need strlen().
Please consider the new, enhanced version of your program, which
not only finds all ',' delimited substrings in each of your input
array of strings, but also places them into output char array
out[10][cnt][100], where cnt is a counter of ',' in each of the
input string.
Note: your input data have to be allocated by DS segment
to be easily transferred into Oracle, define them in the static
section
of the C program (in the beginning of the C code, before main() ).

Regards,

Dr. Fairman, Ph.D.

Enhanced help for students on ALL subjects!
http://www.*-*-*.com/

Appendix: New program code:
===begin new code ===
#include <stdio.h>
#include <string.h>

/* this enhanced version uses strtok() and strcpy() from string.h
to do all you need and even more ( placing all ',' delimited
substrings of the each input string into 3-dim array out[][][]. Enloy!
*/

int i,w,t;
/* new */ int cnt[10]; /* =how much ',' in i- string */
/* new */ int j,n;
char *str;

/* new */       char out[10][20][100],*ss;

char *string1[] = {"CLB,11.100000,20021211163304",
"PB,8.860000,20021211163301",
"STNR,13.100000,20021211161717",
"TEM.SM,NoData",
"FP.FP,NoData",
"ANA.SM,NoData",
"UNA.NA,NoData",
"WVC.FP,NoData",
"MMT,5.810000,20021211163303",
"BRM,16.320000,20021211163301,this,enhanced,program,writes,all,tokens,into
the array=out,ENJOY IT!!!"};

int main(void)
{
    for (i=0;i<10;i++)
    {
    str = string1[i];
/*  new code: */
t = 0;
ss = strtok(str,",");
if(ss == NULL)
continue;
strcpy(&out[i][t][0],ss);
  for(;;){
ss = strtok(NULL,",");
if(ss != NULL)
{t++; cnt[i]++;
strcpy(&out[i][t][0],ss); }
else
break;
         }
/* printing out the content of the array "out"  */
for (n = 0 ; n <= cnt[i]; n++)
printf(" \nString number %d substring N = %d: Content =
%s",i,n,out[i][n]);
  }  /* end of "for (i=0;i<10;i++)"  */

Quote:
}

====end new code ===

Quote:

> I have not seen C since 1988 and yesterday, I was given a small
> program to write in C. Being a database guy I am not sure even where
> to begin from. But, last night I tried to put some simple concepts
> together. The requirement is:

> Accept a pointer array.Each element in the pointer array is a record
> that is comma delimited. Take each of these records, parse them out
> and write to an oracle database. I think I can figure out how to write
> to Oracle if I can parse the records out properly.

> Here is what I have so far ... now don't laugh since this is a hack
> and it is based on a day's worth of learning. For now, I just created
> the array locally just to get the code working. I just need to have
> something working very soon. Any help will be appreciated.

> Thanks

> #include <stdio.h>
> #include <string.h>
> #include <string.h>
> #include <stdlib.h>

> int i, k, w,l,m;
> int indx[3];
> char *column;
> char *str;
> char delimiter = ',';

> int str_len (char *s)
> {
>   char *p = s;

>   while (*p != '\0')
>       p++;
>   return p - s;
> }

> char* find_column(char *r,int start,int end)
> {
>     char *tmp = r;
>     char *col;
>     printf("%d %d\n",start,end);
>       while ((*tmp++ = *col++) && ((tmp-r) >=start) && ((tmp -r) <
> end))
>         {
>           ;
>         }
>      return (col);
> }

> int main(void)
> {
> char *string1[] = {"CLB,11.100000,20021211163304","PB,8.860000,20021211163301","STNR,13.100000,200212111617
> 17","TEM.SM,NoData","FP.FP,NoData","ANA.SM,NoData","UNA.NA,NoData","WVC.FP,NoData","MMT,5.810000,2002121116
> 3303","BRM,16.320000,20021211163301"};

> for (i=0;i<10;i++)
>   {
>     w=str_len(string1[i]);
>     char *str = string1[i];
>     k=1;
> /*    printf("%s  %i\n",string1[i],w); */
>     while (*str != '\0')
>       {
>         if ((*str == delimiter) && (*str != '\0'))
>            {
>             indx[k] = str - string1[i];
> /*            printf("%d \n",k); */
>             k++;
>            }
>         str++;
>       }
>     for (l=1;l<k;l++)
>     {
>       printf("%s %d %d  %d\n",string1[i],indx[l-1],indx[l],k);
>       column=find_column (string1[i],indx[l-1],indx[l]);
>     }
>  /*    printf ("FINAL %s\n",*column); */
>   }
> }

--



Wed, 01 Jun 2005 02:46:04 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. Need some help in writing a simple C program

2. Need Help on simple ISAM like program

3. need help in extremely simple c program

4. need help debugging simple c program

5. Need some help with tweeking a simple program

6. Help on a simple calculator program which needs Trigo, Arithmethic, log functions..[Apologies for repeat mail]

7. Need help with simple C Program

8. Simple BC+31 program - help needed

9. !Need help with simple C program

10. Need Help with a SIMPLE mouse control program in C.

11. Need Help in converting Simple C program to VC

12. Need help with simple dialog program?

 

 
Powered by phpBB® Forum Software