Re: Problem with ofstream...
WHY do you think it should be able to find that header???
1 Attachment(s)
Re: Problem with ofstream...
oh crap you're right. lol
okay that problem is solved.... but then I got 4 big errors telling me this...
error in the text file....
// Program 16.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string>
#include <fstream>
#include <iostream>
using std::ofstream;
using std::ifstream;
using std::cout;
using std::cin;
using std::endl;
int main ( )
{
int i;
struct record
{
char name[15];
float amt;
};
record master[10] = {{"Helen", 10}, {"Julie", 20}, {"Lena", 30}, {"Alan", 40}, {"Annie", 50}, {"May", 60},
{"Lee", 70}, {"Sam", 80}, {"June", 90}, {"Bill", 100}};
record trans[7] = {{"Lena", 10}, {"Julie", 5.75}, {"Lee", 15.02}, {"Ed", 40}, {"Julie", 10.00}, {"Art", 5.00},
{"Bill", 7.32}};
ofstream foutmaster("master.dat");
ofstream fouttrans("trans.dat");
for (i=0; i<10; i++)
{
foutmaster << master.name[i] << master.amt[i] << endl;
}
for (i=0; i<7; i++)
{
fouttrans << trans.name[i] << trans.amt[i] << endl;
}
foutmaster.close("master.dat");
fouttrans.close("trans.dat");
}
I just can't figure out how to solve this problem....
Re: Problem with ofstream...
Quote:
Originally Posted by
ch91woo
oh crap you're right. lol
record master[10] = {{"Helen", 10}, {"Julie", 20}, {"Lena", 30}, {"Alan", 40}, {"Annie", 50}, {"May", 60},
{"Lee", 70}, {"Sam", 80}, {"June", 90}, {"Bill", 100}};
for (i=0; i<10; i++)
{
foutmaster << master.name[i] << master.amt[i] << endl;
}
I just can't figure out how to solve this problem....
The "master" which you defined is an array of struct record.
So, if you want to print the every element of master, I think you must do it like this:
fourmaster << master[i].name << master[i].amt << endl;
By the way, it is recommended that you move the definition of "record" out of the main()
Re: Problem with ofstream...
ooohh okay. I fixed that.
Now I have to close the file.
I tried fouttrans.close();
foutmaster.close();
and fout.close();
but none of these worked....
how do I close these .dat files?
// Program 16.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string>
#include <fstream>
#include <iostream>
using std::ofstream;
using std::ifstream;
using std::cout;
using std::cin;
using std::endl;
int main ( )
{
int i;
struct record
{
char name[15];
float amt;
};
record master[10] = {{"Helen", 10}, {"Julie", 20}, {"Lena", 30}, {"Alan", 40}, {"Annie", 50}, {"May", 60},
{"Lee", 70}, {"Sam", 80}, {"June", 90}, {"Bill", 100}};
record trans[7] = {{"Lena", 10}, {"Julie", 5.75}, {"Lee", 15.02}, {"Ed", 40}, {"Julie", 10.00}, {"Art", 5.00},
{"Bill", 7.32}};
ofstream foutmaster("master.dat");
ofstream fouttrans("trans.dat");
for (i=0; i<10; i++)
{
foutmaster << master[i].name << master[i].amt << endl;
}
for (i=0; i<7; i++)
{
fouttrans << trans[i].name << trans[i].amt << endl;
}
}
Re: Problem with ofstream...
Code:
fouttrans.close();
foutmaster.close();
is enough.
However, recommends you to use ofstream like this:
Code:
ofstream foutmaster;
foutmaster.open("master.data", ... );
..........
foutmaster.close();
Re: Problem with ofstream...
oh okay. thanks. that's got the problem solved :)
Re: Problem with ofstream...
Quote:
Originally Posted by
gfzhang
However, recommends you to use ofstream like this:
Code:
ofstream foutmaster;
foutmaster.open("master.data", ... );
..........
foutmaster.close();
why?
Why not just his:
Code:
ofstream foutmaster("master.data", ... );
// read data, etc.
// when goes out of scope, it will close file automatically.
Re: Problem with ofstream...
Quote:
Originally Posted by
STLDude
why?
Why not just his:
Code:
ofstream foutmaster("master.data", ... );
// read data, etc.
// when goes out of scope, it will close file automatically.
wait does that mean when the file doesn't close the VSC++ program brings up an error?
Re: Problem with ofstream...
Quote:
Originally Posted by
STLDude
why?
Why not just his:
Code:
ofstream foutmaster("master.data", ... );
// read data, etc.
// when goes out of scope, it will close file automatically.
It is "recommend", but not necessary.
A good manner of coding, I think.
Open it before using it, close it after using it.
Otherwise, sometime in a big program, if another function want to access
the ***.data, and foutmaster has not been closed yet, maybe problem or trouble.
Re: Problem with ofstream...
Quote:
Originally Posted by
gfzhang
It is "recommend", but not necessary.
A good manner of coding, I think.
Open it before using it, close it after using it.
Otherwise, sometime in a big program, if another function want to access
the ***.data, and foutmaster has not been closed yet, maybe problem or trouble.
oh okay. thanks :]