CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2001
    Posts
    86

    converting to binary file

    I have a file that needs to be converted to a binary file. To do this I understand I'll need to open a binary file pointer. However, when I do a

    xchar = fgetc(infile); // infile is non-binary
    // file pointer

    How do I make xchar into a binary so it can be written into the the binary file?




  2. #2
    Join Date
    Nov 1999
    Location
    CA
    Posts
    115

    Re: converting to binary file

    How about this:

    #include <stdio.h>
    #include <stdlib.h>



    void main()
    {
    FILE *inFile = NULL;
    FILE *outFile = NULL;

    if((inFile = fopen("textfile.txt", "rb")) == NULL)
    {
    fprintf(stderr, "Can't open file textfile.txt to read.\n");
    exit(1);
    }

    if ((outFile = fopen("binfile.bin", "wb")) == NULL)
    {
    fprintf(stderr, "Can't opne file binfile.txt to write.\n");
    exit(1);
    }

    int currChar;

    while((currChar = fgetc(inFile)) != EOF)
    fputc(currChar, outFile);

    fclose(inFile);
    fclose(outFile);
    }





  3. #3
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: converting to binary file

    Under Windows/DOS, all the program will do is change \r\n line endings to \n

    If that's all you require then it's fine.

    If the text file contains data such as numbers in text format and you want to store them in a binary format then it is far more complex. In that case you will need to know the format of the input file and parse it, then write it out to a pre-defined binary format.


    The best things come to those who rate

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured