PDA

View Full Version : File handling using GCC/G++


anantkhaitan
10-05-2007, 06:37 PM
Friends point me where I m wrong.

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


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
10-05-2007, 07:32 PM
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
10-05-2007, 07:57 PM
^^^
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
10-05-2007, 09:49 PM
Once you've used the namespace, the std:: is not necessary.

anantkhaitan
10-05-2007, 10:39 PM
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()
{
ofstream f;
f.open("1.txt");
f.put(65);
cout<<"Inserted";
f.close();
return 1;
}

anantkhaitan
12-05-2007, 02:34 PM
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
12-05-2007, 10:17 PM
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
12-05-2007, 11:33 PM
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
12-05-2007, 11:37 PM
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
13-05-2007, 10:04 AM
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
13-05-2007, 12:28 PM
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
13-05-2007, 01:03 PM
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
13-05-2007, 01:04 PM
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
13-05-2007, 01:35 PM
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
13-05-2007, 01:48 PM
Friends avoid quarrel between different programming languages here .. plz help me solving my problem. ;)

anantkhaitan
22-05-2007, 02:25 PM
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
22-05-2007, 04:19 PM
>>> 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.