cuốn sách gpt4 ai đã làm

c++ - 从 .tsv 文件中读入

In lại Tác giả: Walker 123 更新时间:2023-11-28 01:15:03 26 4
mua khóa gpt4 Nike

我正在尝试从格式为制表符分隔值的文件中读取信息:

  

ví dụ:

Seaking 119 Azumao
Mr. Mime 122 Barrierd
Weedle 13 Beedle

目前我是这样做的:

string americanName;
int pokedexNumber;
string japaneseName;

inFile >> americanName;
inFile >> pokedexNumber
inFile >> japaneseName;

我的问题源于“Mime 先生”中的空格,因为字符串可以包含空格。

我想知道如何正确读取文件。

câu trả lời hay nhất

标准库使用诸如 locales 之类的东西根据您的系统区域设置确定不同符号的类别和其他与区域设置相关的事物。由于各种 unicode 问题,标准流使用它来确定什么是空格。

您可以使用这个事实来控制您的情况下 ' ' 的含义:

#include 
#include
#include

struct tsv_ws : std::ctype
{
mask t[table_size]; // classification table, stores category for each character

tsv_ws() : ctype(t) // ctype will use our table to check character type
{
// copy all default values to our table;
std::copy_n(classic_table(), table_size, t);
// here we tell, that ' ' is a punctuation, but not a space :)
t[' '] = punct;
}
};

int chính() {
std::string s;
std::cin.imbue(std::locale(std::cin.getloc(), new tsv_ws)); // using our locale, will work for any stream
while (std::cin >> s) {
std::cout << "read: '" << s << "'\n";
}
}

这里我们将 ' ' 设为标点符号,而不是空格符号,因此流不再将其视为分隔符。确切的类别并不重要,但不能是 space.

这是相当强大的技术。例如,您可以将 ',' 重新定义为要在 CSV 中读取的空格格式。

关于c++ - 从 .tsv 文件中读入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58974225/

26 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress