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

}




}

My output looks like this in the text file. Date Ï@ 4ÿ" K@ €E@

This is the code.

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

   // cout << sizeof(items);
    outputFile.open ( "Entries.txt" , ios::out|ios::in );

    if ( outputFile.is_open () )
    {

        //product items = { 0, "", 0.0, 0.0 };

        cout << "Creating file....";


        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), ios::beg );

        outputFile.write ( (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) );
    }*/

}




}

The output looks like this:

Date Ï@ 4ÿ" K@ €E@

Looks to me it's just garbage in the name string after the \0. Before inputting the name you can zero or SPACE out the string.

Looks to me it's just garbage in the name string after the \0. Before inputting the name you can zero or SPACE out the string.

What do you mean by zeroing out or spacing the string input. Can you give me an example.

Hey there. I have a new problem with my code ( File Processing ). The message that I am getting is: H:\C++ Files\Header_Files\RandomFiles.o:RandomFiles.cpp:(.text+0x413)||undefined reference to `displayRecord(Products)'|
||=== Build finished: 1 errors, 0 warnings ===|.

Can someone please tell me what is going on here. All help will be gladly appreciated.


This is the code:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using std::ios;
using std::cin;
using std::cout;
using std::endl;
using std::setw;
using std::ostream;
using std::fstream;
using std::ifstream;
using std::ofstream;
using std::getline;
using std::string;

struct Products
{
    int productId ;
    char productName [20] ;
    char productPrice[5] ;
    char productWeight[3] ;
};


void menu ();
bool addRecord ( Products );
void displayRecord ( Products );

int main ()
{
    Products items;
    int choice = 0;
    char option = ' ';
    ifstream inputFile;


    do
    {
        system ("cls");
        menu ();
        cout << "PLEASE SELECT OPTION:";
        cin >> choice;

        switch ( choice )
        {
            case  1: system ("cls");
                     cout << "ENTER DATA BELOW:\n";

                     cout << "\n1:Enter product ID Number: ";
                     cin >> items.productId;
                     cin.ignore (200,'\n');

                     cout << "\n3:Enter product's name: ";
                     cin >> items.productName;
                     cin.ignore (200,'\n');

                     cout << "\n4:Enter product's price: ";
                     cin >> items.productPrice;
                     cin.ignore (200,'\n');

                     cout << "\n5:Enter product's weight: ";
                     cin >> items.productWeight;

                    // checks for bool type stating if memory is available
                     if ( addRecord ( items ) )
                     {
                          system ("cls");
                          cout << "\nYOUR ENTRY WAS ADDED SUCCESSFULLY!\n";
                          system ("pause");
                          system ("cls");
                          menu ();
                     }
                     else
                    {
                       cout << "OPPS THERE WAS A PROBLEM CREATING THE FILE.";
                    }
                    break;

            case 2: cout << "This is the entry you requested.";
                    displayRecord ( items );
                    break;


            default : system ("cls");
                      cout << "THERE IS NO SUCH OPTION AVIALIBLE.\n";
                      system ("pause");
                      system ("cls");
                      menu ();



        }
        cout << "\nDO YOU WANT TO PERFORM A NEXT ACTION (Y=YES / N=NO)?";
        cin >> option;
        option = toupper ( option );

    }while (option == 'Y');

    return 0;
}


void menu ()
{
    for ( int count = 0; count < 80; count++ )
    cout << "*";

    cout << setw(140) << "!!!FILE PROCESSING PROGRAM!!!\n\n";

    for ( int count = 0; count < 80; count++ )
    cout << "*";

    cout << "\n\nOPTION 1: To add a new record.\n\n"
         << "OPTION 2: To veiw record(s).\n\n"
         << "OPTION 3: View list entries.\n\n"
         << "OPTION 4: EXIT\n\n";

    for ( int count = 0; count < 80; count++ )
    cout << "*";
}// End of mainMenu function


bool addRecord (  Products records )
{
     ofstream outputFile;

     outputFile.open ( "Data.txt", ios::out|ios::binary );

    if ( outputFile.is_open () )
    {
        Products items = {0, "", "", ""};

        for ( int entries = 0; entries < 5; entries++ )
        {
            outputFile.write ( reinterpret_cast <const char*> (&items), sizeof (items) );
            cout << sizeof (items);
        }

        outputFile.seekp ( (records.productId - 1 ) * sizeof (records) );
        outputFile.write ( reinterpret_cast <const char*> (&records), sizeof(Products) );
        outputFile.close ();
        return true;
    }
    else
    {
        return false;
    }

}



void viewRecord ( Products records )
{
     ifstream inputFile;

     inputFile.open ( "Data.txt",ios::in|ios::binary );

    if ( inputFile.is_open () )
    {
        int account = 0;
        //Products items;


        cout << "Enter account that you want to view:";
        cin >> account;


        //outputFile.open ( "Data.txt", ios::out|ios::app|ios::binary );
        inputFile.seekg ( (account - 1) * sizeof (records) );
        inputFile.read ( reinterpret_cast < char*> (&records), sizeof(records) );

        cout << records.productId << endl
             << records.productName << endl
             << records.productPrice << endl
             << records.productWeight;

        inputFile.close ();
    }

}

This is the code.

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

why do all these header files are included?

A using statement is not a header include.

In this case, they are used in place of using namespace std; to only make certain parts of the std namespace visible instead of the whole thing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.