从文件中读取,将值插入到整数和字符C++
Reading from file inserting values into ints and chars C++
我想用密钥从外部文件中读取短二进制文件。
3 A 0100 3 E 0101 3 G 0110 3 M 0111 3 N 10103 H 1011 2 S 100 1 T 00 210 2 I 111
3 在名为 POS 的整数中
A 在名为 al 的字符中
0100 在一个名为 bin 等的数组中...
打开文件,逐行读取文件数据,然后从该行中提取所需的内容。
std::string line;
ifstream read;
//open data files
read.open(file_name);
if(read.is_open())
cout << "File ./" << file_name << " is open.n";
else {
cout << "Error opening " << file_name << ".n";
exit(0);
}
while (std::getline(read, line))
{
// line =3 A 0100 3 E 0101 3 G 0110 3 ...
std::istringstream iss (std::move(line));
std::string val_str, al, bin;
while(! iss.str().empty())
{
try{
iss>>val_str;
int val= std::stoi(val_str); //val = 3 in the first run of the while loop
iss >> al; //al = A in the first run of the while loop
iss >> bin
// you can use val, al ,bin
}catch(..){
break;
}
}
}
相关文章:
- 显示基于用户输入的整数的字符
- 尝试将字符串/字符转换为整数会产生意外结果
- 如何使用C++将字符串中的字符转换为整数变量
- 将超出范围的整数分配给有符号字符类型
- 碱基对映射 - 将 argv[i] 移动到整数或字符作为输入
- 当我输入字符类型的数字时,为什么我无法获得整数?
- 输入验证以筛选出字符、字符串和一系列整数
- 字符和整数中 **(ptr+1) 的值差异
- 如何将整数向量值插入字符向量
- 字符到整数数组
- 为什么对无符号字符进行算术运算会将它们提升为有符号整数
- 不能将字符转换为整数吗?
- 如果我们在其中输入一个整数,则字符会给出整数作为输出,但是当分配给它一个整数时,这不会发生。为什么?
- 为什么我们不能将使用异或运算找到的整数转换为字符?
- 有没有办法将字符类型转换为整数?
- 在c++中读取文件时,它如何自动将字符放入字符数组,将数字放入整数变量
- (C/C++)fscanf_s从txt文件以字符形式读取数组时缺少整数参数错误
- 如何从字符数组C++中提取2个整数并将它们存储在2个变量中(初学者)
- 取消随机排列整数/字符数组
- 双倍到无符号的整数/字符