Connecting Tech Pros Worldwide Forums | Help | Site Map

How to print the strings reading from file

Ram Laxman
Guest
 
Posts: n/a
#1: Jul 22 '05
Hi all,
The below code doesnot print the string reading from the file?
error C2679: binary '<<' : no operator defined which takes a
right-hand operand of type 'class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> >' (or there is no
acceptable conversion)
Error executing cl.exe.

Does anybody know how to fix it.Iam using VC++ 6.0.

#include<fstream>
#include<ios>
#include <iostream.h>
#include<string>
#include<iomanip>
#include<sstream>
#include<vector>
#include<algorithm>
#include<stdio.h>
#include <fcntl.h>
#include <io.h>
#include <cstdlib>
#include <cstring>


const char *filename ="C:\\result.txt";
//std::ostream &operator<<(std::ostream &os, const mypair &p)

using namespace std;

int main()
{

string tok1;
int result;
string s1,s2("a");
vector<int> v1;

int status;
status = _open(filename,_O_RDONLY);
if(status == -1)
{
printf("Couldnot able to Open file ");
}
else
{
printf("Opening of file Successful");
}

ifstream ifs(filename); // Open for reading
ofstream out("C:\divert.txt"); // Open for writing
string s,line;
while(getline(ifs, s)) // Discards newline char
{

// cout << s ; // ... must add it back
s += line + "\n";
// printf("%s",s);
cout << s << endl;
}

David Harmon
Guest
 
Posts: n/a
#2: Jul 22 '05

re: How to print the strings reading from file


On 9 Feb 2004 23:22:04 -0800 in comp.lang.c++, ram_laxman@india.com (Ram
Laxman) was alleged to have written:[color=blue]
>Does anybody know how to fix it.Iam using VC++ 6.0.
>
>#include<fstream>
>#include<ios>
>#include <iostream.h>[/color]

Never use
#include <iostream.h>
It belongs to an old, obsolete, bad version of the io library that
will make you crazy.

Instead use
#include <iostream>

This will go along very happily with the
using namespace std.
that you already have.

The same thing applies with less emphasis to the other .h C++ includes.

John Harrison
Guest
 
Posts: n/a
#3: Jul 22 '05

re: How to print the strings reading from file



"Ram Laxman" <ram_laxman@india.com> wrote in message
news:24812e22.0402092322.361bd090@posting.google.c om...[color=blue]
> Hi all,
> The below code doesnot print the string reading from the file?
> error C2679: binary '<<' : no operator defined which takes a
> right-hand operand of type 'class std::basic_string<char,struct
> std::char_traits<char>,class std::allocator<char> >' (or there is no
> acceptable conversion)
> Error executing cl.exe.
>
> Does anybody know how to fix it.Iam using VC++ 6.0.
>
> #include<fstream>
> #include<ios>
> #include <iostream.h>[/color]

Here's your error, <iostream>, no .h

john


Jonathan Turkanis
Guest
 
Posts: n/a
#4: Jul 22 '05

re: How to print the strings reading from file



"David Harmon" <source@netcom.com> wrote in message
news:40428ac6.45590726@news.west.earthlink.net...[color=blue]
> On 9 Feb 2004 23:22:04 -0800 in comp.lang.c++, ram_laxman@india.com[/color]
(Ram[color=blue]
> Laxman) was alleged to have written:[color=green]
> >Does anybody know how to fix it.Iam using VC++ 6.0.
> >
> >#include<fstream>
> >#include<ios>
> >#include <iostream.h>[/color]
>
> Never use
> #include <iostream.h>
> It belongs to an old, obsolete, bad version of the io library that
> will make you crazy.[/color]

Well, it was okay in its day. Some day there will be an 'oldies'
revival. But if you have to use the old headers for some reason, you
can't mix them with the new ones, such as <fstream>.
[color=blue]
>
> Instead use
> #include <iostream>
>
> This will go along very happily with the
> using namespace std.
> that you already have.
>
> The same thing applies with less emphasis to the other .h C++[/color]
includes.[color=blue]
>[/color]

It's not quite the same. <iostream.h> and its cousins are prestandard
headers which have been completely replaced by <iostream>, etc. The
headers from the C standard librarary, ending in '.h', such as
<stdio.h>, are part of the C++ standard library, and are perfectly
okay to use. Unless you have some good reason, however, its better to
use the versions prefixed by 'c', such as <cstdio>.

Jonathan


Frank Schmitt
Guest
 
Posts: n/a
#5: Jul 22 '05

re: How to print the strings reading from file


ram_laxman@india.com (Ram Laxman) writes:
[color=blue]
> Hi all,
> The below code doesnot print the string reading from the file?
> error C2679: binary '<<' : no operator defined which takes a
> right-hand operand of type 'class std::basic_string<char,struct
> std::char_traits<char>,class std::allocator<char> >' (or there is no
> acceptable conversion)
> Error executing cl.exe.
>
> Does anybody know how to fix it.Iam using VC++ 6.0.
>
> #include<fstream>
> #include<ios>
> #include <iostream.h>
> #include<string>
> #include<iomanip>
> #include<sstream>
> #include<vector>
> #include<algorithm>
> #include<stdio.h>
> #include <fcntl.h>
> #include <io.h>
> #include <cstdlib>
> #include <cstring>[/color]

- Don't use non-standard-includes (e.g. <iostream.h>)
- Do you really need all of these?
- You forgot to #include <string>

HTH & kind regards
frank

--
Frank Schmitt
quattro research GmbH
e-mail: schmitt NO at SPAM quattro-research !@! dot com
David Harmon
Guest
 
Posts: n/a
#6: Jul 22 '05

re: How to print the strings reading from file


On Tue, 10 Feb 2004 01:16:18 -0700 in comp.lang.c++, "Jonathan Turkanis"
<technews@kangaroologic.com> was alleged to have written:[color=blue]
>Well, it was okay in its day. Some day there will be an 'oldies'
>revival. But if you have to use the old headers for some reason, you
>can't mix them with the new ones, such as <fstream>.[/color]

Or as in this case, <string>

Closed Thread