Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > What is the problem with the code?

Reply
Thread Tools

What is the problem with the code?

 
 
yezi
Guest
Posts: n/a
 
      10-05-2005
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);

 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      10-05-2005
In article <(E-Mail Removed). com>,
yezi <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Peter Nilsson
Guest
Posts: n/a
 
      10-05-2005
Walter Roberson wrote:
> yezi <(E-Mail Removed)> 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

 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      10-05-2005
In article <(E-Mail Removed) .com>,
Peter Nilsson <(E-Mail Removed)> 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
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-05-2005
http://www.velocityreviews.com/forums/(E-Mail Removed)-cnrc.gc.ca (Walter Roberson) writes:
> In article <(E-Mail Removed) .com>,
> Peter Nilsson <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed) <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.
 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      10-05-2005
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?"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem problem problem :( Need Help Mike ASP General 2 05-11-2004 08:36 AM



Advertisments