从文件中读取插入值到整数和字符 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 1010 3 H 1011 2 S 100 1 T 00 2
10 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;
}
}
}
我想使用密钥从外部文件读取短二进制文件。
3 A 0100 3 E 0101 3 G 0110 3 M 0111 3 N 1010 3 H 1011 2 S 100 1 T 00 2 10 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;
}
}
}