ifstream on gcc 3.2 problem, simple question

A

Anton Ishmurzin

Greetings All,

I think everybodyknows the answer already. But i
am quite a newbie in c++.

I've got the following line in my code:

ifstream ini_file_in("filename.dat", ios::in);

But, the gcc (GCC) 3.2 spits out some stuff about
deprecated headers:

user@machine:~/prog/src> gcc -Wall extract_data.cpp
In file included from
/usr/include/g++/backward/fstream.h:31,
from extract_data.cpp:3:
/usr/include/g++/backward/backward_warning.h:32:2:
warning: #warning This file includes at least one
deprecated or antiquated header. Please consider
using one of the 32 headers found in section
17.4.1.2 of the C++ standard. Examples include
substituting the <X> header for the <X.h> header
for C++ includes, or <sstream> instead of the
deprecated header <strstream.h>. To disable this
warning use -Wno-deprecated.
extract_data.cpp: In function `int main()':
extract_data.cpp:9: `ios' undeclared (first use
this function)
extract_data.cpp:9: (Each undeclared identifier is
reported only once for each
function it appears in.)
extract_data.cpp:9: parse error before `::' token
extract_data.cpp:11: warning: the address of
`std::ifstream ini_file_in(...)',
will always be `true'

Which include file do i need to allow the ifstream
on this gcc 3.2?

br
Anton.
 
G

Grumble

Anton said:
ifstream ini_file_in("filename.dat", ios::in);

#include <fstream>

ifstream ini_file_in("filename.dat");

The above should work.

If you must specify the mode, my documentation says:

ifstream ini_file_in("filename.dat", ios_base::in);

but it's redundant.
 
T

tom_usenet

Greetings All,

I think everybodyknows the answer already. But i
am quite a newbie in c++.

I've got the following line in my code:

ifstream ini_file_in("filename.dat", ios::in);

But, the gcc (GCC) 3.2 spits out some stuff about
deprecated headers:
Which include file do i need to allow the ifstream
on this gcc 3.2?

You need <fstream> (no .h). But you also need use use namespace std,
the namespace containing the standard libraries. e.g.

#include <fstream>

std::ifstream ini_file_in("filename.dat", std::ios::in);

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
 
J

J. Campbell

Anton Ishmurzin said:
Greetings All,

I think everybodyknows the answer already. But i
am quite a newbie in c++.

Anton...read the warning carefully...it's really quite clear. Use
 
M

Mike Wahler

Grumble said:
#include <fstream>

ifstream ini_file_in("filename.dat");

The above should work.

No it shouldn't.

Make it

std::ifstream ini_file_in("filename.dat");
If you must specify the mode, my documentation says:

ifstream ini_file_in("filename.dat", ios_base::in);

std::ifstream ini_file_in("filename.data", std::ios_bas::in);
but it's redundant.

Correct.

-Mike
 
A

Anton Ishmurzin

Yes, this solves the problem with the obsolete header. But what's wrong
with this code:

ishmurzi@myhost:/home/ishmurzi/newcode> cat extract_data.cpp
#include <iostream> //
#include <fstream> //
using namespace std;

int main() {

ifstream ini_file_in("ini_file", ios::in);

if (! ini_file_in)
{
cerr << "cannot open the ini file" << endl;
exit(1);
}
else
{
cout << "ini-file opened successfully." << endl;
};

cout << "extracting data" << endl;

return 0;
}

if i compile it with icc then it's comiles even with no warnings and remarks.

but if i ask gcc to do the same, it complains about undefined references:

ishmurzi@myhost:/home/ishmurzi/newcode> gcc -Wall extract_data.cpp
/tmp/cczlKupx.o: In function `main':
/tmp/cczlKupx.o(.text+0x26): undefined reference to
`std::basic_ifstream said:
::basic_ifstream[in-charge](char const*, std::_Ios_Openmode)'
/tmp/cczlKupx.o(.text+0x3d): undefined reference to `std::basic_ios<char,
std::char_traits<char> >::eek:perator!() const'
/tmp/cczlKupx.o(.text+0x4c): undefined reference to
`std::basic_ostream<char, std::char_traits<char> >& std::endl<char,
std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
/tmp/cczlKupx.o(.text+0x59): undefined reference to `std::cerr'
/tmp/cczlKupx.o(.text+0x5e): undefined reference to
`std::basic_ostream said:
&, char const*)'
/tmp/cczlKupx.o(.text+0x67): undefined reference to
`std::basic_ostream said:
::eek:perator<<(std::basic_ostream<char, std::char_traits<char> >&
(*)(std::basic_ostream<char, std::char_traits<char> >&))'
/tmp/cczlKupx.o(.text+0x7c): undefined reference to
`std::basic_ostream<char, std::char_traits<char> >& std::endl<char,
std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
/tmp/cczlKupx.o(.text+0x89): undefined reference to `std::cout'
/tmp/cczlKupx.o(.text+0x8e): undefined reference to
`std::basic_ostream said:
&, char const*)'

and so on, but only concerning std:something.

What's going on? Shouldnt i use the "-l" switch? I mean something like

$ gcc -Wall -lsome_lib extract_data.cpp

?

thanks a lot in advance,
Anton.
 
R

Rolf Magnus

Anton said:
Yes, this solves the problem with the obsolete header. But what's
wrong with this code:

ishmurzi@myhost:/home/ishmurzi/newcode> cat extract_data.cpp
#include <iostream> //
#include <fstream> //
using namespace std;

int main() {

ifstream ini_file_in("ini_file", ios::in);

if (! ini_file_in)
{
cerr << "cannot open the ini file" << endl;
exit(1);
}
else
{
cout << "ini-file opened successfully." << endl;
};

cout << "extracting data" << endl;

return 0;
}

if i compile it with icc then it's comiles even with no warnings and
remarks.

but if i ask gcc to do the same, it complains about undefined
references:

ishmurzi@myhost:/home/ishmurzi/newcode> gcc -Wall extract_data.cpp
/tmp/cczlKupx.o: In function `main':
/tmp/cczlKupx.o(.text+0x26): undefined reference to
`std::basic_ifstream said:
::basic_ifstream[in-charge](char const*, std::_Ios_Openmode)'
/tmp/cczlKupx.o(.text+0x3d): undefined reference to
`std::basic_ios<char, std::char_traits<char> >::eek:perator!() const'
/tmp/cczlKupx.o(.text+0x4c): undefined reference to
`std::basic_ostream<char, std::char_traits<char> >& std::endl<char,
std::char_traits<char> >(std::basic_ostream<char,
std::char_traits<char> >&)' /tmp/cczlKupx.o(.text+0x59): undefined
reference to `std::cerr' /tmp/cczlKupx.o(.text+0x5e): undefined
reference to `std::basic_ostream<char, std::char_traits<char> >&
std::operator said:
&, char const*)'
/tmp/cczlKupx.o(.text+0x67): undefined reference to
`std::basic_ostream said:
::eek:perator<<(std::basic_ostream<char, std::char_traits<char> >&
(*)(std::basic_ostream<char, std::char_traits<char> >&))'
/tmp/cczlKupx.o(.text+0x7c): undefined reference to
`std::basic_ostream<char, std::char_traits<char> >& std::endl<char,
std::char_traits<char> >(std::basic_ostream<char,
std::char_traits<char> >&)' /tmp/cczlKupx.o(.text+0x89): undefined
reference to `std::cout' /tmp/cczlKupx.o(.text+0x8e): undefined
reference to `std::basic_ostream<char, std::char_traits<char> >&
std::operator said:
&, char const*)'

and so on, but only concerning std:something.

What's going on?

This is getting compiler specific, but anway. You're using gcc instead
of g++ for your C++ code. When used for linking, that means that the
things needed for C++ (like e.g. the C++ standard library) are not
linked in.
Shouldnt i use the "-l" switch?

No, just use g++ for C++, and gcc for C.
 
A

Anton Ishmurzin

Rolf said:
Anton Ishmurzin wrote:

Yes, this solves the problem with the obsolete header. But what's
wrong with this code:

ishmurzi@myhost:/home/ishmurzi/newcode> cat extract_data.cpp
#include <iostream> //
#include <fstream> //
using namespace std;

int main() {

ifstream ini_file_in("ini_file", ios::in);

if (! ini_file_in)
{
cerr << "cannot open the ini file" << endl;
exit(1);
}
else
{
cout << "ini-file opened successfully." << endl;
};

cout << "extracting data" << endl;

return 0;
}

if i compile it with icc then it's comiles even with no warnings and
remarks.

but if i ask gcc to do the same, it complains about undefined
references:

ishmurzi@myhost:/home/ishmurzi/newcode> gcc -Wall extract_data.cpp
/tmp/cczlKupx.o: In function `main':
/tmp/cczlKupx.o(.text+0x26): undefined reference to
`std::basic_ifstream said:
::basic_ifstream[in-charge](char const*, std::_Ios_Openmode)'
/tmp/cczlKupx.o(.text+0x3d): undefined reference to
`std::basic_ios<char, std::char_traits<char> >::eek:perator!() const'
/tmp/cczlKupx.o(.text+0x4c): undefined reference to
`std::basic_ostream<char, std::char_traits<char> >& std::endl<char,
std::char_traits<char> >(std::basic_ostream<char,
std::char_traits<char> >&)' /tmp/cczlKupx.o(.text+0x59): undefined
reference to `std::cerr' /tmp/cczlKupx.o(.text+0x5e): undefined
reference to `std::basic_ostream<char, std::char_traits<char> >&
std::operator said:
&, char const*)'
/tmp/cczlKupx.o(.text+0x67): undefined reference to
`std::basic_ostream said:
::eek:perator<<(std::basic_ostream<char, std::char_traits<char> >&
(*)(std::basic_ostream<char, std::char_traits<char> >&))'
/tmp/cczlKupx.o(.text+0x7c): undefined reference to
`std::basic_ostream<char, std::char_traits<char> >& std::endl<char,
std::char_traits<char> >(std::basic_ostream<char,
std::char_traits<char> >&)' /tmp/cczlKupx.o(.text+0x89): undefined
reference to `std::cout' /tmp/cczlKupx.o(.text+0x8e): undefined
reference to `std::basic_ostream<char, std::char_traits<char> >&
std::operator said:
&, char const*)'

and so on, but only concerning std:something.

What's going on?


This is getting compiler specific, but anway. You're using gcc instead
of g++ for your C++ code. When used for linking, that means that the
things needed for C++ (like e.g. the C++ standard library) are not
linked in.

Shouldnt i use the "-l" switch?


No, just use g++ for C++, and gcc for C.

Thanks a lot, Rolf. Now it works perfect.
Thanks everybody participated!!!

Anton.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top