Hii Friends,

I do not know how to use FILE for writing data.
I have tried something but i missed something to write and i do not know what to write now to get data inside file.
Help me plz.
Regards,
Shivi

circle.cc

#include <iostream>
#include "circle.h"
using namespace std;

double Circle::getarea() const
{
 return 3.14*radius*radius;
}

double Circle::setradius(double r)
{
radius = r;
}
int main()
{
Circle c1;
FILE *fp;
cout<<"Enter the radius: ";
double rad;

cin>>rad;
c1.setradius(rad);
double area = c1.getarea();
cout<<"area of circle: "<<area<<endl;
fp = fopen("area.dat", "w");
printf("%d\n", c1.getarea());
fclose(fp);
return 0;
}

circle.h

#ifndef CIRCLE_H_
#define CIRCLE_H_
#include<iostream>
using namespace std;
class Circle
{
public:
Circle() {};
double getarea() const;
double setradius(double r);
private:
double radius;
double a;

};
#endif

Recommended Answers

All 7 Replies

>>I do not know how to use FILE for writing data.
Excellent :) Don't use it in c++ programs. Use fstream, ofstream or ifstream objects.

commented: yup +20

>>I do not know how to use FILE for writing data.
Excellent :) Don't use it in c++ programs. Use fstream, ofstream or ifstream objects.

I know ofstream but the problem is when i restart my program i have delete the writen file everytime.
ofstream do not have clean old and rewrite on old.
if in the output file i have like 200 entries i can not do this I think.

if u know tell me how?

After Ancient Dragons post the only thing I could do was to lead you to that a proper source so that you can read from the standard ones rather than some link that pops up :) !!! So enjoy reading this.

ofstream is for output particularly so just use flags as:

ofstream file;
file.open(<filename>,ios::app);

Or fstream with file opened as

fstream file;
file.open(<filename>,ios::out|ios::app);

for appending rather than over writing.

Actually the post is confusing because the above flag is for appending(not deleting the old contents).

Of course that can be done -- if you want the old information deleted when you open the file just specify ios::trunc as the second argument in the open statement. ofstream out("file.txt", ios::trunc); Look here for complete reference.

[edit]Maybe I misunderstood the problem. See ^^^ post.[/edit]

@Ancient Dragon :

Of course that can be done -- if you want the old information deleted when you open the file just specify ios::trunc as the second argument in the open statement. ofstream out("file.txt", ios::trunc); Look here for complete reference.

[edit]Maybe I misunderstood the problem. See ^^^ post.[/edit]

On opening a file as :

ofstream file;
file.open(<filename>);

Default action is truncation so no need of ios::trunc flag but ya for appending we do need ios::app flag.

Of course that can be done -- if you want the old information deleted when you open the file just specify ios::trunc as the second argument in the open statement. ofstream out("file.txt", ios::trunc); Look here for complete reference.

[edit]Maybe I misunderstood the problem. See ^^^ post.[/edit]

if i have 100 timesteps (loop) for calculations and i have 100 result entries. trunc will overwrite after each calculation , it will never store all results.
out/app will store all the 100 data and when i will stop my program and restart new data will be written after 100 means from 101 102.............entries.

I hope u guys understand what is my problem.

Thanks

Well if you want appending of information follow post #4
if overwriting you don't need to use any flag as such for more info about this refer post # 5 and # 6

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.