We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,560 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

ifstream and ofstream

okay so im kind of confused now. . . . . i didn't even have a code to show you cause I know its wrong..I've been absent 2 days on my Programming so its really hard to cope up...i tried researching for ifstream.h and ofstream.h....and so far ifstream.h is for reading a file, and ofstream.h is for writing to a file right??

I got the lab exercise that i missed so I could understand clearly but so far, I understood the ofstream.h.100%(maybe) but no idea ifstream.h..(yea, im using TURBO c++ 4.5 compiler..its really old)

So here's the deal:
What if a problem was to make a program that asks the user for input file(yes, so my inputs are in a file..file to display screen..so its reading to a file right?) with inputs of an arithmetic operator keyword and 2 numbers.(in this case I would want to do simple addition, keyword is "sum")...

Then output(in monitor)..

The sum of (whatever the first input is in the file) and (whatever the second input is in the file) is (the sum of the 2 input file)..

there that's the problem..i got confused because i think i'll need both ofstream.h and ifstream.h...right?...can you please give me a simple code with explanation of it....and I swear this is not an assignment...i just want to cope up with our lesson..Thaks alot! ! !

6
Contributors
9
Replies
4 Days
Discussion Span
9 Months Ago
Last Updated
10
Views
Question
Answered
RonKevin
Junior Poster in Training
63 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

A quick reminder I use is to use the first letter.

O -fstream = output/writing
I -fstream = input/ reading

That's about it. So if you need to write out to a file you will need ofstream.

Edit** And what AD said below.

iamthwee
Posting Genius
6,254 posts since Aug 2005
Reputation Points: 1,567
Solved Threads: 476
Skill Endorsements: 33

i didn't even have a code to show you cause I know its wrong.

That doesn't matter here -- most of the code we see posted here is wrong. All we require is that you make an effort to write your program, we don't require the code be correct.

researching for ifstream.h and ofstream.h....and so far ifstream.h is for reading a file, and ofstream.h is for writing to a file right??

No. There are no such header files. fstream, ifstream and ofstream are all declared in the same header file named fstream.h (current compilers use <fstream> instead of <fstream.h>

If you can you should upgrade your compiler to either Code::Blocks or VC++ 2010 Express, both are free. If you live in India then you may not have that option because your school most likely requires Turbo C.

Ancient Dragon
Achieved Level 70
Team Colleague
32,104 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,575
Skill Endorsements: 68

You are probably right, you might need to use both... luckily c++ provides fstream.h which is a combination of ifstream and ofstream. If you are just outputting to the screen though you can just use the ofstream object cout provided in iostream.

Labdabeta
Practically a Master Poster
613 posts since Feb 2011
Reputation Points: 27
Solved Threads: 31
Skill Endorsements: 1

oh i see..well this is what ive done so far...The input file contains sum 9 9...but the output it gives me is not correct..

#include<fstream.h>

void main()
{
    ifstream in;
    in.open("A57M2_Manuela.txt");
 char SUM[4];
 int x,y;
    in>>SUM;
    in>>x;
    in>>y;
 int sum;
    sum=x+y;
    cout<< "The total of "<<x<<" and "<<y<<" is"<<sum;
}

the output gives me random numbers for x, y and sum.....it should've read the input file which is sum 9 9...see im confused..as i said the problem was to make a program that asks the user for input file(yes, so my inputs are in a file..file to display screen..so its reading to a file right?) with inputs of an arithmetic operator keyword and 2 numbers.(in this case I would want to do simple addition, keyword is "sum")...

Then output(in monitor)..

The sum of (whatever the first input is in the file) and (whatever the second input is in the file) is (the sum of the 2 input file)..

RonKevin
Junior Poster in Training
63 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Maybe I misunderstand the problem, but if the first two numbers in the file are 9 and 8 then all you have to do is read x and y, then add them todether. There is no need for SUM array. Just delete lines 7 and 9.

Ancient Dragon
Achieved Level 70
Team Colleague
32,104 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,575
Skill Endorsements: 68

You might want to check and see if you are even opening the file. to do that you can use this

if(in.fail())
    cout << "The file could not be open;"

I would put that right after line 6 to make sure the file is open before you try to read from it.

NathanOliver
Posting Virtuoso
1,513 posts since Apr 2009
Reputation Points: 269
Solved Threads: 277
Skill Endorsements: 3

It still won't give me the result...maybe i'll rephrase the my problem...

Lets say: I have a file..that file contains..
sum 9 9...

what I'm going to do is read from that file so that it would give me an output of

The sum of 9 and 9 is 18

gets? in here i think i'll use ifstream...oh my, i hope someone would help me

RonKevin
Junior Poster in Training
63 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Try printing out avarything you read in as soon as you read it.
Can you use STRINGs rather than CHARs? If not, make your CHAR array larger -- say [20].

...oh my,

Channelling George Takai, I see... ;-)

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 36

Guys, i finally figured out what to do..tell me if this is okay,,,it worked for me so far..

#include<fstream.h>
#include<cstring.h>
#include<math.h>
#include<conio.h>

void main()
{
 int x,y;
 ifstream in;

 in.open("file.txt");
 string kw;
 in>>kw;
 in>>x;
 in>>y;

 if(kw=="sum"||kw=="plus"||kw=="total")
 {
  cout<< "The "<<kw<<" of "<<x<< "and "<<y<< "is "<<x+y;
 }
 else if(kw=="difference"||kw=="minus"||kw=="subtraction")
 {
  cout<< "The "<<kw<<" of "<<x<< "and "<<y<< "is "<<x-y;
 }
 else
 {clrscr();}

 in.close();

}
RonKevin
Junior Poster in Training
63 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 7 Months Ago by Ancient Dragon, iamthwee, WaltP and 2 others

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0964 seconds using 2.84MB