|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now! |
|
#1
|
|||
|
|||
|
I have a program which is supposed to write to a file. I have included <fstream> in my program before main function. I have an fout in my main function and in another function. I get the follwoing errors in the last line of code in the following function. Appreciate any help.
PHP Code:
error C2065: 'fout' : undeclared identifier error C2297: '<<' : illegal, right operand has type 'char [4]' |
|
#2
|
||||
|
||||
|
That's because you haven't declared what fout is. You have to declare it of type fstream like this:
Code:
//Print Student Data
fstream fout("filename.txt", ios_base::out | ios_base::trunc);
fout << StudentID << "\t " << StudentName << "\t\t\t" << FinalGrade;
fout.close();
Hope this helps. |
|
#3
|
|||
|
|||
|
I have opened the output file in my main function. Do I have to copy the statement to the other function as well?
|
|
#4
|
||||
|
||||
|
If you open fout in a function -- in any function, even in main() -- , then it is only known within that function. To make it known to a function that main calls, you need to pass it as an argument to that function.
A visual aid that we were given for figuring out scope is to view the entire source file as a flat field. Something declared on that field is visible while you are on that field. Now represent a function as a hole in the ground and everything declared in that function is visible within that hole and everything on the field is visible within that function (the hole only comes up to knee height, so you can still see the field). Also visible to you is the presence of the other functions, but you cannot see what is inside their holes just as they cannot see what is inside your hole. You can toss messages about what you have to the other functions (ie, pass parameters in function calls), but that's all. Of course, that was for Pascal, wherein having functions within functions (holes within holes) complicates the picture a bit, but the analogy still held. Hope it helps. So another solution would be to declare fout in the file outside of the main function, on the flat field of the analogy I gave above. Then it would be known globally to ALL functions within that file. However, some academicians try to discourage the use of global variables. Some may even consider it almost as heretical as using goto . You will need to know what your instructor's attitude towards global variables is before you use that approach. I wouldn't want for you to get points taken off. Last edited by dwise1_aol : April 11th, 2003 at 10:03 AM. |
|
#5
|
||||
|
||||
|
Quote:
Eek, no! You open the output file just once. What you need to do is to make fout known to the function that you call (read my other response on this). If you use one to open a file, then the other one will know nothing about that. BTW, if you declare fout in main() and then also declare it in the function that you call from main, those are two different variables that have nothing to do with each other. In this case, you should only declare fout once, but make it known to all functions that will need it, either by passing it as a parameter or by declaring it globally (bearing in mind the caveats about what your instructor would think of your solution). |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Printing to file error on 1 line of code only |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|