Thread: Why the function behave like that?

  1. #1
    Registered User zahid's Avatar
    Join Date
    Aug 2001
    Posts
    531

    Why the function behave like that?

    Can anyone check the code?
    Copy all lines below and give a name.c and compile, run with 3 command line arguments.

    /*
    Add New: create_group_of_usrs.c
    Descrip: create a group of user from file
    Update : 11th Oct, 2001 */

    #include <stdio.h>
    #include <time.h>

    #define SESSION_ID_FILE "session_id_file"
    #define SESSION_ID_FILE_SIZE 1024

    int get_session_id(long parent_id, long *session_id)
    {
    FILE *fp;
    time_t t;
    struct stat *file_stat;

    t=time(NULL);

    srand(t);
    *session_id=rand();


    /* Problem start here*/
    stat(SESSION_ID_FILE, file_stat); /*This line is responsible*/

    /* How can I compare ?*/
    /*
    if(file_stat->st_size >= SESSION_ID_FILE_SIZE)
    fp = fopen(SESSION_ID_FILE,"w");
    else
    */
    /* Problem End here*/

    fp = fopen(SESSION_ID_FILE,"a");


    if(fp==NULL)
    {
    fp=fopen(SESSION_ID_FILE,"w");
    if(fp==NULL) printf("File Open Error\n");;
    return 0;
    }

    fprintf(fp,"%ld %ld %ld\n",*session_id,t,parent_id);
    fclose(fp);

    return 0;
    }


    int main (int argc, char *argv[])
    {
    long parent_id=4678;
    long session_id;

    printf("<!-- : %s : %s : %s :-->\n",argv[1],argv[2],argv[3]);
    /**Statement Okay*/
    get_session_id(parent_id, &session_id);
    printf("<!-- : %s : %s : %s :-->\n",argv[1],argv[2],argv[3]);
    /**Statement: Segmentation error*/

    return 0;
    }
    Last edited by zahid; 12-20-2001 at 02:15 AM.
    [ Never code before desk work ]
    -------------------------------------:-->
    A man who fears Nothing is the man who Loves Nothing
    If you Love Nothing, what joy is there in your life.
    =------------------------------------------------------= - I may be wrong.

  2. #2
    Registered User goran's Avatar
    Join Date
    Sep 2001
    Posts
    68
    Zahid,

    It could be a lot of help if you could post the problems you faced and the objective of the program.

    Anyway, here is the bug.

    you have defined
    struct stat *file_stat;

    which conflicts with
    stat(SESSION_ID_FILE, file_stat);
    since stat is a structure which you are trying to use as a function.

    Hope this helps.
    I don't wait for the future; it comes soon enough.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM