Performing binary operations on a file - get()

Hi there,

I copied this code out from one of my c++ books from the chapter concerning file I/O, and, specifically, binary operations on a file. I found this section not to be very well explained and I'm not sure what this code is meant to do. I'm guessing you need to feed main() a text file from the same directory you have your code saved in and the code reads the file as binary and outputs the result as characters. But, as it is main() does not take any file as a parameter and when I run it it just outputs: Usage: PR <filename>

So what is this code supposed to do? Thanks in advance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <fstream>
using namespace std;

int main(int argc, char *argv[]) 
{
	char ch;
	
	if(argc!=2)
	{
		cout << "Usage: PR <filename>\n";
		return 1;
	}
	
	ifstream in(argv[1], ios::in | ios::binary);
	
	if(!in)
	{
		cout << "Can't open file!\n";
		return 1;
	}
	
	while(in)
	{
		in.get(ch);
		if(in) cout << ch;
	}
	
	in.close();
	return 0;
}
That code expects you to run the program from the command line prompt, and expects the filename to be supplied as a parameter. Say your program is called test1.exe and the input file is called myfile.txt. You would type on the command line:
test1.exe myfile.txt

If you run the program from within an IDE, there should be a menu option where you can type in the parameter value (that is, the name of the file). Usually to be sure the file will be found, you would specify the full path to the file, for example "C:\myfiles\myfile.txt"

Alternatively, you could type the full file name in the program itself, like this, (note each '\' has to be typed as a double '\\'.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <fstream>
using namespace std;

int main() 
{
    char filename[] = "C:\\myfiles\\myfile.txt";  // full filename and path to file

    
    ifstream in(filename, ios::in | ios::binary);
    
    if(!in)
    {
        cout << "Can't open file" << filename << endl;
        return 1;
    }
    
    char ch;
    
    while (in.get(ch))
    {
        cout << ch;
    }
    
  
    return 0;
}
Last edited on
Many thanks fr your reply, so far I have tried entering the command line prompt, and entering the parameter value into the IDE, it worked and I was able to read form the file, but a short text file saying:

This
is
a
test
:)

Gave this output

{\rtf1\ansi\ansicpg1252\cocoartf1138\cocoasubrtf510
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
\paperw11900\paperh16840\margl1440\margr1440\vieww10800\viewh8400\viewkind0
\pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\pardirnatural

\f0\fs24 \cf0 This \
is \
a \
test \
:) }

using both methods. Do I need to save the file as a format other than .rtf or .txt in order to output only what I wrote in the file?
You need to save the file as plain text. It looks like you have a RTF document there which includes all sorts of extra formatting details (font name, font size etc.).
Yes that worked, thanks a lot
Topic archived. No new replies allowed.