Binary File Programming Problem

Basically i want to write user inputs to a binary file using C++ streams. Although these programs compile successfully, it doesn't write a record to my binary file. What could be the problem? I send my codes below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#ifndef PERSON_H
#define PERSON_H

#include <string>
using namespace std;

class Person 
{
public: 
	//accessor functions for personID
	void setID(string id) {	id = ID; }
	string getID() const  {	return ID; }

	//accessor functions for personName
	void setName(string name) { name = Name; }
	string getName() const 	  { return Name; }

	//accessor functions for personDepartment 
	void setDepartment(string department) { department = Department; }
	string getDepartment() const 	      { return Department; }

	Person() //default constructor, set each field to the empty string
	{
	ID = "";
	Name = "";
	Department = "";
	}

	friend istream & operator >> (istream &stream, Person &p);

private:
	 //data members
        string ID;
        string Name;
        string Department;
};

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//Function to prompr user and read fields of a Person
#include <iostream>
#include <cstdlib>
#include <string>
#include "string.h"
#include "Person.h"

istream & operator >> (istream &stream, Person &p)
{	
	string id = p.getID();
	string name = p.getName();
	string department = p.getDepartment();

	//read fields from input 
	cout << "Enter ID of the person: " << flush; 	       getline(stream, id);
	const char * str = id.c_str();  //convert a c string to a const char *
	if (strlen(str) == 0) return stream;
	cout << "Enter Name of the person: " << flush;	       getline(stream, name);
	cout << "Enter Department of the person: " << flush;   getline(stream, department);

	return stream;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//Write Person objects into a stream file
#include <fstream>
#include <string>
#include <iostream>
#include <cstdlib>
#include "Readper.cpp"
using namespace std;

ostream & operator << (ostream & stream, Person &p)
{ //insert fields into file
	stream << p.getID() << p.getName() << p.getDepartment();
	return stream;
}

int main(int argc, char *argv[])
{
	Person p;	
	// If the user didn't provide a filename command line argument, print an error and exit.
	if (argc <= 1)
	{
		cout << "Usage: " << argv[0] << " <Filename>" << endl;
		exit(1);
	}

	char *filename = argv[1];
	ofstream stream(filename, ios::out | ios::binary);
	if (stream.fail())
	{
		cout << "File open failed!" << endl;
	return 0;
	}

	while (1)
	{
		cin >> p;  //read field of person
		const char * str = p.getID().c_str();  //convert a c string to a const char *
		if (strlen(str) == 0) break;
		//write person to output stream
		stream << p; //write field of person
	}
}


Thanks for your help...
inside operator>> id, name and department are copies. Modifying these copies will not affect the member's of p.
You declared operator>> as a friend function, that means it can edit Person's privates (lol, hint hint...)
Thanks for your helps but I couldn't understand what i have to correct on my code? What do you mean Peter87 for "cin >> p" part. How can it be effect real values not copies ? Mathhead200 what do you mean with your hint? :(
You have to update the values inside Person somehow. One way is to use your set functions to update the values.
Yuppy! :)) I did. But this is the %5 of what I have to do! :)) Thanks for your help Peter87...I post the correct codes below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#ifndef PERSON_H
#define PERSON_H

#include <string>
using namespace std;

class Person 
{
public: 
	//accessor functions for personID
	void setID(string id) 
	{	
		int length = id.size();
		ID.assign(id.begin(), id.end());
		//ID[length] = '|'; //append | delimeter to ID
	}
	string getID() const  {	return ID; }

	//accessor functions for personName
	void setName(string name) 
	{ 
		int length = name.size();
		Name.assign(name.begin(), name.end());
		//Name[length] = '|';
	}
	string getName() const 	  { return Name; }

	//accessor functions for personDepartment 
	void setDepartment(string department) 
	{ 
		int length = department.size();
		Department.assign(department.begin(), department.end());
		//Department[length] = '|'; 
	}
	string getDepartment() const 	      { return Department; }
	
	Person() //default constructor, set each field to the empty string
	{
	ID = "";
	Name = "";
	Department = "";
	}

	friend istream & operator >> (istream &stream, Person &p);
	friend ostream & operator << (ostream & stream, Person &p);
private:
	//data members
        string ID;
        string Name;
        string Department;	
};

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//Function to prompr user and read fields of a Person
#include <iostream>
#include <cstdlib>
#include <string>
#include "string.h"
#include "Person.h"

istream & operator >> (istream &stream, Person &p)
{	
	string id = p.getID();
	string name = p.getName();
	string department = p.getDepartment();

	//read fields from input 
	cout << "Enter ID of the person: " << flush; 	      getline(stream,id);  p.setID(id);
	const char * str = id.c_str();  //convert a c string to a const char *
	if (strlen(str) == 0) return stream;
	cout << "Enter Name of the person: " << flush;	      getline(stream, name); p.setName(name);
	cout << "Enter Department of the person: " << flush;  getline(stream, department);  p.setDepartment(department);

	return stream;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//Write Person objects into a stream file
#include <fstream>
#include <string>
#include <iostream>
#include <cstdlib>
#include "Readper.cpp"
using namespace std;

ostream & operator << (ostream & stream, Person &p)
{ //insert fields into file
	stream << p.getID() << p.getName() << p.getDepartment();
	return stream;
}

int main(int argc, char *argv[])
{
	Person p;	
	// If the user didn't provide a filename command line argument, print an error and exit.
	if (argc <= 1)
	{
		cout << "Usage: " << argv[0] << " <Filename>" << endl;
		exit(1);
	}

	char *filename = argv[1];
	ofstream stream(filename, ios::out | ios::binary);
	if (stream.fail())
	{
		cout << "File open failed!" << endl;
	return 0;
	}

	while (p.getID() != "0")
	{
		cin >> p;  //read field of person
		const char * str = p.getID().c_str();  //convert a c string to a const char *
		if (strlen(str) == 0) break;
		//write person to output stream
		stream << p; //write field of person
	}
}


If I have a question, i will use this post again...Thanks...
Last edited on
With this program, I create and write my binary file but when I execute this program, my old binary file replace with a new one! How can I add a new record onto my existing binary file?
pass the ios::app flag to the stream constructor.
Ok. I did this, when i write the correct answer you answered sequentially Peter87 thanks :))
How i can split a string that is entered from standart input in main function? String is like that: "insert 1234|malik|company"
I have to split "insert" part and after space "1234|malik|company" remaining part. A pseudocode like:
1
2
3
4
5
6
7
8
9
10
11
while (1)
{
cin >> input;
commandpart string = split1(input);
recordpart string = split2(input);
if (commandpart == "insert")  insert(recordpart);
.
.
.
if (commandpart == "quit") break;
} 

Thanks for your helps...
Last edited on
Topic archived. No new replies allowed.