Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Closed Thread
 
Thread Tools Display Modes
  #1 (permalink)  
Old July 28th, 2003, 03:59 AM
Registered User
 
Join Date: Jul 2003
Location: , , .
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;
}

Closed Thread


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading and writing .PST file shoakat ASP.NET 2.0 Basics 1 January 25th, 2011 07: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 07:50 AM



All times are GMT -4. The time now is 05:35 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.