Thread: Print binary numbers to disk file, problem

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    71

    Print binary numbers to disk file, problem

    Could somebody tell how I can actually write the input and output to a disk file?
    When I open the disk file, it only has the description, but not the data

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <process.h>
    
    int main(void)
    {
       FILE *test2_file;
       int num;
       int no_bits;
       int reply = 0;
    
    
       test2_file = fopen ("decimal2binary.txt", "w");
       fprintf (test2_file, "This is the content of the decimal2binary file.");
    
       do
       {
          system("cls");
          printf("\n                Welcome to the DECIMAL to BINARY program!");
          printf("\nThis program accepts a decimal number and converts it into a ");
          printf("binary number.\nIt also states how many bits the number occupies.");
          printf("\n\nEnter a number: ");
          scanf("%d",&num);
          no_bits=count_bits(num);
          printf("The number of bits is %d",no_bits);
          bin_print(num,no_bits);
          printf("\n");
          printf("Would you like to input another number (y/n): ");
          reply = getche();
          if ((reply != 'n') && (reply != 'N'))
             {
                printf("\n\nTo terminate inputs you need to reply N or n -");
                printf(" your reply was %c", reply);
             }
       }
       while ((reply == 'y') || (reply == 'Y'));
       fclose(test2_file);
    
       printf("\n\n");
       printf("Bye friends!\n");
       system("pause");
    }
    
    count_bits(int n)
    {
       int i = 0;
    
       for (;n!=0; n = n >> 1)
       i++;
    
       return(i);
    }
    
    
    bin_print(int x, int n_bits)
    {
       int j;
    
       printf("\nno. %d in binary: ",x);
    
       for( j = n_bits - 1; j >= 0; j--)
       printf("%i",( x >> j ) & 01);
    }

  2. #2
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    man fprintf, fscanf, fgets

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Precisely. printf is writing to stdout, i.e. the console screen. fprintf will write to the open file.

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by Guti14
    When I open the disk file, it only has the description, but not the data
    Code:
    test2_file = fopen ("decimal2binary.txt", "w");
    You're re-creating the file, therefore the data is erased...
    Should be:
    Code:
    test2_file = fopen ("decimal2binary.txt", "r+");
    to have both read/write acess to the file.
    Last edited by xErath; 10-03-2004 at 09:12 PM.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You need to pass your output file to the print function:
    Code:
          bin_print(test2_file,num,no_bits);
    Then bin_print() would be:
    Code:
    bin_print(FILE *fp, int x, int n_bits)
    {
       int j;
    
       fprintf(fp,"\nno. %d in binary: ",x);
    
       for( j = n_bits - 1; j >= 0; j--)
       fprintf(fp,"%i",( x >> j ) & 01);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-09-2008, 07:27 AM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  3. Writing a struct to a binary file problem
    By k_w_s_t_a_s in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 10:03 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. What kind of programs should I start writing?
    By Macabre in forum C++ Programming
    Replies: 23
    Last Post: 04-12-2003, 08:13 PM