a question about unix programming

Posted on 2006-03-21
Last Modified: 2010-04-17

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!
Question by:wolf415
    LVL 6

    Expert Comment

    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.
    LVL 6

    Expert Comment

    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.

    LVL 27

    Accepted Solution

    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));
    LVL 84

    Expert Comment

    #include <unistd.h>
    truncate("filename", 0);

    #include <stdio.h>
    char string[100];
    int i=9;

    Author Comment

    ThANK YOU!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Suggested Solutions

    Title # Comments Views Activity
    Doc'in system (example?) BA 7 64
    tripleUp challenge 7 54
    wefewf 2 28
    strCopies  challenge 17 60
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    A short article about problems I had with the new location API and permissions in Marshmallow
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now