Hello there!

So I have an assignment where I am given a text file with customer information that I need to convert into a "label" format. The original file has caret (^) symbols marking the different lines to be organized.

I have the input totally finished, the only thing I'm stuck on is how to copy the data being stored from the input into a new user-declared text file?

#include<iostream>
#include<fstream>
#include<string>
#include<cstring>

using namespace std;

bool inputData(ifstream& inData);
void outputData(ofstream& outData);

int main()
{  
    ifstream inData;
    ofstream outData;

    inputData(inData);
    inData.close();
    
    outputData(outData);
    
    system("pause");
    return EXIT_SUCCESS;
}
bool inputData(ifstream& inData)
{
    string inFileName;
    bool done = false;
    
    while(!done)
    {
       cout << "Enter input file name: ";
       getline(cin, inFileName);
    
       inData.open(inFileName.c_str());
       
       if(inData.is_open() && inData.good())
       {
            cout << "Data Loaded." << endl;
            done = true;
       }
       
       else
       {
           cout << "Bad File..." << endl;
           inData.clear();
       }
    }
       
   string custName;
   string custAddress;
   string custCity;
   string custState;
   string custZip;
   
    while(!inData.eof())
    {          
       getline(inData, custName, '^');
       getline(inData, custAddress, '^');
       getline(inData, custCity, '^');
       getline(inData, custState, '^');
       getline(inData, custZip);
    }
}

void outputData(ofstream& outData)
{
    string outFileName;
    cout << "\nEnter output file name: ";
    getline(cin, outFileName);
    
    outData.open(outFileName.c_str());
}
    
    //if(outData.is_open())
    //{
        //getline(outData,

Thanks in advance!

Recommended Answers

All 4 Replies

The loop on line 55 is not saving the data in memory. It is just simply overwiring all the data that was read during the previous iteration of that loop with new data. If you want to do anything useful with any of that data you need to save it into a vector of structures, where a structure contains all the data for one line of the file. Once you have that finished you can easily store the contents of the vector to a file in another function.

structure customer
{
   string name;
   string address;
   string city;
   string state;
   string zip;
};

vecotor<customer> custdata;


... <snip>
   customer cust;
   // note that eof() is not needed here.
   while(getline(inData, cust.name, '^'))
    {          
       getline(inData, cust.address, '^');
       getline(inData, cust.city, '^');
       getline(inData, cust.state, '^');
       getline(inData, cust.zip);
       custdata.push_back(cust);
    }

Thanks for the reply!

Is it really not saving into memory?

I mean, I did a test to display all of the input stream data as it would appear in the output text file in the console, and it all showed correctly in the console. Is the vector method really necessary? I'm a little weak when it comes to vectors because I have yet to do much work with them.

Anyways, if it outputs correctly in the console, shouldn't there be a way to make that output into a new text file instead of the console?

Thank you!

>>Anyways, if it outputs correctly in the console, shouldn't there be a way to make that output into a new text file instead of the console?


Your program is trying to read the data in one function and write it back out to another file in another function. The only way that can be done like that is to save all the data in memory when it is read.

The alternative way to do it is to write it back out to a file in the same loop that reads it. If you do that then neither outputData() nor the vector is not needed.

I went with what you said about outputting in the same while loop that reads in and it works perfectly!

Thank you!

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.