Jump to content

How to Append binary files and compare strings in C++?

- - - - -

  • Please log in to reply
7 replies to this topic

#1
bexita

bexita

    Newbie

  • Members
  • PipPip
  • 15 posts
Hi
I have problem with comparison when input new staff info to binary file:

For exp: I have a info.text containing
name[tab]title[tab]position[tab]phone number [tab] ID

This file already transfered to info.dat.

Now , I want to append this binary file info.dat by adding new staff until
i need to take note that ID , as long as the ID is as same as (case sensitive)
the one in the staff info.dat , it will continue to ask user to input a new unique
ID.


How do I do that ?
Can I use stricm() function to compare ? And How do I compare between user-key-in
to the info inside the binary info.dat?

assuming I have constructed struct Staff{name, tittle...);

Thanks.:pinguin:

#2
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
C or C++? In C you can use strcmp function to compare cstrings, in C++ you have == operator (for std::string).
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#3
bexita

bexita

    Newbie

  • Members
  • PipPip
  • 15 posts
its C++ , can you tell me abit more about == operator , thx.

#4
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
It works the same as comparing, lets say, integers:
std::string s1("test");
std::string s2("test");

if (s1 == s2)
    std::cout << "both strings match";

A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#5
bexita

bexita

    Newbie

  • Members
  • PipPip
  • 15 posts
But how do I compare between user-key-in and the date inside the dat file?

#6
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Ask user to provide data and read data from file, then compare them.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#7
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
So everytime you get new input, you scan the entire binary file for a matching id, if none found you add new staff member, if not you ask them to re-enter the idnum. Is this the approach you must use?
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#8
bexita

bexita

    Newbie

  • Members
  • PipPip
  • 15 posts
Yea, thats what I want , but not the code :crying:


cout << "Email: " ;

cin >> ID_input;

			

			

			

while(strcmp(s.id,ID_input)==0)

{

				

file.read(reinterpret_cast <char*> (&s), sizeof(s));

cout << "Email exists, re-type antother one"<< endl;

cout << "Email : " ;

cin >> ID_input;

strcpy(s.id,ID_input);

				

}





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users