Thread: Re: [Dev-C++] HELP---Read/Write Files in Dev C++
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
From: Michael C. <jd...@ho...> - 2001-09-25 07:19:37
|
Hi Julian, I am currently at work and haven't Dev-C++ installed, so I canot test your program. I noticed that you do not test the outcome of the open for either of your files. This would be a good first step. E.g. Extract from Bjarne Stroustrup, p.638 (note: your example may vary slightly for it to work) ifstream from(file1); ofstream to (file2); if (!from) ...; if (!to) ...; If I have time I will download Dev-C++ later to try it out but I am very busy right now. Hope my little contribution helps, Michael >From: Julian Etienne <j_e...@ya...> >Reply-To: dev...@li... >To: dev...@li... >Subject: [Dev-C++] HELP---Read/Write Files in Dev C++ >Date: Mon, 24 Sep 2001 14:27:42 -0700 (PDT) > >I have a program that complies and runs in both >Microsoft C++ 6.0, and my schools Dec Unix C++ >complier. I opened a new project in Dev C++ make it a >console application. I added these three files to my >project pwrpro.h, pwr.cpp, runpwr.cpp The program >compiles with no errors. The problem is that this >program should read data from a file and the then >write a new file, with calculations in it. It writes a >blank file. I am wondering if I am missing something >for it to work correctly in this IDE. When i do not >put my file in.data in the directory it correctly >prints "the file is not found" in the output file. > >ANY HELP WILL BE GREATLY APPRECIATED!!!! > >//---------- >// Specification File (pwrpro.h) >// This module provides exponentiation functions. >//---------- > >int PowerOfInt ( int someInt, int exp ); >// PRE: Assign int (someInt) and exp > 0 >// POST: Firstnum raised to the power "expon1" >//(NOTE: Large exponents may produce overflow) > >float PowerOfFloat ( float someFloat, int exp ); >// PRE: Assign float (someFloat) and assigned some exp > > 0 >// POST: someFloat raised to the power "exp" >//(NOTE: Large exponents may produce overflow) > >bool NonZeroInt (int exp); >//PRE: Take infile exp >//POST: make sure exp > 0 > >----------------------------------------------- >----------------------------------------------- >----------------------------------------------- > >//---------- >// Specification File (pwr.cxx) >// This module provides exponentiation functions. >//---------- >#include <iomanip.h> >#include "pwrpro.h" > >int PowerOfInt ( int someInt, int exp ) >//........... >// PRE: Assign int (someInt) and exp > 0 >// POST: Firstnum raised to the power "expon1" >//(NOTE: Large exponents may produce overflow) >//........... >{ > >int i; >int partialVal = 1; > >for ( i = 1; i <= exp; i++) >partialVal *= someInt; >return partialVal; > >} > >float PowerOfFloat (float someFloat, int exp ) >//........... >// PRE: Assign float (someFloat) and assigned some exp > > 0 >// POST: someFloat raised to the power "exp" >//(NOTE: Large exponents may produce overflow) >//........... >{ >int i; >float partialVal = 1.0; > >for ( i = 1; i <= exp; i++) >partialVal *= someFloat; >return partialVal; > >} > >bool NonZeroInt (int exp) >//.......... >//PRE: Take infile exp >//POST: make sure exp > 0 >//.......... >{ >bool value; > >if (exp <= 0) value = false; >else >value = true; >return value; >} > >------------------------------------------------- >------------------------------------------------- >------------------------------------------------- > >//************************************************************************* > > >//PURPOSE >// runpwr.cxx) >// This program reads IntNum, IntExp, FloatNum, >FloatExp from an input file and calculates >// IntNum^Int Exp and Float Num^ Float Exp. >// Note X^n denotes X raised to the nth power. >//************************************************************************* > > >#include <fstream.h> >#include <iomanip.h> >#include "pwrpro.h" > >int main() >{ >ifstream inFile; >ofstream outFile; > >inFile.open("in.data", ios::nocreate ); >outFile.open("out.data"); > >if (! inFile) >{ >outFile<<" ERROR: Unable to locate file named >'in.data' "<<endl; >return 0; >} > >int IntExp, FloatExp, IntNum; >float FloatNum; > >inFile >> IntNum >> IntExp >> FloatNum >> FloatExp; > >while (! inFile.eof() ) >{ > >if (NonZeroInt (IntExp)== false) >outFile <<"This Function accepts only " ><<"postive integer exponents!" ><<" Your exponent is zero or negative. > >"; >else >{ > >outFile <<IntNum << "^"<< IntExp << " = " << setw (10) > ><< PowerOfInt(IntNum,IntExp) <<endl; > >} > > >if (NonZeroInt (FloatExp)== false) >outFile <<"This Function accepts only " ><<"postive integer exponents!" ><<" Your exponent is zero or negative. > >"; >else >{ > >outFile <<FloatNum << "^"<<FloatExp << " = " << setw >(10) ><<setiosflags(ios::fixed) << setprecision >(2)<<PowerOfFloat(FloatNum, FloatExp) ><<endl <<endl; > >} > >inFile >> IntNum >> IntExp >> FloatNum >> FloatExp; >} > >inFile.close(); >outFile.close(); >return 0; >} > > >__________________________________________________ >Do You Yahoo!? >Get email alerts & NEW webcam video instant messaging with Yahoo! >Messenger. http://im.yahoo.com > >_______________________________________________ >Dev-cpp-users mailing list >Dev...@li... >https://lists.sourceforge.net/lists/listinfo/dev-cpp-users _________________________________________________________________ Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp |