A question about file streaming

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2005
Posts: 1
Reputation: KUJhawks is an unknown quantity at this point 
Solved Threads: 0
KUJhawks KUJhawks is offline Offline
Newbie Poster

A question about file streaming

 
0
  #1
Feb 26th, 2005
Hello everyone, I have just started learning C++ and I am trying to write a program that reads from a file (scores.txt) and then prints the average of these numbers to the screen and print to another file named "average.txt." I have written some code but when I compile I keep getting an error with the following lines (undefined identifier 'print'):

print(cout, temp); // print to screen
print(fout, temp); // print to file

Any help or tips would be appreciated. Thanks!

Complete code:
  1. #include <iostream>
  2. #include <fstream> // for file I/O
  3. #include <cstdlib> // for exit
  4. using namespace std;
  5.  
  6. void copyFile(ifstream&, ofstream&);
  7.  
  8. int main ()
  9. {
  10. ifstream in_stream;
  11. ofstream out_stream;
  12.  
  13. in_stream.open("scores.txt");
  14. out_stream.open("average.txt");
  15.  
  16. // This code checks for failures
  17. if( in_stream.fail() || out_stream.fail())
  18. {
  19. cout << "Error opening file... exiting..." << endl;
  20. // Exits the program
  21. exit(1);
  22. }
  23.  
  24. double next, sum = 0;
  25. int count = 0;
  26.  
  27. // The code continues until the end of the file is reached
  28. while(!in_stream.eof())
  29. {
  30. in_stream >> next; // get next number
  31. sum = sum + next; // keeps track of the sum
  32. count++;
  33. }
  34.  
  35. // calculate and print average
  36. double average = sum / count;
  37. cout << "average = " << average;
  38.  
  39. copyFile(in_stream, out_stream);
  40.  
  41. // close both streams
  42. in_stream.close();
  43. out_stream.close();
  44.  
  45. return 0;
  46. }
  47.  
  48. void copyFile(ifstream &fin, ofstream &fout)
  49. {
  50. char temp;
  51.  
  52. while(!fin.eof())
  53. {
  54. // read in one char including spaces
  55. // and new lines!
  56. fin.get(temp);
  57.  
  58. print(cout, temp); // print to screen
  59. print(fout, temp); // print to file
  60. }
  61. }
Last edited by alc6379; Feb 28th, 2005 at 5:50 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 27
Reputation: Gnome_101 is an unknown quantity at this point 
Solved Threads: 0
Gnome_101 Gnome_101 is offline Offline
Light Poster

Re: A question about file streaming

 
0
  #2
Feb 26th, 2005
I've never used the print command to do what you are trying to do. I use cout>>temp; which could work.

Also, to print to the file, you can open a stream, and use it to print to a file.


ifstream read_file;
ofstream write_file;

ifstream is in file stream
ofstream is out file stream


write_file.open(partNum.c_str()); // will open a file for writing with file name

write_file<<partNum2; // will write any data you want to the scree.

Use cout to print it to the screen.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 3316 | Replies: 1
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC