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:
//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);
constchar * 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;
}
//Write Person objects into a stream file
#include <fstream>
#include <string>
#include <iostream>
#include <cstdlib>
#include "Readper.cpp"
usingnamespace 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
constchar * 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 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? :(
//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);
constchar * 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;
}
//Write Person objects into a stream file
#include <fstream>
#include <string>
#include <iostream>
#include <cstdlib>
#include "Readper.cpp"
usingnamespace 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
constchar * 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...
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?
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;
}