Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old July 28th, 2003, 03:59 AM
Registered User
 
Join Date: Jul 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Reading from a text file and writing to a Binary 1

Hi Guys,

I want to write my data read froma text file and write to binary file. Please help!!!!


#include <stdio.h>
#include <stdlib.h>
char * itobs (int n, char * ps);
int invert_end (int num, int bits);
int main (void)
{
    char bin_str[4 * sizeof (int) + 1];
    int number =0;
    char c[10];
    FILE *file;
    FILE *stream;
    file = fopen("numbers1.dat", "r");
    stream = fopen("numbers2.dat", "wb");
    if(file==NULL)
    {
        printf("Error: can't open file.\n");
        return 1;
    }
    else
    {
        printf("File opened successfully.\n");
        while(fgets(c, 16, file)!=NULL)
        {
            number= atoi(c);
            fprintf(stream,"%s", itobs(number, bin_str));
        }
        printf("Now closing file.\n");
        fclose(file);
        fclose(stream);
      }
    return 0;
}
char * itobs(int n, char *ps)
{
    int i; static int size = 4 * sizeof(int);
    for (i = size - 1; i >= 0; i--, n>>= 1)
    ps[i] = (01 & n) + '0';
    ps[size] = '\0';
    return ps;
}






Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading and writing .PST file shoakat ASP.NET 2.0 Basics 1 January 25th, 2011 08:31 AM
Read Text file and convert to Binary file VB.net sjlsysprg1 Pro VB.NET 2002/2003 4 June 29th, 2007 06:53 AM
Opening Text file in Binary mode for reading lawsoncobol Access VBA 1 August 3rd, 2006 04:01 AM
Reading from a xml file and writing to another billy_bob_the_3rd XML 5 April 29th, 2005 01:40 PM
Reading field of 'Long' datatype from binary file peri C# 0 January 2nd, 2005 08:50 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.