Reading Binary File

Ok, So i'm trying to read a file and I think its in Binary and I have to add 65 to each character. Am I on the write track, or is something other than binary.

Here is the text in the data file
.-&1 34+ 3(.-2ß(-ß#$"18/3(-&ß+$5$+ß.-$íß'$ß-$73ß.-$ß(2ß ß+(33+$ß,.1$ß"' ++$-&(-&íß,$22 &$í373ß(2ß ß3$73ß%(+$ëß!43ß$ "'ß+$33$1ß(2ß/1(-3$#ß(-ß(32ß 2"((ß-4,$1(" +ß%.1,íß$ #ß(-ß$ "'ß-4,!$1ß -#ß38/$ß" 23ß(3ß 2ß ß"' 1 "3$1íß'$ß1$24+3ß6(++ß!1(-&ß8.4ß3.ß+$5$+ß36.àɾ

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
32
33
34
35
36
37
38
39
40
 #include <iostream>
#include <ostream>
#include <fstream>
#include <Windows.h>
#include <string>

using namespace std;

int main()
{
	ifstream inFile;
	
	
	char cypherIn[500];

	inFile.open("cypher.dat", ios::in | ios::binary+65);

	if(inFile.fail())
		{
			cout << "File did not open!";
			Sleep(2000);
			exit(1);
		}

	else
		{

			for(int i=0;i<500;i++)
			{
				inFile.read((char*) &cypherIn[i],sizeof(cypherIn[i]));
			}
		
		}
	for(int f=0;f<500;f++)
	{
		cout << cypherIn[f];
	}
	
	return 0;
}
move +65 from line 16 to line 36

instead of using the loop on line 28 you can simply write:

inFile.read(cypherIn,sizeof(cypherIn));

if it's not guaranteed that the inFile contain 500 characters you better use gcount() (instead of 500) for the loop on line 34

http://www.cplusplus.com/reference/istream/istream/gcount/?kw=gcount
1
2
3
4
5
    ifstream inFile("cypher.dat", ios::binary);
    
    char ch;
    while (inFile.get(ch))
        cout << char (ch+65);
Thank you very much. I tried this and it worked but it just gives me numbers. Is it something other than binary?




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
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <ostream>
#include <fstream>
#include <Windows.h>
#include <string>

using namespace std;

int main()
{
	ifstream inFile;
	
	
	char cypherIn[500];

	inFile.open("cypher.dat", ios::in | ios::binary);

	if(inFile.fail())
		{
			cout << "File did not open!";
			Sleep(2000);
			exit(1);
		}

	else
		{

			for(int i=0;i<261;i++)
			{
				//inFile.read((char*) &cypherIn[i],sizeof(cypherIn[i]));
				inFile.read(cypherIn,sizeof(cypherIn));
			}
		
		}
	for(int f=0;f<500;f++)
	{
		cout << hex << cypherIn[f]+65;
	}
	
	return 0;
line 37
cout << hex << cypherIn[f]+65;
you are printing out an integer value in hexadecimal format.
What you should be doing is printing the character corresponding to that integer.
You do that by casting it from one type to another
examples:
1
2
3
4
5
    cout << (char) 65;
    cout << char(66);
    cout << static_cast<char>(67);
    char c = 68;  // Assign value to character variable
    cout << c;

Output:
ABCD


That's what I did at line 5 in my previous example:
 
    cout << char (ch+65);

(also notice you don't need an array, you can just read in one character at a time and output the converted result).

Also, line 31
 
    inFile.read(cypherIn,sizeof(cypherIn));

That is attempting to read 500 characters from the file. You certainly don't need to attempt to do that 261 times. Just once would probably be more than enough. :)
Last edited on
Awesome. Thank you so much. I changed my last part of the code to this and it worked.


1
2
3

cout <<  char(cypherIn[f]+65);
Topic archived. No new replies allowed.