a question about unix programming


in linux c programming
how to change int to char
and how to delete the content of a file(but don't delete the file)?

Thank you!
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Do you mean you want to convert the int value 9 into character '9'?
Simply do char c = '0' + int_value -- assuming that you have already tested that int_value is positive and not bigger than 9.
P.S. If you want to basically truncate the file to 0 bytes -- which is what i understand you are trying to achieve -- simply call fopen() with a parameter of "w" -- according to the man pages, fopen called with this parameter "Opens an empty file for writing. If the given file exists, its contents are destroyed." -- so after calling FILE * fp = fopen( "filename", "w" ); and then fclose(fp); you will end up with a file of 0 bytes -- either whether it existed or not before.

1) If the question is 'how to get character for given digit from '0' to '9', then I aggree with livitudor.
If the question is "how to cast from int to char", there is no easy way to change int to char due to non-portability between different CPU types and compilers.
2) I aggree with liviutudor, fopen() is ok, also you may also call (this is a direct syscall without extra payload):
close(open("filename", O_WRONLY | O_TRUNC, 0766));

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
#include <unistd.h>
truncate("filename", 0);

#include <stdio.h>
char string[100];
int i=9;
wolf415Author Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.