# File handling using GCC/G++



## anantkhaitan (May 10, 2007)

Friends point me where I m wrong.

Code:


> #include<fstream>
> #include<iostream>
> int main()
> {
> ...



Command for compilation :


> $ g++ a.cpp -o a



Error message(s):


> a.cpp: In function ‘int main()’:
> a.cpp:5: error: ‘ofstream’ was not declared in this scope
> a.cpp:5: error: expected `;' before ‘f’
> a.cpp:6: error: ‘f’ was not declared in this scope


----------



## sam_1710 (May 10, 2007)

u forgot to give namespace buddy!!!
this code will compile..

```
#include<fstream>
#include<iostream>
using namespace std;
int main()
{
ofstream f;
f.open("1.txt");
f.put(65);
std::cout<<"Inserted";
f.close();
return 1;
}
```


----------



## kaustav_geek (May 10, 2007)

^^^
Yeah.. Absolutely correct... As per the ANSI C++ standards, the headers are not longer considered as files.. They are referred from namespace std.....Hence, inclusion of

```
using namespace std;
```

is a standard..
Don't forget it.


----------



## Sykora (May 10, 2007)

Once you've used the namespace, the std:: is not necessary.


----------



## anantkhaitan (May 10, 2007)

Thanks a million buddies.. I thought it was only for console in/out thats why used std:: before cout.. anyways i successfully compiled my code..


> #include<fstream>
> #include<iostream>
> using namespace std;
> int main()
> ...


----------



## anantkhaitan (May 12, 2007)

Friends I will be really grateful if anyone of u can suggest me alternative to common used conio functions like getch(), gotoxy(), kbhit(), _setcursortype() etc.


----------



## saurabh.sauron (May 12, 2007)

for linux, i dont think is required for getch()...anywayz, here is one.



> system("Pause");



i dont think linux is able to recognize it.


----------



## webgenius (May 12, 2007)

anantkhaitan said:
			
		

> Friends I will be really grateful if anyone of u can suggest me alternative to common used conio functions like getch(), gotoxy(), kbhit(), _setcursortype() etc.


are you programming under linux? if yes, then you can't use <conio.h> under GCC.


----------



## freebird (May 12, 2007)

Linux
------
Anjuta is a good gui ide for gcc,g++ and more.eclipse also there.
but CLI compiling helps learning gcc core isnt it?


----------



## Zeeshan Quireshi (May 13, 2007)

anantkhaitan said:
			
		

> Friends I will be really grateful if anyone of u can suggest me alternative to common used conio functions like getch(), gotoxy(), kbhit(), _setcursortype() etc.


 well mate , conio.h is a propreitary header file of Borland Inc , you won't find it bundled with ny oher compiler than borland compilers , so it's better you use *Standard C++* n not rely on *compiler-specific* until it's a must ..

better still i sugget you Program in C# , it's much easier to program in it than C++ n it has a vast library of functons n you will hardly ever need to use ny external library .


----------



## kaustav_geek (May 13, 2007)

> better still i sugget you Program in C# , it's much easier to program in it than C++ n it has a vast library of functons n you will hardly ever need to use ny external library .



C# in Linux ? Is _it_ not proprietary too ? In case it isn't.. I don't think Linux has inherent support for C# , right ? 

A side question.... How strong is Python when compared to C++ or C# ?? Give a neutral opinion over it.........


----------



## Zeeshan Quireshi (May 13, 2007)

kaustav_geek said:
			
		

> C# in Linux ? Is _it_ not proprietary too ? In case it isn't.. I don't think Linux has inherent support for C# , right ?
> 
> A side question.... How strong is Python when compared to C++ or C# ?? Give a neutral opinion over it.........


 Python is a scripting language and is not meant for heavy duty System Software development ,

whereas C++/C# are for system level application development .
although C# is a lot easier and as powerful as c++ .

python , Perl , etc r very useful when you wanna do simple tasks n automation like extracting links from web-page , or designing phone-cook kinda software .

also C++/C# give much more performance as they r compiled n Python apps r interpreted .


----------



## anantkhaitan (May 13, 2007)

eh! Friends I should have explained why I need getch() and others..Ok my mistake
Here is my explanation, Take this program for example :

```
int main()
{
int i=0;
char a[]="Help Me! ",ch;
do
{
ch=getch();
printf("%c",a[i%9]);
i++;
}while(ch!=27); // 27 means ESC
return 0;
}
```

Now in this program whatever key u press.. Only "Help me! " string will be displayed.
Help me in writing the same program without getch().

The same is with kbhit() : This function is used a continuous loop iteration until a key is pressed:

```
while(!kbhit())
{
<code>
}
```

now I want a similar thing.


----------



## Sykora (May 13, 2007)

kaustav_geek said:
			
		

> How strong is Python when compared to C++ or C# ?? Give a neutral opinion over it.........



I'll try to be as neutral as I can 

Python lets you do more, and do it faster. By faster, I don't mean that the program itself is faster, but that it is easier to write a program in python because of the ease of the language.

The drawback is of course, that is slower than compiled languages. This is mainly noticeable in number crunching programs and the like. 

The reason why I (and most other Python programmers) prefer python is that the faster program development time is a huge compromise over the performance hit. There are many things you can do in Python that would break your mind in C/C++. Not that it's not possible, just that it's very hard.

Sorry for the off-topic.


----------



## anantkhaitan (May 13, 2007)

Friends avoid quarrel between different programming languages here .. plz help me solving my problem.


----------



## anantkhaitan (May 22, 2007)

So this thread is dying, Guys please help me with my problem..
And if anyone can write the same program in Python which I have written in Post no. 13


----------



## Sykora (May 22, 2007)

```
>>> s = 'Help me! '
>>> i = 0
>>> while True :
...     try :
...             c = raw_input()
...     except KeyboardInterrupt :
...             break
...     print s[i % 9],
...     i += 1
```

I tested for Ctrl-C rather than Esc.


----------

