What is the problem with the code?

Discussion in 'C Programming' started by yezi, Oct 5, 2005.

  1. yezi

    yezi Guest

    Hi: I am coding to output the result to the tmp file in order to send
    to the client. The following is the code I write to output the system
    call to a tmp file "tmp.txt". However, when running it, it generate the
    message like " Segmentation fault (core dumped)"

    Really confused by that . Any comments is welcomed.

    tmpfile = fopen("tmp.txt","a+");
    if(tmpfile == NULL) {
    perror("Error: can't open file.\n");
    exit(1);;
    }
    startTm = time(NULL);
    ptr = localtime(&startTm);
    //Print the time of request and Time on the server side
    fprintf(tmpfile,"The client IP is %s, The systime
    is,",inet_ntoa(their_addr.sin_addr));
    fprintf(tmpfile,asctime(ptr)); //show time problem ???
    fprintf(tmpfile,"\n The command is %s, The status is
    %s\n",buf,status);

    sep[20]="ls";
    //process the buffer content
    strcat(sep,"> tmp.txt");
    system(sep);
    yezi, Oct 5, 2005
    #1
    1. Advertising

  2. In article <>,
    yezi <> wrote:
    >Hi: I am coding to output the result to the tmp file in order to send
    >to the client. The following is the code I write to output the system
    >call to a tmp file "tmp.txt". However, when running it, it generate the
    >message like " Segmentation fault (core dumped)"


    > fprintf(tmpfile,"The client IP is %s, The systime
    >is,",inet_ntoa(their_addr.sin_addr));
    > fprintf(tmpfile,asctime(ptr)); //show time problem ???


    The second argument of fprintf is expected to be a format string.
    If the result of asctime() happened to include a % then the fprintf
    could try to convert a non-existant argument.

    You could try,

    fprintf(tmpfile,"The client IP is %s, The systime is, %s",
    inet_ntoa(their_addr.sin_addr), asctime(ptr) );


    As a general reminder: any time you are expecting a pointer to a
    system structure or newly allocated structure to be returned, then
    you should be checking for a NULL pointer return.
    --
    Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
    Walter Roberson, Oct 5, 2005
    #2
    1. Advertising

  3. Walter Roberson wrote:
    > yezi <> wrote:
    > > fprintf(tmpfile,"The client IP is %s, The systime
    > >is,",inet_ntoa(their_addr.sin_addr));
    > > fprintf(tmpfile,asctime(ptr)); //show time problem ???

    >
    > The second argument of fprintf is expected to be a format string.
    > If the result of asctime() happened to include a % then the fprintf
    > could try to convert a non-existant argument.
    >
    > You could try,
    >
    > fprintf(tmpfile,"The client IP is %s, The systime is, %s",
    > inet_ntoa(their_addr.sin_addr), asctime(ptr) );
    >
    >
    > As a general reminder: any time you are expecting a pointer to a
    > system structure or newly allocated structure to be returned, then
    > you should be checking for a NULL pointer return.


    Your points are valid in general, but note that asctime() cannot
    return a null pointer, nor can it return a pointer to a string
    containing a % character.

    [inet_ntoa() is not a standard C function, so it is off-topic in clc.]

    --
    Peter
    Peter Nilsson, Oct 5, 2005
    #3
  4. In article <>,
    Peter Nilsson <> wrote:
    >Your points are valid in general, but note that asctime() cannot
    >return a null pointer, nor can it return a pointer to a string
    >containing a % character.


    Is it impossible for a locale to define a timezone name or day or
    month name that contains a % character ?
    --
    When Love is gone, there's always Justice.
    When Justice is gone, there's always Force.
    When Force is gone, there's always Mom. -- Laurie Anderson
    Walter Roberson, Oct 5, 2005
    #4
  5. -cnrc.gc.ca (Walter Roberson) writes:
    > In article <>,
    > Peter Nilsson <> wrote:
    >>Your points are valid in general, but note that asctime() cannot
    >>return a null pointer, nor can it return a pointer to a string
    >>containing a % character.

    >
    > Is it impossible for a locale to define a timezone name or day or
    > month name that contains a % character ?


    asctime() ignores the locale; it returns a string of the form

    Sun Sep 16 01:03:52 1973\n\0

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Oct 5, 2005
    #5
  6. yezi

    Chris Dollin Guest

    yezi wrote:

    > sep[20]="ls";
    > //process the buffer content
    > strcat(sep,"> tmp.txt");


    `sep` wasn't declared, but I'm having difficulty imagining a
    declaration for it that allows its 20th element to be a char*
    and for `sep` itself to be a char*.

    Conclusion: something is broken.

    --
    Chris "electric hedgehog" Dollin
    "I know three kinds: hot, cool, and what-time-does-the-tune-start?"
    Chris Dollin, Oct 5, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=

    Fire Code behind code AND Javascript code associated to a Button Click Event

    =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=, Feb 10, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    21,218
    =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=
    Feb 11, 2004
  2. Alan Silver
    Replies:
    1
    Views:
    1,696
    Alan Silver
    Sep 15, 2005
  3. keithb
    Replies:
    1
    Views:
    906
    Bruce Barker
    Mar 29, 2006
  4. Replies:
    0
    Views:
    462
  5. thedarkman
    Replies:
    5
    Views:
    715
    Denis McMahon
    Sep 14, 2010
Loading...

Share This Page