Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

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

case sensitive binary string binary file

  • Please log in to reply
7 replies to this topic

#1 bexita

bexita

    CC Newcomer

  • Just Joined
  • PipPip
  • 15 posts

Posted 18 October 2011 - 07:54 AM

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:
  • 0

#2 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts
  • Location:::1
  • Programming Language:C++, Python

Posted 18 October 2011 - 04:52 PM

C or C++? In C you can use strcmp function to compare cstrings, in C++ you have == operator (for std::string).
  • 0

The roots of education are bitter, but the fruit is sweet.


#3 bexita

bexita

    CC Newcomer

  • Just Joined
  • PipPip
  • 15 posts

Posted 18 October 2011 - 04:55 PM

its C++ , can you tell me abit more about == operator , thx.
  • 0

#4 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts
  • Location:::1
  • Programming Language:C++, Python

Posted 18 October 2011 - 05:58 PM

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";

  • 0

The roots of education are bitter, but the fruit is sweet.


#5 bexita

bexita

    CC Newcomer

  • Just Joined
  • PipPip
  • 15 posts

Posted 18 October 2011 - 10:39 PM

But how do I compare between user-key-in and the date inside the dat file?
  • 0

#6 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts
  • Location:::1
  • Programming Language:C++, Python

Posted 19 October 2011 - 04:23 AM

Ask user to provide data and read data from file, then compare them.
  • 0

The roots of education are bitter, but the fruit is sweet.


#7 fread

fread

    Programming God

  • Senior Member
  • PipPipPipPipPipPip
  • 897 posts
  • Location:Trinidad and Tobago
  • Learning:C, Java, C++, C#, PHP, Python, PL/SQL

Posted 19 October 2011 - 09:46 AM

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?
  • 0

#8 bexita

bexita

    CC Newcomer

  • Just Joined
  • PipPip
  • 15 posts

Posted 19 October 2011 - 05:24 PM

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);
				
}

  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download