I have created .txt file and this program should read my .txt file and exports t
ID: 3569189 • Letter: I
Question
I have created .txt file and this program should read my .txt file and exports to another file. Somehow is not work. Can someone help
I Finish most of my program but I can't figure out how to write bool extractData
#include
#include
#include
#include
using namespace std;
?
bool loadArrays(const char fileName[], long idArray[], int storeArray[], int qtyArray[], int & count, int maxCells);
void printArrays(ostream & w, const long idArray[], const int storeArray[], const int qtyArray[], int count);
bool extractData(const char newFileName[], long requestId, int baseQty, const long idArray[], const int storeArray[], const int qtyArray[], int count, int & newCount);
void main()
{
const int maxCells = 50;
char NewFileName;
char fileName[50];
int requestId;
const int ORDER_VALUE = 500;
long idArray[maxCells];
int storeArray[maxCells];
int qtyArray[maxCells];
int count = 0;
int newCount = 0;
bool result;
result = loadArrays(fileName, idArray, storeArray, qtyArray, count, maxCells);
if (result == false)
{
if (count == 0)
{
cerr << "Couldn't open the input file" << endl;
exit(1);
}
else
{
cerr << "couldn't load all the data from the file" << fileName;
exit(2);
}
}
printArrays(cout, idArray, storeArray, qtyArray, count);
cout << "Enter a product ID" << endl;
cin >> requestId;
}
?
?
bool loadArrays(const char fileName[], long idArray[], int storeArray[], int qtyArray[], int & count, int maxCells)
{
bool ok = true;
count = 0;
ifstream infile;
infile.open(fileName);
if (!infile)
ok = false;
else
{
int i = 0;
while (i < maxCells && infile >> idArray[i] >> storeArray[i] >> qtyArray[i])
{
i++;
}
count = i;
if (!infile.eof())
ok = false;
infile.close();
}
return ok;
}
void printArrays(ostream & w, const long idArray[], const int storeArray[], const int qtyArray[], int count)
{
for (int i = 0; i <= count; i++)
{
w << idArray[i] << " " << storeArray[i] << " " << qtyArray[i] << endl;
}
}
bool extractData(const char newFileName[], long requestId, int baseQty, const long idArray[], const int storeArray[], const int qtyArray[], int count, int & newCount)
{
}
Explanation / Answer
C++ Program:
#include "stdafx.h"
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
bool loadArrays(const char fileName[], long idArray[], int storeArray[], int qtyArray[], int & count, int maxCells);
void printArrays(ostream & w, const long idArray[], const int storeArray[], const int qtyArray[], int count);
bool extractData(const char newFileName[], long requestId, int & baseQty, const long idArray[], const int storeArray[], const int qtyArray[], int count, int & newCount);
void main()
{
const int maxCells = 10;
char NewFileName[50] = "pr2.txt";
char fileName[50] = "pr1.txt";
int requestId;
const int ORDER_VALUE = 500;
long idArray[maxCells];
int storeArray[maxCells];
int qtyArray[maxCells];
int count = 0;
int newCount = 0;
bool result;
result = loadArrays(fileName, idArray, storeArray, qtyArray, count, maxCells);
if (result == false)
{
if (count == 0)
{
cerr << "Couldn't open the input file" << endl;
exit(1);
}
else
{
cerr << "couldn't load all the data from the file" << fileName;
exit(2);
}
}
printArrays(cout, idArray, storeArray, qtyArray, count);
cout << "Enter a product ID" << endl;
cin >> requestId;
int baseQty;
if(extractData(NewFileName,requestId, baseQty,idArray, storeArray, qtyArray,count, newCount))
{
cout<<" "<<requestId<<" is extracted from the data and its baseQty is: "<<baseQty<<endl;
}
system("pause");
}
bool loadArrays(const char fileName[], long idArray[], int storeArray[], int qtyArray[], int & count, int maxCells)
{
bool ok = true;
count = 0;
ifstream infile;
infile.open(fileName);
if (!infile)
ok = false;
else
{
int i = 0;
while (i < maxCells && infile >> idArray[i] >> storeArray[i] >> qtyArray[i])
{
i++;
}
count = i;
if (!infile.eof())
ok = false;
infile.close();
}
return ok;
}
void printArrays(ostream & w, const long idArray[], const int storeArray[], const int qtyArray[], int count)
{
for (int i = 0; i < count; i++)
{
w << idArray[i] << " " << storeArray[i] << " " << qtyArray[i] << endl;
}
}
bool extractData(const char newFileName[], long requestId, int & baseQty, const long idArray[], const int storeArray[], const int qtyArray[], int count, int & newCount)
{
newCount = 0;
ofstream outfile;
outfile.open(newFileName);
if (!outfile)
return false;
else
{
int i = 0;
while (i < count )
{
if(idArray[i] == requestId)
{
outfile << idArray[i] << storeArray[i] << qtyArray[i];
baseQty = qtyArray[i];
return true;
}
i++;
}
outfile.close();
if(newCount == 0)
return false;
else
return true;
}
}
----------------------------------------------------------------------------------------------------------------------------