Thread: a question files

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    1

    a question files

    hi ppl

    i want someone please to answer me!

    how can write a data such as ( 0.7) to a file in a size which is smallest
    that is not a float , i want the least size that i can

    another question plz:
    if i write to a file the ascii of a number and i want to read it from the file ,will the c++ program understand it as the original number
    ex:
    putc(myfile,56);

    if i want to do
    getc(myfile);
    will the received char be the 56 ??????

    thnx in advance
    amin!

  2. #2
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    how can write a data such as ( 0.7) to a file in a size which is smallest
    that is not a float , i want the least size that i can

    I have no idea what you mean. Try to word your sentences using
    grammer that everyone can understand.


  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    If you want to treat an ascii representation as a number then you need to convert it, you can use atoi() or since you want a decimel point atof()

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    how can write a data such as ( 0.7) to a file in a size which is smallest
    that is not a float , i want the least size that i can
    I understand your question, but I don't know the answer. A float can hold from 1.2 x 10exp-23 to 3.4x10exp38, and all you want to store is a little .7 without wasting all those extra bits. I'm sure someone here will be able to tell you how to do that.

    if i write to a file the ascii of a number and i want to read it from the file ,will the c++ program understand it as the original number
    ex:
    putc(myfile,56);

    if i want to do
    getc(myfile);
    will the received char be the 56 ??????
    First, it looks like you have your arguments backwards:
    Code:
    int putc( int ch, FILE *stream );
    When I try this:

    putc(56, file1);

    It writes an 8 to file1. When I try this:

    char ch = getc(file1);

    and then display it to the console window, I get an 8. But, I believe the act of displaying it, converts the ASCII 56 in the variable to an 8.

    Since an ASCII 56 produces an 8 in the file, I can set up a test. I will write 56 to file1, close file1, open file1, then do this:

    char ch;
    getc(ch, file1);

    I won't know what is in ch at this point. But, if I write ch to a second file, and it produces an 8, then I will have this situation:

    putc(56, file1)----->8
    putc(ch, file2)----->8

    and that can only mean that ch is ASCII 56. Here goes...................there is an 8 in both files.
    Last edited by 7stud; 04-06-2005 at 06:04 AM.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I just found a trick for outputting the ASCII code of a char. You can do this:

    Code:
    char ch;
    ch=getc(ch, file1);
    
    int  ascii_code = ch;
    cout<<ch; //display the character
    cout<<ascii_code; //display the ascii code
    I think the lesson is: with a char type, C++ will convert the ascii number stored in the variable to the character whenever you try to display it, whether in a file or in the console window. If you want to actually see the ascii code, you need to convert the char variable to an int.
    Last edited by 7stud; 04-06-2005 at 06:16 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick question on reading files in C
    By TaiL in forum C Programming
    Replies: 12
    Last Post: 10-05-2008, 09:48 PM
  2. Replies: 26
    Last Post: 06-15-2005, 02:38 PM
  3. input/output files question
    By ssjnamek in forum C++ Programming
    Replies: 2
    Last Post: 04-22-2005, 12:38 PM
  4. Question about ".o" files
    By Jez_Master in forum C++ Programming
    Replies: 1
    Last Post: 04-11-2002, 01:54 AM
  5. Using files: Wacked Question
    By Denethor2000 in forum C++ Programming
    Replies: 2
    Last Post: 04-06-2002, 12:54 AM