| | |
A question about file streaming
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Feb 2005
Posts: 1
Reputation:
Solved Threads: 0
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:
print(cout, temp); // print to screen
print(fout, temp); // print to file
Any help or tips would be appreciated. Thanks!
Complete code:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> // for file I/O #include <cstdlib> // for exit using namespace std; void copyFile(ifstream&, ofstream&); int main () { ifstream in_stream; ofstream out_stream; in_stream.open("scores.txt"); out_stream.open("average.txt"); // This code checks for failures if( in_stream.fail() || out_stream.fail()) { cout << "Error opening file... exiting..." << endl; // Exits the program exit(1); } double next, sum = 0; int count = 0; // The code continues until the end of the file is reached while(!in_stream.eof()) { in_stream >> next; // get next number sum = sum + next; // keeps track of the sum count++; } // calculate and print average double average = sum / count; cout << "average = " << average; copyFile(in_stream, out_stream); // close both streams in_stream.close(); out_stream.close(); return 0; } void copyFile(ifstream &fin, ofstream &fout) { char temp; while(!fin.eof()) { // read in one char including spaces // and new lines! fin.get(temp); print(cout, temp); // print to screen print(fout, temp); // print to file } }
Last edited by alc6379; Feb 28th, 2005 at 5:50 pm.
•
•
Join Date: Apr 2004
Posts: 27
Reputation:
Solved Threads: 0
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.
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.
![]() |
Similar Threads
- File streaming server with multiple clients (Networking Hardware Configuration)
- save ram file to hard disk,how? (Graphics and Multimedia)
- Question about file read/write (Java)
- Windows Media Player Says The File IS Corrupt But Its Not (Windows 95 / 98 / Me)
- Difficulty understanding Object Orientated File Streaming (C++)
- Saving a file using C++ (C++)
- question about file and printer sharing (Web Browsers)
Other Threads in the C++ Forum
- Previous Thread: Help referencing variables
- Next Thread: Please hel me.....is urgent!!!
Views: 3316 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





