Hey guys I am having a problem writing to my text file. I notice when the data is written some of it comes out as gibberish. Take a look and let me know what I am doing wrong. Thank you.
#include <iostream>
#include <string>
#include <fstream>
using std::cin;
using std:: ios;
using std::cout;
using std::endl;
using std::string;
using std::ifstream;
using std::ofstream;
struct product
{
int id;
char name [15];
double price;
double weight;
};
int main ()
{
product items;
ofstream outputFile;
ifstream inputFile;
int count = 0;
outputFile.open ( "Entries.txt" , ios::out|ios::binary|ios::in );
if ( outputFile.is_open () )
{
product items = { 0, "", 0.0, 0.0 };
cout << "Creating file....";
for ( int records = 0; records < 5; records++ )
{
outputFile.write ( reinterpret_cast < const char* > (&items), sizeof (items));
}
cout << "Enter product id:";
cin >> items.id;
while ( items.id > 0 && items.id < 5 )
{
cout << "Enter product name:";
cin >> items.name;
cout << "Enter product price:";
cin >> items.price;
cout << "Enter product weight:";
cin >> items.weight;
outputFile.seekp ( (items.id - 1) * sizeof (items) );
outputFile.write ( reinterpret_cast < const char* > ( &items ), sizeof(items) );
cout << "Enter product id:";
cin >> items.id;
}
outputFile.close ();
cout << "\nFile created.";
/* cout << "Enter product id:";
cin >> items.id;
cout << "Enter product name:";
cin >> items.name;
cout << "Enter product price:";
cin >> items.price;
cout << "Enter product weight:";
cin >> items.weight;
outputFile.write ( reinterpret_cast < const char* > ( &items ), sizeof(items) );
}*/
}
}