So, I was writing a program for static cryptanalysis, when faced the unusual behavior.
First, I wrote the counter of characters, and that's where the problem appears.
I've got the file:
//alphabet.txt
abcdefghijklmnopqrstuvwxyz
and when I tried to count letters frequency, got some interesting result! (Don't pay attention to first line, it just says that ifstream is open)
D:\Workspaces\EclipseWS\StaticAnalysis\src>g++ main.cpp
D:\Workspaces\EclipseWS\StaticAnalysis\src>a.exe
1!
a 1
b 1
c 1
d 1
e 1
f 1
g 1
h 1
i 1
j 1
k 1
l 1
m 1
n 1
o 1
p 1
q 1
r 1
s 1
t 1
u 1
v 1
w 1
x 1
y 1
z 2
As you can see, program says that 'z' appears 2 times in the text, but it doesn't.
Now, to technical details.
Operating System: Windows 10 Enterprise LTSC
System Type: 64bit
C++ compiler: MinGW
alphabet.txt File encoding: ANSI
Code, I parsed the file with:
#include <fstream>
#include <iostream>
#include <map>
#include <string>
using namespace std;
#define encrypted_fname "D:\\Workspaces\\EclipseWS\\StaticAnalysis\\src\\alphabet.txt"
void printMap(const map<char,int>& m){
for (const auto& p : m){
cout<<p.first<<'\t'<<p.second<<endl;
}
}
int main(){
ifstream ifs(encrypted_fname);
cout << ifs.is_open() << "!\n";
map<char,int> letterCount;
char buffer;
while(ifs){
ifs >> buffer;
letterCount[buffer]+=1;
}
printMap(letterCount);
}
I tried to change file encoding to UTF-8
1!
» 1
¿ 1
ï 1
a 1
b 1
c 1
d 1
e 1
f 1
g 1
h 1
i 1
j 1
k 1
l 1
m 1
n 1
o 1
p 1
q 1
r 1
s 1
t 1
u 1
v 1
w 1
x 1
y 1
z 2
..Unicode big endian\unicode..
1!
þ 1
ÿ 1
28
a 1
b 1
c 1
d 1
e 1
f 1
g 1
h 1
i 1
j 1
k 1
l 1
m 1
n 1
o 1
p 1
q 1
r 1
s 1
t 1
u 1
v 1
w 1
x 1
y 1
z 1
But as you can see, output is a bit lame in every case!
I can provide more information if you need, just say me how to get it.
My main questions are: why does it happen? Do I have a mistake in the code? How to fix it?