• C

sending array of char in a function?ERROR

int lenGCos(const char* str)
{
        char* t;
            int count=0;
            int start=0;
            int end=0;
            int i=0;

            while(i<strlen(str))
        {
                  if(str[i]==':' && count==4)
                        start=i;
                  if(str[i]==':' && count==5)
                        end=count;
                  i++;
        }

        return (start-end);
}

// icall the function from here..
printf("The string lebgth is %s", lenGCos(line));
Carl3003Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

van_dyCommented:
>>  int lenGCos(const char* str)  return type of lenGCos() is integer

>> printf("The string lebgth is %s", lenGCos(line));  you are using %s, %s is for printing out string. use %d insted
0
Carl3003Author Commented:
printf("The string lebgth is %d", lenGCos(line));

I have this error msg:
warning: passing arg 1 of `lenGCos' from incompatible pointer type
0
Carl3003Author Commented:
it works now but i dont see the value.
      while(fgets(line[i], 512, fp)!=NULL)
      {
            char str[10];
            sprintf(str,"%d",getuid());

            if( strstr( line[i], str)!=NULL)
            {
                  printf("The string lebgth is %d", lenGCos(line[i]));
                  //UpdateGecos(line[i], buf);
            }
            i++;

      }
0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

van_dyCommented:
how have you declared line[] ??
is it
char line[SOME_NUMBER];
 ??

in that case use

while(fgets(line, 512, fp)!=NULL)
0
van_dyCommented:
while(fgets(line, 512, fp)!=NULL)
     {
          char str[10];
          sprintf(str,"%d",getuid());

          if( strstr( line, str)!=NULL)
          {
               printf("The string lebgth is %d", lenGCos(line));
               //UpdateGecos(line[i], buf);
          }
          i++;

     }

how ever i would reccommend you to post you entire code..
0
Carl3003Author Commented:
Actually i am getting a value but its always 0
0
Carl3003Author Commented:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>

#define TRUE  1
#define FALSE 0


#define MAX_USER 256
#define MAX_CHAR 512

int lenGCos(const char* str);
char* SubString(const char* str, int from, int to);
int FindNthChar(const char* str, char c, int nth);
int UpdateGecos(char* cur_entry, char* buf);

int main()
{
      char buf[512];
      printf("new gcos :  ");      
    if( fgets(buf, 512, stdin) == NULL)
            exit(1);
      else
        buf[strlen(buf)-1] = '\0';

    char entries[MAX_USER][MAX_CHAR];
    int not_updated = TRUE;        
    char* uid_str;
    int i, fd;
    int index = 0;
      fd = open("passwd", O_RDWR);
      FILE *fp=fopen("passwd","r");
    if(fp==NULL)
    {
            //perror(av[1]); exit(1);
    }
      char line[256][512];
      char current;
      int count=0;
      i=0;

      while(fgets(line[i], 512, fp)!=NULL)
      {
            char str[10];
            sprintf(str,"%d",getuid());

            if( strstr( line[i], str)!=NULL)
            {
                  printf("The string lebgth is %d", lenGCos(line[i]));
                  //UpdateGecos(line[i], buf);
            }
            i++;

      }
/*
    while(fgets(line[index], 512, fp) )
    {
            if(not_updated)
        {
                  uid_str = SubString(line[index], FindNthChar(line[index], ':', 2)+1, FindNthChar(line[index], ':', 3)-1);
                  if(getuid() == atoi(uid_str))
                  {
                              UpdateGecos(line[index], buf);
                              not_updated = FALSE;  
                  }
        }
        index++;
      }
        */
       
        ftruncate(fd, 0);                                               // Make file 512 0(zero)
       
        for(i=0 ; i < index ; i++)
               write(fd, line[i], strlen(line[i]));  

       // close(fd);
        fclose(fp);
       
        return 0;
}

/* update gecos fiend of cur_entry to buf */
int UpdateGecos(char* cur_entry, char* buf)
{
        int str_count = 0;
        char new_entry[512] = "";
       
        str_count = strlen( SubString(cur_entry, 0, FindNthChar(cur_entry, ':', 4)) );
        str_count += strlen(SubString(cur_entry, FindNthChar(cur_entry, ':', 5), strlen(cur_entry)-1));
       
        strcat(new_entry, SubString(cur_entry, 0, FindNthChar(cur_entry, ':', 4)));
        strncat(new_entry, buf, 512 - str_count);
        strcat(new_entry, SubString(cur_entry, FindNthChar(cur_entry, ':', 5), strlen(cur_entry)-1));
       
        strcpy(cur_entry, new_entry);
}

int lenGCos(const char* str)
{
        char* t;
            int count=0;
            int start=0;
            int end=0;
            int i=0;

            while(i<strlen(str))
        {
                  if(str[i]==':' && count==4)
                        start=i;
                  if(str[i]==':' && count==5)
                        end=count;
                  i++;
        }

        return (start-end);
}


/* return sub-string of str from 'from' to 'to' which are indexes of the string */
char* SubString(const char* str, int from, int to)
{
        char* t;
        int i=0;
       
        t = (char*)malloc(to - from + 2);
        while(from <= to)
        {
                t[i++] = str[from++];
        }
        t[i] = '\0';
        return t;
}

       
/* Find Nth char and return its index */
int FindNthChar(const char* str, char c, int nth)
{
        int i=0, j=0;
        while(str[i] != '\0')
        {
                if(str[i++] == c && ++j == nth)
                        return i-1;
        }
        return -1;
}
0
van_dyCommented:
int lenGCos(const char* str)
{
        char* t;
          int count=0;
          int start=0;
          int end=0;
          int i=0;

          while(i<strlen(str))
        {
               if(str[i]==':' && count==4)
                    start=i;
               if(str[i]==':' && count==5)
                    end=count;
               i++;
        }

        return (start-end);
}

what is this above function actually supposed to do?
can you explain with an input string?? like what length do u want it to return ?
0
Carl3003Author Commented:
okk....i think i forgto to increament count++;
thats why
0
van_dyCommented:
this function, lenGCos() is always goign to return 0.
this is because, count and end remain 0 once the while() loop
terminates. i think you should update the variable count somewhere
in the loop because it will remain zero throughout and the two
conditions  str[i]==':' && count==4  &  str[i]==':' && count==5
will never be satisfied, there by the loop causes no efect on the values of
start and end,
 
0
Carl3003Author Commented:
Its supposed to return the fName + lName
int lenGCos(const char* str)
{
        char* t;
            int count=0;
            int start=0;
            int end=0;
            int i=0;

            while(i<strlen(str))
        {
                  if(str[i]==':' && i==4)
                        start=i;
                  if(str[i]==':' && i==5)
                        end=count;
                  i++;

        }

        return (start-end);
}

i will parse a line of the text file
0
Carl3003Author Commented:
myusername:x:userid:300:fName lName:/data/myuserid:/bin/bash
0
van_dyCommented:
hmm, try this

//not tested.
int lenGCos(const char* str)
{
        char user[100], somename[100], uid[100], uno[100], fullname[100], data[100], shellname[100];
        sscanf(str, "%s:%s:%s:%s:%s:%s:%s", user, somename, uid, uno, fullname, data, shellname);
        return strlen(fullname);
}
0
van_dyCommented:
please ignore the previous post, it
willl not work.

use this for lenGCos()


int lenGCos(const char* str)
{
      int i = 0;
      int count = 0;
      int j = 0;
      while(count != 4){
            if(str[i++] == ':')
                  count++;
      }
      j = i;
      while(count != 5){
            if(str[j++] == ':')
                  count++;
      }
      return j - i - 1;
}

0
Carl3003Author Commented:
i dont understand but i always get the same numbers even though i change fName and lName the size always remains a constant
0
van_dyCommented:
umm, i dont see why should that happen.
are you using the function lenGCos() from my last post?
0
van_dyCommented:
Just try this simple program and see if it correctly prints out
the lengths.

#include <stdio.h>

int lenGCos(const char* str)
{
     int i = 0;
     int count = 0;
     int j = 0;
     while(count != 4){
          if(str[i++] == ':')
               count++;
     }
     j = i;
     while(count != 5){
          if(str[j++] == ':')
               count++;
     }
     return j - i - 1;
}


int main()
{    
         char line[256][512];
          FILE *fp=fopen("passwd","r");
     while(fgets(line[i], 512, fp)!=NULL)
     {
          char str[10];
          sprintf(str,"%d",getuid());

          if( strstr( line[i], str)!=NULL)
          {
               printf("The string lebgth is %d", lenGCos(line[i]));
               //UpdateGecos(line[i], buf);
          }
          i++;

     }
     return 0;
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Carl3003Author Commented:
yes.. thats how i change the names. when i open the files i see the names are changed but when i run again i see the same size as before
0
van_dyCommented:
are you trying to change the :fName lName: entry in every line whose uid is = current userid ?
0
Carl3003Author Commented:
the uid is unuique. it should return only one line with that current uid
0
Carl3003Author Commented:
i think i made it working.. thanks a lot
0
van_dyCommented:
Hi Carl,
     Sorry for the delay, there was some problem with
connectiviy here. anyway  here is what i was suggesting.
i understand these are your requirements
1) you havea "passwd" file,.
2) you want to seach the fie for a certain uid
3) you take as input, the firstname and last name.
4) you replace the first name and last names for the present uid
     with what you have asked from the user.

There is a siple way to implement this. consider:
-----------------
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>

int main()
{
          char line[256][512];
        char change[256];
          FILE *fp = fopen("passwd", "r");
          char lName[25];
          char fName[25];
          char str[10];
        int i = 0;
        int j = 0;

           printf("Please enter new first name :");
           scanf("%s", fName);
           printf("Please enter new last name :");
           scanf("%s", lName);

          if(!fp){
               printf("unable to open file");
              return 1;
           }
           sprintf(str, "%d", getuid());
         while(fgets(line[i], 512, fp)){
               if(strstr(line[i], str)){
                     int count = 0;
                     strcpy(change, line[i]);
                     j = 0;
                     while(count != 4){
                           if(line[i][j] == ':')
                                 count++;
                           j++;
                     }
                     line[i][j] = '\0';
                     strcat(line[i], fName);
                     strcat(line[i], " ");
                     strcat(line[i], lName);
                     while(change[++j] != ':');
                     strcat(line[i], change + j);
               }
               i++;
         }

         fclose(fp);
         unlink("passwd");;
         fp = fopen("passwd", "w");
         for(j = 0; j < i; j++)
               fputs(line[j], fp);

         fclose(fp);
         return 0;
}
---------------------
guess this will work for you. if that is what you wanted and there is something u are unclear about, you can continue asking in this same thread.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.