[英]get characters from file rather than getchar
I cannot work out how to read text from a file rather than from getchar() 我无法弄清楚如何从文件而不是从getchar()读取文本
calc entropy of a string 一个字符串的计算熵
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include <string>
#include <iostream>
#include <cmath>
#include <fstream>
#include <cstring>
using namespace std;
double log2(double number)
{
return log(number)/std::log(2.);
}
int main() {
unsigned long table[256], total = 0;
double entropy = 0;
char mychar;
string line;
ifstream myfile ("sometext.txt");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
cout << line << '\n';
}
}
short i;
for(i=0; i<256; i++)
table[i] = 0;
while(1)
{
mychar = getchar();
how to read from myfile.txt ? 如何从myfile.txt中读取?
if (mychar==EOF) // ctrl Z
{break;}
table[mychar]++;
}
for(i=33;i<127;i++)
if(table[i]) {
total += table[i];
entropy -= log2(table[i])*table[i];
}
entropy /= total;
entropy += log2(total);
printf("Total Characters: %6d\n",total);
printf("Entropy: %5.6f\n",entropy);
}
The loop reading lines with std::getline()
reads the content of the file! 带有
std::getline()
的循环读取行读取文件的内容! You could actually process the data from the std::string
s already read: 您实际上可以处理已读取的
std::string
的数据:
while (std::getline(myfile, line)) {
std::cout << line << '\n';
for (std::string::const_iterator it(line.begin()), end(line.end()); it != end; ++it) {
unsigned char mychar = *it;
++table[mychar];
}
}
The inner loop iterates over all characters in the string line
. 内循环遍历字符串
line
中的所有字符。 It obtains an unsigned char
from the character currently processed (ie, from *it
) because char
may be signed type and yield negative values which probably doesn't work too well. 它从当前处理的字符(即来自
*it
)中获取unsigned char
,因为char
可能是有符号类型并且产生负值,这可能不能很好地工作。 The ASCII characters are all positive but, eg, the u-umlaut ü
from my name may become a negative values; ASCII字符都是正面的,但,例如,U型变音
ü
从我的名字可能会成为负值; I'd guess that isn't really an issue for your input but I prefer code which works even when unexpected things happen. 我猜这对你的输入来说不是一个真正的问题,但我更喜欢代码,即使出现意想不到的事情也能正常工作。
In any case, this loops terminates when the std::getline()
failed because there is no further data. 在任何情况下,当
std::getline()
失败时,此循环终止,因为没有其他数据。 If you want to read the data again , you'll either need to open a new std::ifstream
or reset the std::ifstream
you got: 如果你想再次读取数据,你需要打开一个新的
std::ifstream
或重置你得到的std::ifstream
:
myfile.clear(); // clear error flags
myfile.seekg(0, std::ios_base::beg); // move to the start of the file
To actually read individual chars as int
you could use 要将各个字符实际读取为
int
您可以使用
mychar = myfile.get();
Personally, I'd be inclined to read the characters using iterators, though: 就个人而言,我倾向于使用迭代器读取字符,但是:
for (std::istreambuf_iterator<char> it(myfile), end; it != end; ++it) {
char mychar = *it; // well, you could keep using *it, of course
// ...
}
Use fgetc(). 使用fgetc()。 You can pass it a FILE pointer.
你可以传递一个FILE指针。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.