Solved

How to Read/Write Text File and Binary File.

Posted on 2002-03-24
7
409 Views
Last Modified: 2013-11-15
My question is how to read/write text file and binary file one character at a time?

I try this one already.. but read nothing.  Why?

  FILE *fp;
  char temp;
  clrscr();
  if((fp=fopen("abc.txt","rt"))==NULL){
    cout<<"Error..."<<endl;
    exit(0);
  }
  else
    cout<<"No Error"<<endl;

  while(!feof(fp)){
    temp=fgetc(fp);
    cout<<"Read character="<<temp<<endl;getch();
  }


Please give me sample coding for read/write of text file and binary file.
please provide code in four methods. thanks
0
Comment
Question by:yongyih
  • 4
  • 2
7 Comments
 
LVL 1

Accepted Solution

by:
bluprint earned 50 total points
ID: 6892380
Here are two methods, each one shows a read and a write (for a total of all 4 combinations you wanted).

****** This will open a binary file and read each character and write each character to a different file

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


void main() {

FILE *fp, *fp2;
 char temp;
 
 if((fp=fopen("abc.txt","rb"))==NULL){
   cout<<"Error..."<<endl;
   exit(0);
 }
 else
   cout<<"No Error"<<endl;

 if((fp2=fopen("bcd.txt","wb"))==NULL){
   cout<<"Error..."<<endl;
   exit(0);
 }
 else
   cout<<"No Error"<<endl;

 temp=fgetc(fp);
 while(!feof(fp)){
      //read
   cout<<"Read character="<<temp<<endl;


   //write
   fputc(temp, fp2);

   //get next character
   temp=fgetc(fp);
 }
}

****** This will open a text file and read each character and write each character to a different file

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


void main() {

FILE *fp, *fp2;
 char temp;
 
 if((fp=fopen("abc.txt","rt"))==NULL){
   cout<<"Error..."<<endl;
   exit(0);
 }
 else
   cout<<"No Error"<<endl;

 if((fp2=fopen("bcd.txt","wt"))==NULL){
   cout<<"Error..."<<endl;
   exit(0);
 }
 else
   cout<<"No Error"<<endl;

 temp=fgetc(fp);
 while(!feof(fp)){
      //read
   cout<<"Read character="<<temp<<endl;


   //write
   fputc(temp, fp2);

   //get next character
   temp=fgetc(fp);
 }
}
0
 
LVL 2

Author Comment

by:yongyih
ID: 6893290
thanks for reply.
but why everytime i write to a binary file, why i still can read the content of the binary file?  (still in text format)
I open the file as binary already using fopen.

tell me why, ok.  and make sure that the reading of binary file working properly or not.  

after that, i will accept your answer, thanks again.
0
 

Expert Comment

by:GEliyahu
ID: 6893731
Check out the object CFile..it's the easiest way to take care in files.
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 1

Expert Comment

by:bluprint
ID: 6894233
The reason is that there is no real difference between a text file and a binary file, all files are made up of binary data.

As long as the only characters in the file are "text", that is, they have viewable values (values that a text editor knows), you will be able to read them with a text editor. If the characters have some non-viewable value, they will show up as funny looking characters.

Another difference is that some C functions will not work reading a "binary" file if it has certain types of characters, or rather, the functions won't work very predictably. When they reach the control characters, they may stop or they may keep reading. Functions like getc or putc just work one character at a time and don't care what kind of character it is.

Make sense?

Also, I ran both of these programs, and they ran fine.
0
 
LVL 1

Expert Comment

by:bluprint
ID: 6894458
After a little thought...there are a few other things about opening binary vs opening in text mode.

Opening in text mode will do some character translations.

For example, in Windows, a newline is represented by two characters, line-feed and carraige-return. When a file is opened in text mode, that two-character combination is translated into one character, represented by \n.

In binary mode, that translation doesn't occur.

There are probably lots of other examples of when character translations occur in text mode but not in binary mode...
0
 
LVL 2

Author Comment

by:yongyih
ID: 6895438
This is my problem now.. if i read from text file which has carriage return. e.g.
abc
def

in my program, it will read as abcdef or abc def
not remember has space between abc and def.

do you mind to tell me how to do the checking?
i try to check using '\n', but cannot..

anyway, i will accept your answer.

thanks for reply and hope you can reply me soon. =)
0
 
LVL 1

Expert Comment

by:bluprint
ID: 6896548
The way you check for a carraige return depends on the system you are on, and how you opened the file.

For a binary read, if a Unix/Linux system, check for \n (0A in hex), if on Windows check for (i think) \r\n (0D, 0A in hex)

That's one problem with opening something in binary, it doesn't translate the newline sequence in windows to a single character (from 0D0A to 0A).

If you open this in text mode, you can always check for \n and that's it.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem to file 3 74
Company IT policies concerning licensing 4 75
Easy to use inventory management software 7 92
Error creating a new C++ project in ,net 20 33
All of the resources available today make learning a new digital media easier than ever-- if you know where to begin. This is a clear, simple guide to a few of the basic digital art mediums and how to begin learning them on your own.
Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
Using Adobe Premiere Pro, the viewer will learn how to set up a sequence with proper settings, importing pictures, rendering, and exporting the finished product.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now