Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple C++ read file program gives many errors

Status
Not open for further replies.

TheFoxy

Programmer
Joined
Nov 22, 2002
Messages
48
Location
GB
Hey,

Any ideas why the following program doesn't work? It gives this error in MS Visual C++ 6:

Error spawning cl.exe

Andd the following errors in Dev-C++:

[Warning] In function `int main()':
11 no matching function for call to `ifstream::get (char[20])'
124 C:\DEV-CPP\include\g++-3\iostream.h
candidates are: class istream & istream::get(char *, int, char = '\n')
126 C:\DEV-CPP\include\g++-3\iostream.h
class istream & istream::get(unsigned char *, int, char = '\n')
127 C:\DEV-CPP\include\g++-3\iostream.h
class istream & istream::get(char &)
128 C:\DEV-CPP\include\g++-3\iostream.h
class istream & istream::get(unsigned char &)
132 C:\DEV-CPP\include\g++-3\iostream.h
class istream & istream::get(signed char &)
134 C:\DEV-CPP\include\g++-3\iostream.h
class istream & istream::get(signed char *, int, char = '\n')
144 C:\DEV-CPP\include\g++-3\iostream.h
class istream & istream::get(streambuf &, char = '\n')
173 C:\DEV-CPP\include\g++-3\iostream.h
int istream::get()

Source:

Code:
#include <iostream>
#include <fstream>
using namespace std;

int main() 
{
char text[20];

ifstream fileread(&quot;C:\\moo.txt&quot;);

fileread.get(text);
fileread.close();

cout << &quot;Content-type: text/html\n\n<html>\n<body>\n&quot;;
cout << &quot;<p>&quot; << text << &quot;</p>&quot;;
cout << &quot;</body>\n</html>\n&quot;;
return 0;
}
 
this is quick and dirty but it shoul do what you want.
just remember to check that the file exists.

#include <fstream>
#include <iostream>
using namespace std;
int main(int argc, char * argv[]){
if (argc!=2){
cout<<&quot;Usage is: &quot;<< argv[0]<< &quot; <infile>\n&quot;;
return 0;
}
ifstream file;
file.open(argv[1]);
char x;
file.get(x);
cout << &quot;Content-type: text/html\n\n<html>\n<body>\n<p>&quot;;
while (!file.eof()){
if (x!='\n')
cout<<x;
file.get(x);
}
cout << &quot;</p>\n</body>\n</html>\n&quot;;
file.close();
return 0;
}
 
Dev-C++ message is OK. There's no get(x) member function in istream with 1 (char*) type arg. Use getline(...) or read(...) members instead - that's all. istream::get(x)accepts T& only args (see MSDN help). Full VC diagnostic message is more less readable (as usually ;).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top