C++Primer(3rd)一个iostream问题

C、C++和Java语言
回复
头像
gre_linewer
帖子: 180
注册时间: 2006-11-22 10:57
送出感谢: 0
接收感谢: 0
联系:

C++Primer(3rd)一个iostream问题

#1

帖子 gre_linewer » 2007-05-18 22:01

WordCount.h

代码: 全选

#ifndef WordCount_H
#define WordCount_H

#include <iostream>
#include <vector>
#include <string>
using namespace std;

class Location {
    friend ostream& operator<<( ostream&, const Location& );
public:
    Location( int line, int col )
                        : _line( line ), _col( col ) {}
private:
    short _line;
    short _col;
};

class WordCount {
    friend ostream& operator<<( ostream&, const WordCount& );
    friend istream& operator>>( istream&, WordCount& );

public:
        WordCount() {}
        WordCount( string word ) : _word( word ) {}
        WordCount( string word, int ln, int col )
                : _word( word ){ insert_location( ln, col ); }

        string word()   const { return _word; }
        int    occurs() const { return _occurList.size(); }
        void   found( int ln, int col )
                    { insert_location( ln, col ); }

private:
        void insert_location( int ln, int col )
                { _occurList.push_back( Location( ln, col )); }

        string             _word;
        vector< Location > _occurList;
};

#endif
WordCount.C

代码: 全选

#include "WordCount.h"

ostream& operator <<( ostream& os, const Location& lc )
{
    // output of a Loc object:  < 10,37 >
    os << "<" << lc._line
       << "," << lc._col << "> ";

    return os;
}

ostream&
operator <<( ostream& os, const WordCount& wd )
{
    os << "<" << wd._occurList.size() << "> "
       << wd._word << endl;

    int cnt = 0, onLine = 6;
    vector<Location>::const_iterator first = wd._occurList.begin();
    vector<Location>::const_iterator last  = wd._occurList.end();

    for ( ; first != last; ++first )
    {
        // os << Location
        os << *first << " ";

        // formatting: 6 to a line
        if ( ++cnt >= onLine )
           { os << "\n"; cnt = 0;  }

    }
    return os;
}

istream&
operator >>( istream &is, WordCount &wd )
{
/* format of WordCount object to be read:
 * <2> string
 * <7,3> <12,36>
 */

    int ch;

    /* read in less-than token. if not present
     * place istream in bad state and exit
     */
    if ((ch = is.get()) != '<' )
    {
        // is.setstate( ios_base::badbit );
        return is;
    }

    // read in size value.
    int occurs;
    is >> occurs;

    // grab >; not checking for error
    while ( is && (ch = is.get()) != '>' ) ;

    is >> wd._word;

    // read in the locations;
    // format of each location: < line, col >
    for ( int ix = 0; ix < occurs; ++ix )
    {
        int line, col;

        // extract values
        while (is && (ch = is.get())!= '<' ) ;
        is >> line;

        while (is && (ch = is.get())!= ',' ) ;
        is >> col;

        while (is && (ch = is.get())!= '>' ) ;

        wd._occurList.push_back( Location( line, col ));
    }
    return is;
}



IO20_6.02.C

代码: 全选

#include <fstream>
#include "WordCount.h"
using namespace std;

/**
 **
 ** note: must include the WordCount.C as well as io20_6.02.c:
 **       Stan's Silicon Graphics invocation:
 **
 stanl@john:d.ch20 236 : CC -n32 -experimental io20_6.02.c WordCount.C
 stanl@john:d.ch20 237 : a.out
 stanl@john:d.ch20 238 : cat word.out

 <2> Renoir
 <7,12>  <34,18>

 **
 **/

int main()
{
    // open a file word.out for output
    ofstream oFile( "word.out" );

    if ( ! oFile ) {
         cerr << "unable to open output file: word.out!\n";
         return -1;
    }

    // create and manually set
    WordCount artist( "Renoir" );
    artist.found( 7, 12 ); artist.found( 34, 18 );

    // invokes operator <<(ostream&, const WordCount&);
    oFile << artist<<endl;
}
IO20_6.06.C

