Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Using fstream



Adam,
In C++, you don't have to put your variables at the start of code blocks. In fact, you shouldn't, because objects may perform initialization when you declare them. That is what is happening in your case. In C++, variable scope is much more significant than in C. Lookup constructors and destructors.

If you move the line "ifstream fin("input.txt");" just before the line " fin.getline(arr_words, 49); //using getline to get whole words" it should work slightly better. You still have problems, but I'll leave those as an exercise for the programmer.

John


On Feb 7, 2007, at 7:30 PM, Adam wrote:

Hello all,

I created a short program using <fstream>, and in it I created a file that I wanted to put information in, and later extract the information. (see code below) The problem is that it doesn't work - I'll input the data a second later and it doesn't display any information that it stored. Is this a code bug, or something in the way I compiled it?

~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
code
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
#include <iostream>
#include <fstream>

using namespace std;

char arr_words[50];
int fifty_one;
ifstream fin("input.txt");
ofstream fout("input.txt");

int main()
{
fout << "Dude, am I coming in loud and clear?" << 51 << flush; //saves file inside project
fout.close(); //close teh file!

fin.getline(arr_words, 49); //using getline to get whole words
fin >> fifty_one;
cout << fifty_one << endl << arr_words;
fout << arr_words << fifty_one << flush;
return 0;
}

~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~





All help is appreciated and thanks in advance.

~ < [ / Adam-o \ ] > ~

+-------------------------------------------+
| My computer's Stats                      |
| Mac OS 10.4.8                               |
| 1 Ghz Apple Powerbook G4        |
| Xcode v. 2.4.1                                 |
+-------------------------------------------+

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Please keep your posts straight and 
to the point. Also please comment 
the code you use as I am still learning 
OpenGL and Xcode and may not yet 
understand the meaning of the command.
 Thank you.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/xcode-users/email@hidden

This email sent to email@hidden

References: 
 >Using fstream (From: Adam <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.