Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Writing to a file in C++

Writing to a file in C++
Thread Tools
Moderator
Join Date: Sep 2000
Location: Irvine, CA
Status: Offline
Reply With Quote
Apr 18, 2001, 12:28 AM
 
Hello,

I am new to the world of C++ and I just wrote a simple program (because I was bored) that is supposed to be a teacher's program that basically asks for the name of the student (first only), the letter grade, and then the average (integers only). The program runs fine, but I want to save the results to a file named "new.c." How can I do that? I attempted to do just that, but I see a blank new.c file but nothing inside.

Code:
#include<iostream> #include<string> #include <fstream> int main() { int i = 1; char NL = '\n'; char TAB = '\t'; int Max_Num_Students, number; using namespace std; cout << "Welcome to the Teacher's Grading Program v.1" << NL << NL; cout << "Enter number of students" << NL; cin >> Max_Num_Students; number = Max_Num_Students; struct Grades { string name; char grade; double average; } Student[100]; std:: fstream fout; for( i = 1; i < number + 1; i++) { cout << "Enter the name of the " << i << " student." << NL; cin >> Student[i].name; cout << "Enter the letter grade of the " << i << " student." << NL; cin >> Student[i].grade; cout << "Enter the average of the " << i << " student." << NL; cin >> Student[i].average; fout.open("new.c", std::ios: : out); if(!fout.is_open()) return 0; } cout << NL << NL << "-------------RESULTS-------------" << NL << NL; cout << "#" << TAB << "Name" << TAB << TAB << "Grade" << TAB << "Average" << NL; cout << "---------------------------------------" << NL; for( i = 1; i < number + 1; i++) { cout << i << TAB << Student[i].name << TAB << TAB << Student[i].grade << TAB << TAB << Student[i].average << NL; fout << i << TAB << Student[i].name << TAB << TAB << Student[i].grade << TAB << TAB << Student[i].average << NL; } fout.close(); return 0; }
edit: removed an annoying simile in the program.

[This message has been edited by mindwaves (edited 04-18-2001).]
{{{ mindwaves }}}
     
Dedicated MacNNer
Join Date: Nov 1999
Location: Georgetown, Demerara, Guyana
Status: Offline
Reply With Quote
Apr 19, 2001, 12:31 PM
 
Hi mindwaves,

You're certainly busy coding these days, eh? :-)

Is it the case that the 'new.c' file is empty whenever you enter data for more than one student, but it has the expected contents (minus the headings) if there's just one student? If so, the problem might be that the 'fout' filestream is being accidentally opened more than once. [C++ won't write to a filestream that's "too open"; basically, on the second open, an internal 'failbit' gets set, which wouldn't affect the 'is_open()' method but would cause any subsequent '<<' operations to fail.]

Currently, the two statements which open the 'fout' filestream and test its success [i.e., the 'fout.open(...);' and 'if(!fout.is_open()) ...;' statements] are located within the read-loop, which would cause them to be executed more than once, if the number of students is greater than one. To make a long story short, try moving those two statements outside of the read-loop.

Regards,

--Paul
     
Moderator
Join Date: Sep 2000
Location: Irvine, CA
Status: Offline
Reply With Quote
Apr 19, 2001, 01:06 PM
 
Thanks again Paul!! It works now.

Yes, I have been busy programming so I hope that you don't mind if I innundate this forum with some questions. I am eagar to learn but I just need some help along the way.

[This message has been edited by mindwaves (edited 04-19-2001).]
{{{ mindwaves }}}
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 11:08 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2