0

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?

7
  • Can you give one example for your requirement?
    – MayurK
    Mar 22, 2019 at 3:41
  • the file you mean? Mar 22, 2019 at 3:43
  • the file based on the input and the format of each input is fprintf(fp, "%s:%s\n", student.name, student.pass); Mar 22, 2019 at 3:44
  • 1
    No. What I understood from your question is, you want to put if..else.. block outside the while loop right? Why do you want to do that? Because this logic looks fine for me.
    – MayurK
    Mar 22, 2019 at 3:44
  • 2
    Since the code you posted is not complete, I can't run it and make suggestions. I don't want to guess on the part that's missing - might be a huge waste of my time. Why not include a complete, verifiable example - eg something I can compile and run?
    – erik258
    Mar 22, 2019 at 3:51

3 Answers 3

0

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
2
  • After parsing, I put all the data to vector then from there, compare the new data to existing data Mar 25, 2019 at 1:59
  • After parsing, I put all the data to vector then from there, compare the new data to existing data Mar 25, 2019 at 1:59
0

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.

0

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 e.g.

    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, e.g.

    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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.