C++,按数字读取文件行
C++ , reading file lines by numbers
我想制作按数字读取文件的控制台项目。示例:按数字 1 2 查找,它仅在包含这些数字的文件夹中的控制台文本行中打印
bibi ceki 1 2
hasesh cekiii 1 3
krki cko 1 2
在这种情况下,它只会打印出 "bibi ceki" 和 "krki cko"。
在我的代码中有很多遗漏的东西。我没有检查数字是否正确的循环,但这是我能做的最好的和我尝试过的:
#include <fstream>
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main() {
char str1[10], str2[10];
int raz, ode;
ifstream infile("file.txt");
while (infile.good()) {
fscanf(infile, "%s %s %d %d", str1, str2, &raz, &ode); //this thing cant be used lik this
while(raz==1 && ode==2) {
string sLine;
getline(infile, sLine);
cout << sLine << endl;
}
}
infile.close();
return 0;
}
如您所见,带有 fscanf 的行无法正常工作,我不知道该怎么做。
如果有更好的方法,我需要一些帮助和建议,请尽可能具体,我是 c++/c 的新手。
您正在混合使用 C fscanf 函数和 C++ ifstream。
我建议使用 C++,在这种情况下,您可以像这样使用运算符>>:
std::string str1, str2 ;
...
//substitute the following line with the one below
//fscanf(infile, "%s %s %d %d", str1, str2,&raz,&ode);
infile >> str1 >> str2 >> raz >> ode ;
您可以使用 std::getline
逐行阅读,直到代码匹配,当匹配时,您可以 push_back
将名称 std::vector
。您还可以使用定界符构建文件,例如 '|'
,这样您就可以一直读到该字符作为名称,其余字符作为代码。示例文件为:
bibi ceki|1 2
hasesh cekiii|1 3
krki cko|1 2
以下是您如何实现该目标的示例:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
int main()
{
ifstream in_file("file.txt", ios::in);
vector<string> vec;
string names; // store names like: "bibi ceki"
string codes; // store codes like: "1 2"
while (getline(in_file, names, '|')) {
getline(in_file, codes, '\n');
if (codes == "1 2")
vec.push_back(names);
}
for (unsigned int i = 0; i != vec.size(); ++i)
cout << vec[i] << endl;
in_file.close();
}
输出:
bibi ceki
krki cko
我想制作按数字读取文件的控制台项目。示例:按数字 1 2 查找,它仅在包含这些数字的文件夹中的控制台文本行中打印
bibi ceki 1 2
hasesh cekiii 1 3
krki cko 1 2
在这种情况下,它只会打印出 "bibi ceki" 和 "krki cko"。 在我的代码中有很多遗漏的东西。我没有检查数字是否正确的循环,但这是我能做的最好的和我尝试过的:
#include <fstream>
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main() {
char str1[10], str2[10];
int raz, ode;
ifstream infile("file.txt");
while (infile.good()) {
fscanf(infile, "%s %s %d %d", str1, str2, &raz, &ode); //this thing cant be used lik this
while(raz==1 && ode==2) {
string sLine;
getline(infile, sLine);
cout << sLine << endl;
}
}
infile.close();
return 0;
}
如您所见,带有 fscanf 的行无法正常工作,我不知道该怎么做。
如果有更好的方法,我需要一些帮助和建议,请尽可能具体,我是 c++/c 的新手。
您正在混合使用 C fscanf 函数和 C++ ifstream。
我建议使用 C++,在这种情况下,您可以像这样使用运算符>>:
std::string str1, str2 ;
...
//substitute the following line with the one below
//fscanf(infile, "%s %s %d %d", str1, str2,&raz,&ode);
infile >> str1 >> str2 >> raz >> ode ;
您可以使用 std::getline
逐行阅读,直到代码匹配,当匹配时,您可以 push_back
将名称 std::vector
。您还可以使用定界符构建文件,例如 '|'
,这样您就可以一直读到该字符作为名称,其余字符作为代码。示例文件为:
bibi ceki|1 2
hasesh cekiii|1 3
krki cko|1 2
以下是您如何实现该目标的示例:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
int main()
{
ifstream in_file("file.txt", ios::in);
vector<string> vec;
string names; // store names like: "bibi ceki"
string codes; // store codes like: "1 2"
while (getline(in_file, names, '|')) {
getline(in_file, codes, '\n');
if (codes == "1 2")
vec.push_back(names);
}
for (unsigned int i = 0; i != vec.size(); ++i)
cout << vec[i] << endl;
in_file.close();
}
输出:
bibi ceki
krki cko