# n00b c++ code

**URL:** https://forum.kirupa.com/t/n00b-c-code/10592
**Category:** programming
**Created:** [January 3, 2003, 9:05pm UTC](https://forum.kirupa.com/t/n00b-c-code/10592 "2003-01-03T21:05:01Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![kaotic](https://avatars.discourse-cdn.com/v4/letter/k/c77e96/32.png) [@kaotic](https://forum.kirupa.com/u/kaotic)
#### Post date: [January 3, 2003, 9:05pm UTC](https://forum.kirupa.com/t/n00b-c-code/10592/1 "2003-01-03T21:05:01Z")

</div>

just learned how to write and retreive files in c++  
:cool:

```php

//philip
#include< iostream.h >
#include< fstream.h > //neccesary for writing files

void main()
{
	//variables
	char userName[25];  
	int age;   
	ofstream outfile; 
             
 	cout<<"Enter Your Name: ";
	cin.get(userName, 25);
	cout<<Enter Your Age: ";
	cin>>age;

	outfile.open     

                 ("c:/windows/desktop/NameAge.txt", ios :: out); 

	if(outfile) //check for error
	{
		outfile<<userName<<endl;
		outfile<<age<<endl
			outfile.close();
	}
	else //file not open
	{
		cout<<"A fatal error has occured please curl up and die. 
";
	}
}

```

edit–\> why wont it show al the code?

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [January 5, 2003, 1:51am UTC](https://forum.kirupa.com/t/n00b-c-code/10592/2 "2003-01-05T01:51:01Z")

</div>

why do u use:

**ofstream outfile;**  
and then:  
outfile.open(“blah[…]”, **ios :: out** );

i mean, you’ve declared the variable ‘outfile’ as being an ofstream class type, which is a class derived from fstream, specially for writing in a file…

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [January 9, 2003, 6:56pm UTC](https://forum.kirupa.com/t/n00b-c-code/10592/3 "2003-01-09T18:56:12Z")

</div>

that is where it writes the file to

```php

outfile.open     

                 ("c:/windows/desktop/NameAge.txt", ios :: out); 

```

the code is to make a new file on the desktop and open it

```php

outfile<<userName<<endl;
        outfile<<age<<endl;

```

then this command takes the variables that the user entered  
and writes them to the open file

```php
outfile.close();

```

and of course this command closes the open file

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [January 10, 2003, 8:35pm UTC](https://forum.kirupa.com/t/n00b-c-code/10592/4 "2003-01-10T20:35:16Z")

</div>

oh, and one more thing: if you include the fstream header it’s not neccessary to also include iostream.h, because the fstream header uses the iostream.h, so it will include it automaticly…

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [January 10, 2003, 8:36pm UTC](https://forum.kirupa.com/t/n00b-c-code/10592/5 "2003-01-10T20:36:12Z")

</div>

know what i mean ? 🙂

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [January 11, 2003, 4:09am UTC](https://forum.kirupa.com/t/n00b-c-code/10592/6 "2003-01-11T04:09:11Z")

</div>

shiat, i didnt know that  
ill try that out lata  
thanks man  
save me at least 2 or 3 seconds of typing

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [January 11, 2003, 8:08pm UTC](https://forum.kirupa.com/t/n00b-c-code/10592/7 "2003-01-11T20:08:59Z")

</div>

in order to be a good programmer, u must know EVERYTHING… so i said u should know that… one thing from me, one thing from somebody else…

and anyway, the thing with iostream and fstream it’s a basic thing… any begginer knows that…

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [January 12, 2003, 7:34pm UTC](https://forum.kirupa.com/t/n00b-c-code/10592/8 "2003-01-12T19:34:06Z")

</div>

There is no point of not including in iostream.h

All header files should have #ifndef #define so you aren’t including it twice. Not including it in just makes your code more difficult to maniupulate. Say you remove fstream, and use a diffrent header file for file manipulation that dosen’t include iostream?

Its dumb not to include in everything your program relies on.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [January 12, 2003, 10:18pm UTC](https://forum.kirupa.com/t/n00b-c-code/10592/9 "2003-01-12T22:18:01Z")

</div>

you are right in a way… but i thought Kaotic should know about this thing… that’s all

and anyway, when you’re writing a program, either you use file manipulation, either you don’t… before you write a piece of code, you make some plans like ‘what is this program going to do and so on’…

anyway (again), it’s no use to talk about #include, i just gave Kaotic some information, his real problem is something else, right?

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [January 13, 2003, 4:08am UTC](https://forum.kirupa.com/t/n00b-c-code/10592/10 "2003-01-13T04:08:24Z")

</div>

thanks for the advice homies

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [January 17, 2003, 10:47pm UTC](https://forum.kirupa.com/t/n00b-c-code/10592/11 "2003-01-17T22:47:46Z")

</div>

heh… no problem… 🙂
