?
Solved

sending array of char in a function?ERROR

Posted on 2004-11-05
22
Medium Priority
?
502 Views
Last Modified: 2010-04-15
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));
0
Comment
Question by:Carl3003
  • 11
  • 11
22 Comments
 
LVL 5

Expert Comment

by:van_dy
ID: 12507967
>>  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
 

Author Comment

by:Carl3003
ID: 12508044
printf("The string lebgth is %d", lenGCos(line));

I have this error msg:
warning: passing arg 1 of `lenGCos' from incompatible pointer type
0
 

Author Comment

by:Carl3003
ID: 12508069
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:van_dy
ID: 12508092
how have you declared line[] ??
is it
char line[SOME_NUMBER];
 ??

in that case use

while(fgets(line, 512, fp)!=NULL)
0
 
LVL 5

Expert Comment

by:van_dy
ID: 12508108
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
 

Author Comment

by:Carl3003
ID: 12508126
Actually i am getting a value but its always 0
0
 

Author Comment

by:Carl3003
ID: 12508139
#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
 
LVL 5

Expert Comment

by:van_dy
ID: 12508237
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
 

Author Comment

by:Carl3003
ID: 12508252
okk....i think i forgto to increament count++;
thats why
0
 
LVL 5

Expert Comment

by:van_dy
ID: 12508268
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
 

Author Comment

by:Carl3003
ID: 12508273
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
 

Author Comment

by:Carl3003
ID: 12508283
myusername:x:userid:300:fName lName:/data/myuserid:/bin/bash
0
 
LVL 5

Expert Comment

by:van_dy
ID: 12508328
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
 
LVL 5

Expert Comment

by:van_dy
ID: 12508416
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
 

Author Comment

by:Carl3003
ID: 12508447
i dont understand but i always get the same numbers even though i change fName and lName the size always remains a constant
0
 
LVL 5

Expert Comment

by:van_dy
ID: 12508534
umm, i dont see why should that happen.
are you using the function lenGCos() from my last post?
0
 
LVL 5

Accepted Solution

by:
van_dy earned 1000 total points
ID: 12508592
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
 

Author Comment

by:Carl3003
ID: 12508605
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
 
LVL 5

Expert Comment

by:van_dy
ID: 12508677
are you trying to change the :fName lName: entry in every line whose uid is = current userid ?
0
 

Author Comment

by:Carl3003
ID: 12508715
the uid is unuique. it should return only one line with that current uid
0
 

Author Comment

by:Carl3003
ID: 12509545
i think i made it working.. thanks a lot
0
 
LVL 5

Expert Comment

by:van_dy
ID: 12511924
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question