C++ write to file
I have a program which is write to text file.
Example in my textfile:
4
QA131 100 100 100 100
QA132 100 100 100 100
QA133 100 100 100 100
QA134 100 100 100 100
I want to write a new line which store the record and the 4 will turn into 5.
May i know the methond how can i write to file?
Here is my coding:
void inputRecord()
{
Product product[101];
char FILENAME[20];
cout<<"Enter the file you want to open: ";
cin>>FILENAME;
ifstream read(FILENAME);
if (read.is_open())
{
while(read.good())
{
read>>product[0].line;
for(int i =0;i< product[0].line;i++)
{
read >>product[i].PartNumber
>>product[i].initialQuantity
>>product[i].quantitySold
>>product[i].minQuantity
>>product[i].price;
}
product[0].line+=1;
}
read.close();
}
ofstream write(FILENAME);
write<<product[0].line << endl;
for(int i =0;i< product[0].line;i++)
{
write << product[i].PartNumber << '\t'
<< product[i].initialQuantity << '\t'
<< product[i].quantitySold << '\t'
<< product[i].minQuantity << '\t'
<< product[i].price << '\t' << endl;
}
write.close();
cout << "Inventory added successfully!" << endl;
system("pause");
system("CLS");
}
I cannot write the item to file.
Please help. Thanks.
No comments:
Post a Comment