How to print the strings reading from file

R

Ram Laxman

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::eek:stream &operator<<(std::eek:stream &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;
}
 
D

David Harmon

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

#include<fstream>
#include<ios>
#include <iostream.h>

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.
 
J

John Harrison

Ram Laxman said:
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>

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

john
 
J

Jonathan Turkanis

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

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 said:
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.

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
 
F

Frank Schmitt

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>

- 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
 
D

David Harmon

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

Or as in this case, <string>
 

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top