从文件而不是getchar获取字符

时间:2014-01-07 22:32:19

标签: c++

我无法弄清楚如何从文件而不是从getchar()

读取文本

字符串的计算熵

#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();

如何从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); 
}

2 个答案:

答案 0 :(得分:2)

std::getline()的循环读取行读取文件的内容!您实际上可以处理已经读过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];
    }
}

内部循环遍历字符串line中的所有字符。它从当前处理的字符(即unsigned char)中获取*it,因为char可能是签名类型并产生负值,这可能不会很好。 ASCII字符都是正数,但是,例如,我姓名中的u-umlaut ü可能变为负值;我猜这对你的输入来说并不是一个问题,但我更喜欢代码,即使出现意想不到的事情也能正常工作。

在任何情况下,当std::getline()失败时,此循环终止,因为没有其他数据。如果您想再次阅读数据,则需要打开新的std::ifstream或重置您获得的std::ifstream

myfile.clear();                        // clear error flags
myfile.seekg(0, std::ios_base::beg); // move to the start of the file

要将个别字符实际读取为int,您可以使用

mychar = myfile.get();

就个人而言,我倾向于使用迭代器读取字符:

for (std::istreambuf_iterator<char> it(myfile), end; it != end; ++it) {
    char mychar = *it; // well, you could keep using *it, of course
    // ...
}

答案 1 :(得分:0)

使用fgetc()。你可以传递一个FILE指针。

相关问题