[英]Reading from file inserting values into ints and chars C++
I wanted to read the short binaries from an external file with the key.我想用密钥从外部文件中读取简短的二进制文件。
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 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 is in an int called pos 3 在一个名为 pos 的 int 中
A is in a char called al A 在一个叫做 al 的字符中
0100 is in an array called bin etc... 0100 位于名为 bin 等的数组中...
Open your file, read the file data line by line and extract what you need from the line.打开您的文件,逐行读取文件数据并从该行中提取您需要的内容。
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;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.