Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

a question about unix programming

Posted on 2006-03-21
5
Medium Priority
?
325 Views
Last Modified: 2010-04-17
dear:

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!
0
Comment
Question by:wolf415
5 Comments
 
LVL 6

Expert Comment

by:liviutudor
ID: 16246267
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.
0
 
LVL 6

Expert Comment

by:liviutudor
ID: 16246302
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.

0
 
LVL 27

Accepted Solution

by:
Nopius earned 80 total points
ID: 16253547
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));
0
 
LVL 85

Expert Comment

by:ozo
ID: 16253567
#include <unistd.h>
truncate("filename", 0);

#include <stdio.h>
char string[100];
int i=9;
sprintf(string,"%d",i);
0
 

Author Comment

by:wolf415
ID: 16256442
ThANK YOU!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
Loops Section Overview
Screencast - Getting to Know the Pipeline

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question