Thread: How can I write binary to a file?

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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