代码: 全选

#include <fstream>
#include "WordCount.h"
#include <string>
using namespace std;
/*
 ** as is the case with io20_6.02.c, WordCount.C
 ** must also be compiled with this program ...
 stanl@john:d.ch20 258 : cat word.out

 <2> Renoir
 <7,12>  <34,18>

 stanl@john:d.ch20 259 : a.out

 Read in: <2> Renoir
 <7,12>  <34,18>

 stanl@john:d.ch20 260 : cat word.out

 <2> Renoir
 <7,12>  <34,18>

 <2> Renoir
 <7,12>  <34,18>
 */

int main()
{
    WordCount wd;

    fstream file;
    // ios is preStandard class name
    // file.open( "word.out", ios_base::in );
    file.open( "word.out", ios::in );

    if ( ! file ) {
         cerr << "oops! unable to open input file word.out\n";
         return -1;
    }
   file>>wd;
   cout << "Read in: " << wd << endl;
   file.close();


    // ios_base::out would discard current data
    file.open( "word.out", ios::app );//有问题,word.out追加不上去???
    
    if ( ! file ) {
         cerr << "oops! unable to open input file ''word.out''\n";
         return -1;
    }

    file <<endl<< wd << endl;
    file.close();

}
编译没问题,但运行时
$./IO20_6.02
$ cat word.out
<2> Renoir
<7,12> <34,18>
$ ./IO20_6.06
Read in: <2> Renoir
<7,12> <34,18>
oops! unable to open input file ''word.out''

应该是再次打开word.out时出的问题,是不是ios::app出了出问题?想了想好久也没结果.........[/code]
Ubuntu 8.04
IBM R52-18465DC
CPU:PM 1.73Ghz DDR2:768MB 533Mhz
INTEL 915GM/915PM
ATI Mobility Radeon X300 64MB
40G SATA
头像
gre_linewer
帖子: 180
注册时间: 2006-11-22 10:57
送出感谢: 0
接收感谢: 0
联系:

#2

帖子 gre_linewer » 2007-05-19 21:15

还有人晓得啊
Ubuntu 8.04
IBM R52-18465DC
CPU:PM 1.73Ghz DDR2:768MB 533Mhz
INTEL 915GM/915PM
ATI Mobility Radeon X300 64MB
40G SATA
Simon_Ghost
帖子: 39
注册时间: 2006-06-30 17:42
送出感谢: 0
接收感谢: 0
联系:

#3

帖子 Simon_Ghost » 2007-05-20 19:31

整个应该去csdn上问
头像
antonym55
帖子: 353
注册时间: 2007-04-03 9:52
送出感谢: 0
接收感谢: 0
联系:

#4

帖子 antonym55 » 2007-05-20 21:20

加上ios::out 或者用 ofstream 试试

1. ios::out
file.open( "word.out", ios::out | ios::app );
2.ofstream
ofstream myfile;
myfile.open( "word.out", ios::app);
TODO LIST:
1.valgrind:
2.gcov:
3.cppuint:未在Linux中使用
4.PostgreSQL:unixODBC
5.Doxygen:UTF8 中文问题,生成LaTex文档问题
6.LaTex:明确安装问题,UTF8 中文字体问题
头像
gre_linewer
帖子: 180
注册时间: 2006-11-22 10:57
送出感谢: 0
接收感谢: 0
联系:

#5

帖子 gre_linewer » 2007-05-20 22:17

right.
赞ls的
还能简单解释一下刚开始错的原因:为啥iso::app就不行?
另外:fstream 不是有ofstream“具有”的功能么?
Ubuntu 8.04
IBM R52-18465DC
CPU:PM 1.73Ghz DDR2:768MB 533Mhz
INTEL 915GM/915PM
ATI Mobility Radeon X300 64MB
40G SATA
回复

回到 “C/C++/Java”