File handling using GCC/G++

Status
Not open for further replies.

anantkhaitan

Burning Bright
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

Youngling
u forgot to give namespace buddy!!!
this code will compile..
Code:
#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

1337 |)00|) \m/
^^^
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
Code:
using namespace std;

is a standard..
Don't forget it.
 
OP
anantkhaitan

anantkhaitan

Burning Bright
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;
}
 
OP
anantkhaitan

anantkhaitan

Burning Bright
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.
 

webgenius

Ambassador of Buzz
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

Debian Rocks!
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

C# Be Sharp !
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

1337 |)00|) \m/
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

C# Be Sharp !
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 .
 
OP
anantkhaitan

anantkhaitan

Burning Bright
eh! Friends I should have explained why I need getch() and others..Ok my mistake
Here is my explanation, Take this program for example :
Code:
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:
Code:
while(!kbhit())
{
<code>
}

now I want a similar thing.
 

Sykora

I see right through you.
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.
 
OP
anantkhaitan

anantkhaitan

Burning Bright
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

I see right through you.
Code:
>>> 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.
 
Status
Not open for further replies.
Top Bottom