如何使用数组作为搜索词来搜索文件
How to search a file using an array as search term
我正在尝试解析一个 csv 文件并搜索它以查找与我随机生成的数组匹配的值。
我尝试了一些方法但我迷路了,我需要访问整个文件,搜索这些值并输出它所属的索引列。
我的随机数组生成器代码以及我的搜索 csv 函数:
编辑:
编辑代码以使其可重现,添加评论,希望这符合标准(显然除了给定的 csv 文件)。
可重现的例子:
#include <fstream>
#include <iostream>
#include <string>
#include <random>
using namespace std;
//fills array of size 6 with random numbers between 1-50
void random_array(int arr[]){
for(int i=0;i<6;i++)
{
int num = rand()%51;
arr[i]=num;
}
}
int main(){
string line;
fstream file;
int size = 6;
int numbers[size];
int found;
//gets random array from random_array func
srand(time(NULL));
random_array(numbers);
//opens file to search
file.open("CSV directory here");
if(file.is_open())
{
//print random array values
for(int i = 0; i < size; i++){
cout << numbers[i]<< " ";}
cout << endl;
//get file contents and search if contents of array exist in file
while(getline(file, line)){
for(int i = 0; i < size; i++){
found = line.find(std::to_string(numbers[i]));
if(found != string::npos) {
//print found numbers
cout << found << " ";
}
}
}
}
return 0;
}
CSV 格式:
,Date,1,2,3,4,5,6,7
0,Wed 03 January 2001,5,6,16,17,19,22,40
1,Sat 06 January 2001,6,16,23,34,35,40,37
我的输出:
生成的数组:
35 35 38 37 16 31
找到的号码:
26 33 33 39 24 31 34 33 24 6 28 31 33 33 30 0 32 34 33 30 27 38 26 29
29 33 7 24 29 26 26 26 24 24 0 30 30 36 0 0 23 27 0 0 7 36 36 27 30 30
27 27 26 26 32 29 23 38 32 32 28 28 7 25 29 37 25 29 29 26 23 34 31 28
25 31 28 28 34 32 32 35 38 40 25 37 37 35 40 40 30 30 42 42 42 36 35
28 31 25 37 7 27 36 33 36 33 29 39 29 35 34 34 40 40 43 31
我建议您更详细地查看 find
函数。 None 它的重载接受一个整数,最近的匹配(以及将被使用的)接受一个 char
...所以它不会寻找像“12”这样的字符串寻找值为 12 的字符。
您需要先将数字转换为字符串。一个简单的修改是:
for ( int i = 0; i < 6; i++ ) {
if( (offset = line.find( std::to_string( numbers[i] ) ) ) != string::npos ) {
//do something
}
}
我正在尝试解析一个 csv 文件并搜索它以查找与我随机生成的数组匹配的值。
我尝试了一些方法但我迷路了,我需要访问整个文件,搜索这些值并输出它所属的索引列。
我的随机数组生成器代码以及我的搜索 csv 函数:
编辑:
编辑代码以使其可重现,添加评论,希望这符合标准(显然除了给定的 csv 文件)。
可重现的例子:
#include <fstream>
#include <iostream>
#include <string>
#include <random>
using namespace std;
//fills array of size 6 with random numbers between 1-50
void random_array(int arr[]){
for(int i=0;i<6;i++)
{
int num = rand()%51;
arr[i]=num;
}
}
int main(){
string line;
fstream file;
int size = 6;
int numbers[size];
int found;
//gets random array from random_array func
srand(time(NULL));
random_array(numbers);
//opens file to search
file.open("CSV directory here");
if(file.is_open())
{
//print random array values
for(int i = 0; i < size; i++){
cout << numbers[i]<< " ";}
cout << endl;
//get file contents and search if contents of array exist in file
while(getline(file, line)){
for(int i = 0; i < size; i++){
found = line.find(std::to_string(numbers[i]));
if(found != string::npos) {
//print found numbers
cout << found << " ";
}
}
}
}
return 0;
}
CSV 格式:
,Date,1,2,3,4,5,6,7
0,Wed 03 January 2001,5,6,16,17,19,22,40
1,Sat 06 January 2001,6,16,23,34,35,40,37
我的输出:
生成的数组:
35 35 38 37 16 31
找到的号码:
26 33 33 39 24 31 34 33 24 6 28 31 33 33 30 0 32 34 33 30 27 38 26 29 29 33 7 24 29 26 26 26 24 24 0 30 30 36 0 0 23 27 0 0 7 36 36 27 30 30 27 27 26 26 32 29 23 38 32 32 28 28 7 25 29 37 25 29 29 26 23 34 31 28 25 31 28 28 34 32 32 35 38 40 25 37 37 35 40 40 30 30 42 42 42 36 35 28 31 25 37 7 27 36 33 36 33 29 39 29 35 34 34 40 40 43 31
我建议您更详细地查看 find
函数。 None 它的重载接受一个整数,最近的匹配(以及将被使用的)接受一个 char
...所以它不会寻找像“12”这样的字符串寻找值为 12 的字符。
您需要先将数字转换为字符串。一个简单的修改是:
for ( int i = 0; i < 6; i++ ) {
if( (offset = line.find( std::to_string( numbers[i] ) ) ) != string::npos ) {
//do something
}
}