473,796 Members | 2,522 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What is the problem with the code?

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(&star tTm);
//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,statu s);

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

Nov 15 '05 #1
5 1569
In article <11************ *********@o13g2 000cwo.googlegr oups.com>,
yezi <ye*****@hotmai l.com> 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
Nov 15 '05 #2
Walter Roberson wrote:
yezi <ye*****@hotmai l.com> 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

Nov 15 '05 #3
In article <11************ **********@z14g 2000cwz.googleg roups.com>,
Peter Nilsson <ai***@acay.com .au> 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
Nov 15 '05 #4
ro******@ibd.nr c-cnrc.gc.ca (Walter Roberson) writes:
In article <11************ **********@z14g 2000cwz.googleg roups.com>,
Peter Nilsson <ai***@acay.com .au> 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_Keit h) ks***@mib.org <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.
Nov 15 '05 #5
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?"
Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
19176
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have any preconceived ideas about it. I have noticed, however, that every programmer I talk to who's aware of Python is also talking about Ruby. So it seems that Ruby has the potential to compete with and displace Python. I'm curious on what basis it...
699
34264
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro capabilities, unfortunately. I'd like to know if it may be possible to add a powerful macro system to Python, while keeping its amazing syntax, and if it could be possible to add Pythonistic syntax to Lisp or Scheme, while keeping all of the...
92
6545
by: Reed L. O'Brien | last post by:
I see rotor was removed for 2.4 and the docs say use an AES module provided separately... Is there a standard module that works alike or an AES module that works alike but with better encryption? cheers, reed
137
7199
by: Philippe C. Martin | last post by:
I apologize in advance for launching this post but I might get enlightment somehow (PS: I am _very_ agnostic ;-). - 1) I do not consider my intelligence/education above average - 2) I am very pragmatic - 3) I usually move forward when I get the gut feeling I am correct - 4) Most likely because of 1), I usually do not manage to fully explain 3) when it comes true. - 5) I have developed for many years (>18) in many different environments,...
125
14860
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from software giant such as Microsoft SQL Server, Oracle, and Sybase? Is PostgreSQL reliable enough to be used for high-end commercial application? Thanks
44
4289
by: lester | last post by:
a pre-beginner's question: what is the pros and cons of .net, compared to ++ I am wondering what can I get if I continue to learn C# after I have learned C --> C++ --> C# ?? I think there must be many know the answer here. thanks
12
3304
by: Steven T. Hatton | last post by:
This is something I've been looking at because it is central to a currently broken part of the KDevelop new application wizard. I'm not complaining about it being broken, It's a CVS images. Such things happen. The whole subsystem is going through radical changes. I don't really want to say what I think of the code just yet. That would influence the opinions of others, and I really want to know how other people view these things,...
121
10186
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode support IDEs are DreamWeaver 8 and Zend PHP Studio. DreamWeaver provides full support for Unicode. However, DreamWeaver is a web editor rather than a PHP IDE. It only supports basic IntelliSense (or code completion) and doesn't have anything...
46
4263
by: Keith K | last post by:
Having developed with VB since 1992, I am now VERY interested in C#. I've written several applications with C# and I do enjoy the language. What C# Needs: There are a few things that I do believe MSFT should do to improve C#, however. I know that in the "Whidbey" release of VS.NET currently
13
5060
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
0
9531
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10459
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10187
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9055
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7553
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6795
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5446
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3735
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.