Re: Using fstream
Re: Using fstream
- Subject: Re: Using fstream
- From: Laurence Harris <email@hidden>
- Date: Thu, 8 Feb 2007 20:05:37 -0500
On Feb 8, 2007, at 7:23 PM, Adam wrote:
John,
Thank you for your response to my question. Are you saying that I
need to localize the variables? I would normally do this but it was
just a quick program thrown together to see if it would work,
Huh? There's no reason to declare them globally over declaring them
locally five lines farther down. Make it a habit to always use good
programming practices (even in test code such as this), and it's a
good practice to limit the scope of variables as tightly as possible.
Larry
so I understand how that could really mess up my computer in bigger
programs. Is there another way that it could hurt the program? I
think there might be something wrong with using arr_words[50] and
fin.getline(arr_words, 49) but I figured that I didn't want to go
too far, and only 30 characters is all I need.
The problem is not C++, as far as I can tell, but in the execution
of the program. The problem that I am facing is that when I execute
this, the output it gives me is 0, which means that something
didn't work in the transfer... that's the reason it's on the xcode
list and not somewhere else. I think it's in the development mode,
but I don't know much about those so I don't want to wreck my
program. It's set to debug right now... does this help?
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.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
On Feb 7, 2007, at 6:23 PM, John Daniel wrote:
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;
}
_______________________________________________
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