Mail Archives: djgpp/2000/01/30/14:30:41
Manni Heumann wrote:
>
> Standard headers in C++ don't come with a suffix.
> libgpp should not be necessary for C++, you only need it to compile legacy
> code (e.g. to compile programs that use the String class).
>
> But what sort of functionality should <ostream> contain?
It opens a file for output. from my text books it seems to be part of
the standard library.
AFAIK, that measn it should be in djgpp and I probably lost it. It
would be nice if someone could simply verify whether or not <ostream is
supposed to be in dgjpp or not.
> AFAIK once you include <fstream> you don't have to worry about the subclasses.
> Try to change the #include lines below with this:
> #include <fstream>
> #include <string>
>
> I bet it will compile then.
Those headers are already included. If I just erase "#include <ostream>"
the program compiles but doesn't work correctly. I get a message telling
me a file failed to open.
The program is pretty short, so here it is: I'm trying to learn how to
open and manipulate files:
// liner.cpp
// Example of reading and writing text files and
// defining personal iomanipulators
#include <iostream>
#include <fstream>
#include <ostream>
#include <string>
#ifndef MAXPATHLEN
const int MAXPATHLEN = 255;
#endif
const std::string PROG = "liner";
const int MAJOR = 0;
const int MINOR = 1;
std::ostream &
Prog ( std::ostream & stream )
{
stream << "\n"
<< PROG << " Version: " << MAJOR << "." << MINOR
<< std::endl;
return (stream);
}
std::ostream &
Sepline ( std::ostream & stream )
{
stream <<
"------------------------------------------------------------------"
<< std::endl;
return (stream);
}
int
main ( int argc, char * argv[] )
{
char inputfile[MAXPATHLEN] = {0};
char outputfile[MAXPATHLEN] = {0};
if ( argc < 2 )
{
std::cerr << Prog << Sepline
<< "Syntax: " << PROG << " inputfile <outputfile>" <<
std::endl;
return(1);
}
strcpy ( inputfile, argv[1] );
std::ifstream input( inputfile, std::ios::in );
std::ofstream output;
if ( input.is_open() == false )
{
std::cerr << Prog << Sepline
<< "Failed to open: " << inputfile << std::endl;
return(2);
}
std::cout << Prog;
if ( argv[2] )
{
strcpy ( outputfile, argv[2] );
output.open ( outputfile, std::ios::out );
if ( output.bad() )
{
std::cerr << Sepline
<< "Failed to open: " << outputfile << std::endl;
return(3);
}
else
{
std::cout << "Output File: " << outputfile << std::endl;
output << Prog;
output << Sepline;
}
}
std::cout << Sepline;
char buffer[MAXPATHLEN] = {0};
int counter = 0;
int bytes = 0;
while ( !input.eof() )
{
input.getline ( buffer, MAXPATHLEN );
bytes += strlen(buffer);
counter++;
std::cout << counter << "\t| " << buffer << std::endl;
if ( output.good() )
{
output << counter << "\t| " << buffer << std::endl;
}
}
std::cout << Sepline;
std::cout << "Lines in file: " << counter << std::endl;
std::cout << "Bytes in file: " << bytes << std::endl;
if ( output.good() )
{
output << Sepline;
output << "Lines in file: " << counter << std::endl;
output << "Bytes in file: " << bytes << std::endl;
}
output.close();
input.close();
return(0);
}
- Raw text -