简体   繁体   English

从文件而不是getchar获取字符

[英]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.

相关问题 每次使用c ++时,使用getchar()获取字符并将它们保存到换行符中 - using getchar() to get characters and save them into file with new line for each time c++ 从类型而不是从变量获取指针级别 - Get pointer level from type rather than from variable 如何获取#define符号而不是来自预定义常量的值 - how to get #define symbol rather than value from predefine constants 从磁盘而不是内存加密和解密文件? - Encrypt and decrypt a file from disk rather than memory? C ++排序字符而不是整个字符串 - C++ Sorting characters rather than entire strings 将类包含在源文件中而不是标题中 - Include Class in Source File Rather Than Header 使用libcurl通过HTTP从内存(而不是磁盘)发送文件 - Sending a file from memory (rather than disk) over HTTP using libcurl 如何使用ifstream的ifstream使用数据长度而不是新行来读取文件? - How to use ifstream of C++ to read from a file using length of the data rather than new line? C++ count() function 从文本文件读取时显示 1 和 0 而不是总计数 - C++ count() function displays 1's and 0's rather than a total count when reading from a text file 如何从 windows 计算机将 c++ 代码构建到 Visual Studio 2019 上的 a.dmg 文件而不是 a.exe 文件? - How to build c++ code into a .dmg file rather than a .exe file on visual studio 2019 from a windows computer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM