![]() |
new unix problem: signals and write() calls
The following code will print prime numbers and output them to a file specified by the user. Also, it will call the alarm() based on values passed on the command line. The SIGALRM and the SIGUSR1 will cause the program to print the last prime number generated. I'm having problems doing my write(). It should be the most straightforward function but I'm really tired and I can't seem to figure it out. There's also a bug with the way I implemented my command line arguments to call the alarm() but I'm not requesting help with that. I just haven't gotten to fixing it yet.
Thanks in advance. Sorry for how ugly the code is. :
#include <iostream> |
Did you even look up the prototype of write?
:
ssize_t write(int fildes, const void *buf, size_t nbyte); |
Well that certainly explains why I wasn't getting readable files. But I included <stdlib.h> and itoa() is reported as undefined. Does Gcc not support it?
Edit: A little googling made me aware of sprintf(). I'm going to try that. Edit2: sprintf works for my purposes. |
This code works for the most part. I don't know how to format the output though. Ideally, I'd like the output file to look similar to this:
:
1Thanks in advance and again sorry for the mess. :
#include <iostream> |
You need to be more than aware of sprintf; you need to read its docs (and possibly follow the link to printf). If you want a newline after the number, consider using "%d\n".
|
You are indeed right DaWei. I was in a rush to fix the code and didn't bother reading up on sprintf. I have the code working however.
Thanks for the help GE. |
Quote:
Quote:
An alternative that avoids all of these allocation size issues would be to use stringstreams. This lets you use the stream I/O syntax you're already familiar with. Info to get you started can be found here. |
Quote:
|
| All times are GMT -5. The time now is 1:35 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC