繁体   English   中英

C ++从文件中提取数据中的字符串搜索失败

[英]C++ Search for string in data pulled from a file fails

我的代码旨在从文件“ dataset.txt”中提取数据,并且可以。 它能够读取,提取到我的数组中并在命令中显示它。

当我尝试搜索从dataset.txt中提取的数组中的项目时,就会出现问题。 我的代码似乎无法读取它的存在,并且没有显示匹配的结果。

对于任何好奇的人,我所有的数据都是我用来生成虚拟数据的网站上的“虚拟数据”。

#include <cstdlib>
#include <iostream>
#include <string>
#include <algorithm>
#include <iomanip>
#include <fstream>
#include <stdio.h>
#include <vector>

using namespace std;

int main(int argc, char** argv) {

    int ArraySize = 100; // sets array to 100 items

    // initializes all possible arrays in the contact manager
    string LastName[ArraySize] = "no data";
    string FirstName[ArraySize] = "no data";
    string Phone[ArraySize] = "no data";
    string Email[ArraySize] = "no data";
    string NullOne = "";

    // Designed to pull data from the file "dataset.txt"

    int i = 1;

    std::ifstream myfile;
    myfile.open( "dataset.txt", ios::in );

    if(!(myfile.is_open())) {
        cout << "Error Opening File" << endl;
    }
    else {
        cout << "File can be read.  Will pull data." << endl;
    }

    while(myfile.good())
    {
        getline(myfile, NullOne);
        getline(myfile, LastName[i]);
        getline(myfile, FirstName[i]);
        getline(myfile, Phone[i]);
        getline(myfile, Email[i]);

        i++;
    }

    cout << "Data pull complete!" << endl;

    myfile.close();

    // Designed to search all data for a specified name string

    string NameSearch = "null";
    int Matches = 0; // indicates that there is a match at all

    cout << "Search for name" << endl;
    cout << "Please input name : " << endl;
    cout << LastName[4] << endl; // gives me a search parameter to use
    cin >> NameSearch;


    // search for the name in the Name array

    for(int i=0; i<ArraySize; i++) {
        cout << LastName[i] << endl;
       if (LastName[i] == NameSearch) {
           cout << "Name  : " << LastName[i] << endl;
           cout << "Phone : " << Phone[i] << endl << endl;
           Matches++;
       }
   }

    if (Matches == 0) {
        cout << "No matches found" << endl << endl;
    }
    else {
        cout << Matches << " matches found" << endl << endl;
    }

    return 0;
} 

它输出的是:

File can be read.  Will pull data.
Data pull complete!
Search for name
Please input name : 
Chaney
Chaney
no data
Walton
Young
English
Chaney
Carpenter
Castaneda
Potter
Blackwell
Carter
Dyer
Yates
Bentley
Pitts
Dawson
Christensen
Goodwin
Boone
Dunn
Booth
Holman
no data (it displays this for about 40 more lines, I cut it out)
No matches found

它应该找到的匹配项是:

Chaney
Penelope
(518) 996-0514
eget@iaculis.edu

我知道这是一个肮脏的方法,并且我有一个计划要清理它,但是我无法使其正常工作。

看起来非常像您是在从文件读取的字符串的末尾拾起一个回车符\\r

std::getline最多可以读取但不包括行字符\\n的结尾(例如,参见cppreference的第二个条目),但是Windows下的文本文件通常会以两个字符序列\\r\\n结束行。

您可以通过检查您读入的字符串的最后一个字符是否实际上等于\\r来确认这是否是问题。 如果是这样,可以通过使用例如字符串pop_back方法修剪掉最后一个字符来pop_back

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM