C++ 如何使用get()读取文本文件?

C++ 如何使用get()读取文本文件?,c++,file,get,C++,File,Get,我有一个文本文件: a100011 b010100 c001100 我想把它写在这张地图上: map<char, vector<bool>> dict; 不要使用get() 根据您的示例输入,您的记录由换行符分隔,即每行文本一条记录。因此,逐行阅读(效率更高) std::字符串文本\u行; 地图数据库; while(std::getline(字典、文本行)) { const char key=text_行[0]; 文本线擦除(0,1); 数据库[键]=文本行; }

我有一个文本文件:

a100011
b010100
c001100
我想把它写在这张地图上:

map<char, vector<bool>> dict;
不要使用
get()

根据您的示例输入,您的记录由换行符分隔,即每行文本一条记录。因此,逐行阅读(效率更高)

std::字符串文本\u行;
地图数据库;
while(std::getline(字典、文本行))
{
const char key=text_行[0];
文本线擦除(0,1);
数据库[键]=文本行;
}

这是一个例子。还有更多的
std::string
方法可用于将键与从文件读取的字符串中的值分开。

由于您处理的是基于行的数据,因此应使用
std::getline()
读取每一行。您可以根据需要使用
std::istringstream
解析每一行,例如:

#包括
#包括
#包括
#包括
#包括
std::map dict;
std::ifstream字典(“file.txt”);
std::字符串行;
while(std::getline(字典,行))
{
标准::istringstream iss(线);
std::vec;
字符键,c;
iss>>键;
而(iss>>c)向量推回(c=='1');
dict[key]=std::move(vec);
}

如果出于某种原因必须使用get,下面有一个解决方案。您可以看到,它比Remy Lebeau的解决方案(我曾将其用作我的起点)要详细得多。就我个人而言,我更喜欢我的支票!='0'vice==1,就好像有任何其他数字一样,通常0为假,所有其他数字为真,但更可能是一种错误情况,您希望捕获并引起用户的注意。我投票支持雷米的解决方案,它更短,更容易理解,更容易维护。我确实在输入中添加了一个跳过的空格检查,您需要添加额外的逻辑来处理您可能要跳过的任何其他字符,或者根据这些字符标记错误。根据您可能希望使用小于和/或大于测试范围的数量,以及您可能希望使用switch语句

#include <fstream>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

int main() {
    istream &inFile = cin;
    map<char,std::vector<bool>> dict;
    while(inFile.eof() == false){
        char key = inFile.get();
        if(key == '\n' || key == 0) continue;
        vector<bool> inefficientBitfield;
        char newChar = 'a';
        while(inFile.eof() == false){
            newChar = inFile.get();
            if(newChar == '\n' || newChar == 0) break;
            if(newChar == ' ') continue;
            inefficientBitfield.push_back(!(newChar == '0'));
        }
        dict[key] = move(inefficientBitfield);
            
    }
    
        for(auto &p : dict)
    {
        std::cout << p.first << " =";
        for(auto b : p.second) {
            std::cout << ' ' << std::boolalpha << b;
        }
        std::cout << std::endl;
    }
    
    return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main(){
istream&infle=cin;
地图编辑;
while(infle.eof()==false){
char key=infle.get();
如果(键=='\n'| |键==0)继续;
矢量位场;
char newChar='a';
while(infle.eof()==false){
newChar=infle.get();
如果(newChar=='\n'| | newChar==0)中断;
如果(newChar='')继续;
无效位字段。向后推(!(newChar==“0”);
}
dict[key]=移动(无效位字段);
}
用于(自动和p:dict)
{

std::cout为什么你必须使用get?当你说使用get很重要时,它必须是get的那个版本吗?文本文件中的行都是相同的长度吗?@davidodford代码有不同的长度。有时可能会有空格或换行符而不是字符,所以简单的循环“for”(string str;Dictionary>>str;)“无法捕捉简化生活的符号。请使用
std::getline
将文本行读入
std::string
。然后处理该字符串。
std::string text_line;
std::map<char, std::string> database;
while (std::getline(Dictionary, text_line))
{
    const char key = text_line[0];
    text_line.erase(0,1);
    database[key] = text_line;
}
#include <fstream>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

int main() {
    istream &inFile = cin;
    map<char,std::vector<bool>> dict;
    while(inFile.eof() == false){
        char key = inFile.get();
        if(key == '\n' || key == 0) continue;
        vector<bool> inefficientBitfield;
        char newChar = 'a';
        while(inFile.eof() == false){
            newChar = inFile.get();
            if(newChar == '\n' || newChar == 0) break;
            if(newChar == ' ') continue;
            inefficientBitfield.push_back(!(newChar == '0'));
        }
        dict[key] = move(inefficientBitfield);
            
    }
    
        for(auto &p : dict)
    {
        std::cout << p.first << " =";
        for(auto b : p.second) {
            std::cout << ' ' << std::boolalpha << b;
        }
        std::cout << std::endl;
    }
    
    return 0;
}