Project Home
Project Home
Trackers
Trackers
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - Reading from a file and outputting to a file: (4 Items)
   
Reading from a file and outputting to a file  
I'm trying to read data like integers from a simple text file and output to a file, but am having no success.  I am used
 to reading from a file using fstream commands, but these are not working.  What header files do I need to include and 
is there any other specific commands that I need to use to read from a file and to output to a file?
Re: Reading from a file and outputting to a file  
ANSI C stuff would work. Can you post an example?

On 03/10/2011 11:59 AM, Nick Hayes wrote:
> I'm trying to read data like integers from a simple text file and output to a file, but am having no success.  I am 
used to reading from a file using fstream commands, but these are not working.  What header files do I need to include 
and is there any other specific commands that I need to use to read from a file and to output to a file?
> 
> 
> 
> _______________________________________________
> 
> QNX Momentics Getting Started
> http://community.qnx.com/sf/go/post83903
> 
Re: Reading from a file and outputting to a file  
We are trying to do our coding in C++.

Here is the sample code:

#include <cstdlib>
#include <iostream>
#include <math.h>
#include <fstream>
#include <istream>
#include <ostream>
using namespace std;

int main(int argc, char *argv[]) {
	std::cout << "Welcome to the QNX Momentics IDE" << std::endl;
	string num;
	cout<<"hello world"<<endl;
	ifstream input;
	ofstream output;
	input.open("numbers.txt");
	output.open("success.txt");
	int z;
	for(int i=0;i<6;i++){
	input>>z;
	cout<<z<<endl;
	}
	double one, two, ans;
	one = 10.0;
	two = 20.35;
	ans = one*two;
	cout<<ans<<" x"<<endl;
	output<<ans<<" victory"<<endl;
	double degree;
	degree=cos(3.1459/4);
	cout<<degree<<endl;
	ans = pow(one,two);
	cout<<ans<<" pow"<<endl;
	ans = sqrt(one);
	cout<<ans<<" sqrt"<<endl;
	output.close();
	return EXIT_SUCCESS;
}

And our file numbers.txt looks like:
1 2 5 7 8 9

And here is the console output:

Welcome to the QNX Momentics IDE
hello world
134612012
134612012
134612012
134612012
134612012
134612012
203.5 x
0.706345
2.23872e+20 pow
3.16228 sqrt

Re: Reading from a file and outputting to a file  
On Thu, 2011-03-10 at 16:16 -0500, Nick Hayes wrote:
> We are trying to do our coding in C++.
> 
> Here is the sample code:
> 
> #include <cstdlib>
> #include <iostream>
> #include <math.h>
> #include <fstream>
> #include <istream>
> #include <ostream>
> using namespace std;
> 
> int main(int argc, char *argv[]) {
>         std::cout << "Welcome to the QNX Momentics IDE" << std::endl;
>         string num;
>         cout<<"hello world"<<endl;
>         ifstream input;
>         ofstream output;
>         input.open("numbers.txt");

are you sure input.open succeeded?