简体   繁体   中英

Check whole file if name exists

I tried to parse each data from file to student.name and student.pass to get only the student.name . I was successfully able to get all the student.name , but what I want to happen is after checking each line, then I can compare if all the student.name contains name, or if name exists.

while(fscanf(fp, "%[^:]%*c%[^\n]%*c", &student.name, &student.pass) != EOF)
            {
                printf("Username is %s\n", student.name);
                if(strcmp(student.name, a)==0)
                {
                    cout << "Exists" << endl;
                }
                else
                {
                    cout << "Not Exists" << endl;
                }
            }

What happened in this code is that it checks each line and compares. When I try to put the if else condition after the while condition, it can only check the last data.

How am I able to compare the name to all the student.name from the file?

You could make a and put your data in that.

So the part of your while loop snippet becomes:

#include <vector>
//...

  std::vector<accout> accouts;
  while(...) {
    accouts.push_back(student);
  }
  //process accouts here

If I understand well, you want to put the

    if(strcmp(student.name, a)==0)
        {
            cout << "Exists" << endl;
        }
        else
        {
            cout << "Not Exists" << endl;
        }

outside the while loop. By doing so, in your while loop you will just parse all the entries in the file until you get to the last and then you will exit the while loop. After this, it will compare the last name from the file with the name you want to search. Think about the situation when you iterate through a vector in order to check if a value x is equal to a value from the vector.

You are making your life horribly difficult by the hodge-podge of C/C++ you are mixing together, when C++ streams, and getline (with a delimiter of ':' ) provide all you need.

The example below uses an istringstream to hold sample data to read. Just replace it with fstream to read from a file.

While you generally want to store all data you read from a file, simply declare tmp account struct to read into and a vector of your account struct, to hold all data you .push_back (tmp); into the vector eg

    account tmp;                    /* temp account to read from file */
    ...
    std::vector<account> students;  /* vector of accounts to hold students */

    while (getline(is, tmp.name, ':') &&    /* while name & pass read */
            getline(is, tmp.pass))
        students.push_back (tmp);           /* add to vector students */

Now your vector of accounts students holds all the name and pass information for each student. You now prompt the user for a name to search and simply loop over the stored names, eg

    std::string search;             /* string to hold search */
    ...
    std::cout << "enter name to search: ";  /* get search term */
    if (getline (std::cin, search)) {
        for (auto& s : students)            /* loop over structs */
            if (s.name == search) {         /* comparing names to search */
                std::cout << "found: " << s.name 
                << "  (pass: " << s.pass << ")\n";
                goto namefound; /* if found a quick goto to jump not-found */
            }
        std::cerr << "name not found: " << search << '\n';
  namefound:;
    }

That's done. It loops until it finds a match and then just uses a simple goto to just over the default output that is presented if no match is found. (yes, the goto still have very limited but very valuable uses even today)

Putting it altogether, you could do:

#include <iostream> /* use C++ streams and containers */
#include <sstream>
#include <string>
#include <vector>

struct account {    /* your struct */
    std::string name, pass;
};

int main (void) {

    account tmp;                    /* temp account to read from file */
    std::string search;             /* string to hold search */
    std::vector<account> students;  /* vector of accounts to hold students */
    std::istringstream is ( "Mary Jane:12345\n"     /* data - replace */
                            "John Doe:45678" );     /* with your file */

    while (getline(is, tmp.name, ':') &&    /* while name & pass read */
            getline(is, tmp.pass))
        students.push_back (tmp);           /* add to vector students */

    std::cout << "enter name to search: ";  /* get search term */
    if (getline (std::cin, search)) {
        for (auto& s : students)            /* loop over structs */
            if (s.name == search) {         /* comparing names to search */
                std::cout << "found: " << s.name 
                << "  (pass: " << s.pass << ")\n";
                goto namefound; /* if found a quick goto to jump not-found */
            }
        std::cerr << "name not found: " << search << '\n';
  namefound:;
    }
}

Example Use/Input

$ /bin/studentpass
enter name to search: John Smith
name not found: John Smith

$ ./bin/studentpass
enter name to search: John Doe
found: John Doe  (pass: 45678)

$ ./bin/studentpass
enter name to search: Mary Jane
found: Mary Jane  (pass: 12345)

Look things over and let me know if you have further questions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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