0

Hye.
i write this code to get inpur from user such as ram and hard disk capacity as integer type using structures. After that i want to create a file in which i want to save that data. In the code below it is getting input from user but not creating file. Plz tell where is my mistake.
Code is:

#include <conio.h>
#include <iostream.h>
#include <fstream.h>

void main()
{
struct computerspec
{
int ram;
int hdisk;
}compuspec;

cout<<"Enter the RAM capacity"<<endl;
cin>>compuspec.ram;


cout<<endl;
cout<<"Enter the Hard Disk capacity"<<endl;
cin>>compuspec.hdisk;


cout<<endl;

ofstream file;

file.open("computerspec.txt");

file<<"ComputerSpec attributes/Data members\n";

file<<"RAM: "<<compuspec.ram<<endl;

file<<"Hdisk: "<<compuspec.hdisk<<endl;

file.close();

getch();

}

4
Contributors
3
Replies
4
Views
12 Years
Discussion Span
Last Post by alc6379
0

file.open("computerspec.txt");

You need to check if the file is open so

if (file.fail())
{
cerr<<"error opening file"<<endl;
exit(1);
}
else
cout << "File opened for processing" <<endl;

the chances are you havent created that file on the hdd or even if you have it may be in the wrong directory

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.