can someone edit my c code where it will output to a file in
can someone edit my c++ code where it will output to a file in xcode
#include <iostream>
#include <fstream> //For file operations
#include <string.h> //For string comparisions
using namespace std;
struct StockNode{ //Creating of linked list node structure
char itemname[20];
char itemnumber[5];
char quantity[3];
char price[6];
char safestock[3];
struct StockNode *link;
};
class InventoryStock //Class for operations and members
{
StockNode *head;
public:
InventoryStock() //Constructor default
{
head=NULL;
}
void addStockNode(StockNode *nn) //Add node method
{
StockNode *temp=NULL;
if(head==NULL || strcmp(head->itemname,nn->itemname)>=0) // Adding first node whether null or head node smaller than nn
{
nn->link=head;
head=nn;
return;
}
temp=head;
while(temp->link!=NULL && strcmp(temp->itemname,nn->itemname)<0) //Traverse upto null before or find a node which is > than new node nn
temp=temp->link;
nn->link=temp->link; //If found insert in its sorted location
temp->link=nn;
}
void traverse() //To display sorted list
{
StockNode *temp=head;
cout<<\"\ Names of Items : \"<<endl;
while(temp!=NULL)
cout<<temp->itemname<<\"->\";
}
};
int main()
{
InventoryStock i1;
StockNode readstock;
ifstream ifile;
ifile.open(\"Invent.txt\"); //Open file for reading
while(!ifile.eof()) //Read until eof
{
ifile>>readstock.itemname>>readstock.itemnumber>>readstock.quantity>>readstock.price>>readstock.safestock; //Read each record of file
i1.addStockNode(&readstock); //Add to sorted linked list
}
ifile.close(); //Close the file
i1.traverse(); // Traverse list
return 0;
}
Solution
And now, this will write the data to an output file named StockList.txt. Here is the code for you:
#include <iostream>
#include <fstream> //For file operations
#include <string.h> //For string comparisions
using namespace std;
struct StockNode{ //Creating of linked list node structure
char itemname[20];
char itemnumber[5];
char quantity[3];
char price[6];
char safestock[3];
struct StockNode *link;
};
class InventoryStock //Class for operations and members
{
StockNode *head;
public:
InventoryStock() //Constructor default
{
head=NULL;
}
void addStockNode(StockNode *nn) //Add node method
{
StockNode *temp=NULL;
if(head==NULL || strcmp(head->itemname,nn->itemname)>=0) // Adding first node whether null or head node smaller than nn
{
nn->link=head;
head=nn;
return;
}
temp=head;
while(temp->link!=NULL && strcmp(temp->itemname,nn->itemname)<0) //Traverse upto null before or find a node which is > than new node nn
temp=temp->link;
nn->link=temp->link; //If found insert in its sorted location
temp->link=nn;
}
void traverse() //To display sorted list
{
StockNode *temp=head;
ofstream ofile; //Declares an outputstream
ofile.open(\"StockList.txt\"); //Opens a file for outputstream
ofile<<\"\ Names of Items : \"<<endl; //Writes the data to outputstream.
while(temp!=NULL)
ofile<<temp->itemname<<\"->\";
}
};
int main()
{
InventoryStock i1;
StockNode readstock;
ifstream ifile;
ifile.open(\"Invent.txt\"); //Open file for reading
while(!ifile.eof()) //Read until eof
{
ifile>>readstock.itemname>>readstock.itemnumber>>readstock.quantity>>readstock.price>>readstock.safestock; //Read each record of file
i1.addStockNode(&readstock); //Add to sorted linked list
}
ifile.close(); //Close the file
i1.traverse(); // Traverse list
return 0;
}


