Thread: How can I write binary to a file?

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    21

    Unhappy How can I write binary to a file?

    I am 71 years old and haven’t programmed in C for many years. So, this is going slow.
    I am using Borland C++ IDE to write 16 bit code for a picoFlash single board computer with a 186 compatible processor and 512K of Ram. I am adding code to an existing program that takes 183 pages to print. Right now I am trying to write the contents of a string to a file opened using “wb”.
    The compiler and linker don’t give any error messages.
    The I have extracted the relevant code as shown below.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <setjmp.h>
    #include <dos.h>
    #include <alloc.h>
    #include <mem.h>
    #include <time.h>
    #include <tcp.h>
    #include <conio.h>
    #include "g5lib.h"
    #include "cdacs.h"
    #include "comms.h"
    #include "comms2.h"
    #include "procio.h"
     
    extern char far rbuffer[4096];
                    // extern because it is created by another module
                    //far because I ran short of memory space
     
     
    void UploadAFile(char *strDirectory,char *strFileName, int intOffset)
    {
                    char far *farCharPtr;
                    int intNrCharsInLine;
                    char strFullFileName[121];
                    static FILE *TxtULFilePtr;
                    int intCnt;
     
                    Some code
     
                    //Open file for "wb"
                    TxtULFilePtr = fopen(strFullFileName, "wb");
     
                    Some code to calculate farCharPtr
                    Some code to calculate intNrCharsInLine
     
                   
                    //At this point rbuffer containes a string 
                          with imbedded carriage returns and line feeds
     
                    //null terminate rbuffer to avoid the need
                         to create a second large array
                    rbuffer[intOffset] = '\0';                               
                    //Note: If I comment out this line 
                           it doesn’t change the output.
                   
                    //The following six lines were added 
                             to verify the contents of rbuffer during test
    
                     for(intCnt=0;intCnt<intNrCharsInLine;intCnt++)
                     {
                                    farCharPtr = farLinePtr + intCnt;
                                    strncpy(strChar,farLinePtr + intCnt,1);
                                    printf("#204D==============%s==
                                             =========\n",strChar);
                     }
     
                    //Write from rbuffer to file
                    fprintf(TxtULFilePtr,"%s",farLinePtr);
                    fclose(TxtULFilePtr);
     
    }
    In a simple test case, I have a string of characters that were read from a text file. I want to write them into a new file (strFullFileName). The original file contains:

    This is test line 1
    This is test line 2
    This is test line 3
    This is test line 4

    Right now the first two lines are put in a string and passed to this module in “rbuffer”.
    When I run the program, the for loop gives me:

    #204D==============T===========
    #204D==============h===========
    #204D==============i===========
    #204D==============s===========
    #204D============== ===========
    #204D==============i===========
    #204D==============s===========
    #204D============== ===========
    #204D==============t===========
    #204D==============e===========
    #204D==============s===========
    #204D==============t===========
    #204D============== ===========
    #204D==============L===========
    #204D==============i===========
    #204D==============n===========
    #204D==============e===========
    #204D============== ===========
    #204D==============1===========
    #204D=========================
    #204D=========================
    #204D==============T===========
    #204D==============h===========
    #204D==============i===========
    #204D==============s===========
    #204D============== ===========
    #204D==============i===========
    #204D==============s===========
    #204D============== ===========
    #204D==============t===========
    #204D==============e===========
    #204D==============s===========
    #204D==============t===========
    #204D============== ===========
    #204D==============L===========
    #204D==============i===========
    #204D==============n===========
    #204D==============e===========
    #204D============== ===========
    #204D==============2===========
    ===================

    When I check the new file contents, only the first line appears.

    C:\>type test.txt
    This is test Line 1
    Question #1: How can I print the ASCII values next to the letters so I can verify that the nonprinting characters are carriage return and line feed and are not null characters?

    Question #2: Is fprintf(TxtULFilePtr,"%s",farLinePtr); the correct command to put the string in the file?
    Last edited by Jerry900; 12-10-2012 at 03:52 PM.

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Jerry900 View Post
    Question #1: How can I print the ASCII values next to the letters so I can verify that the nonprinting characters are carriage return and line feed and are not null characters?
    If you print a character with the %d format specifier, you'll get the ASCII value:
    Code:
    char test = 'A';
    printf("%c - %d\n", test, test);  // will print "A - 65"
    But then you need to change your debugging code:
    Code:
    for(intCnt = 0; intCnt < intNrCharsInLine; intCnt++)
    {
        char c = *(farLinePtr + intCnt);  // assuming farLinePtr is a char pointer to the beginning of the string
        // alternatively: char c = farLinePtr[intCnt];
       printf("character %d: %c - %d\n", intCnt, c, c);
    }
    Quote Originally Posted by Jerry900 View Post
    Question #2: Is fprintf(TxtULFilePtr,"%s",farLinePtr); the correct command to put the string in the file?
    Yes, but why do you open the file in binary mode when you want to write text into it?

    Bye, Andreas

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    21
    Thank you for the input. I found a way to print ASCII. It is a little more complex than your solution.

    I added the following code to my “for” loop.
    Code:
                    //Convert a string to a character to get the ASCII value
                    CharChar = strChar[0];   //Convert to nondimensiond variable
                    intChar = CharChar;                         //Convert to an integer
                    printf("#204D==============%s===========%u\n",strChar,intChar);
    Days ago when I first tried:
    Code:
     CharChar = strChar[0];
    I got an error message that got me off track.

    The new printout is:
    #204D==============T===========84
    #204D==============h===========104
    #204D==============i===========105
    #204D==============s===========115
    #204D============== ===========32
    #204D==============i===========105
    #204D==============s===========115
    #204D============== ===========32
    #204D==============t===========116
    #204D==============e===========101
    #204D==============s===========115
    #204D==============t===========116
    #204D============== ===========32
    #204D==============L===========76
    #204D==============i===========105
    #204D==============n===========110
    #204D==============e===========101
    #204D============== ===========32
    #204D==============1===========49
    #204D=========================0
    #204D=========================0
    #204D==============T===========84
    #204D==============h===========104
    #204D==============i===========105
    #204D==============s===========115
    #204D============== ===========32
    #204D==============i===========105
    #204D==============s===========115
    #204D============== ===========32
    #204D==============t===========116
    #204D==============e===========101
    #204D==============s===========115
    #204D==============t===========116
    #204D============== ===========32
    #204D==============L===========76
    #204D==============i===========105
    #204D==============n===========110
    #204D==============e===========101
    #204D============== ===========32
    #204D==============2===========50

    This shows that I have null characters at the end of line 1. So, I need to track down where they came from.

    I open the file in binary because I want to transfer binary files. The text file was just a simple test case.

    Thanks again

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    If you truly wish to write binary then the common method is fwrite function in C
    fwrite - C++ Reference
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 06-18-2012, 08:23 AM
  2. Write Binary File
    By doia in forum C Programming
    Replies: 14
    Last Post: 02-26-2010, 10:20 AM
  3. write in a binary file
    By singersinger in forum C++ Programming
    Replies: 1
    Last Post: 07-11-2007, 03:24 AM
  4. Write to binary file from 2D array
    By wbaker01 in forum C++ Programming
    Replies: 10
    Last Post: 11-29-2006, 06:16 AM
  5. How to store n write binary file?
    By megablue in forum C Programming
    Replies: 4
    Last Post: 10-23-2003, 03:53 AM