提交 432bb0aa 编写于 作者: J jzplp

17.37-18.7

上级 0927e6b1
#include<iostream>
#include<fstream>
int main()
{
char cs[10];
std::ifstream ifs("C:\\Users\\jz\\Desktop\\test.txt");
while(ifs)
{
ifs.getline(cs, 9, '\n');
std::cout << "line len: " << ifs.gcount() << std::endl;
std::cout << cs << std::endl;
}
return 0;
}
egawrgee
yebsevey
esbhrtbh
ybse
te
us6r
ryebseveyrayeg
jbur
djvbs
\ No newline at end of file
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
int main()
{
std::fstream inOut("C:\\Users\\jz\\Desktop\\test.txt", std::fstream::ate | std::fstream::in | std::fstream::out);
if(!inOut)
{
std::cout << "Unable to open file!" << std::endl;
return EXIT_FAILURE;
}
std::fstream::pos_type end_mark = inOut.tellg();
inOut.seekg(0, std::fstream::beg);
std::size_t cnt = 0;
std::string line;
while(inOut && inOut.tellg() != end_mark && std::getline(inOut, line))
{
cnt += line.size() + 1;
std::fstream::pos_type mark = inOut.tellg();
inOut.seekp(0, std::fstream::end);
inOut << cnt;
if(mark != end_mark)
inOut << " ";
inOut.seekg(mark);
}
inOut.seekp(0, std::fstream::end);
inOut << "\n";
return 0;
}
egawrgee
yebsevey
esbhrtbhfeef
ybse
tefEGWGE
us6r
ryebseveyrayeg
jbur
djvbs
......@@ -165,3 +165,14 @@ std::bitset<100> b1;
[17.36 程序代码](17.36.cpp)
* **练习17.37**
getline读取文件程序
[17.37 wordConversion.cpp程序代码](17.37/17.37.cpp)
[17.37 test.txt 文本测试文件](17.37/test.txt)
* **练习17.38**
已经在练习17.37中实现
* **练习17.39**
在文件最后写入每行位置程序
[17.39 seekLine.cpp程序代码](17.39/seekLine.cpp)
[17.39 test.txt 文本测试文件](17.39/test.txt)
#ifndef BLOB_H
#define BLOB_H
#include <vector>
#include <memory>
#include <initializer_list>
#include <string>
#include <new>
#include <iostream>
#include <cstdlib>
template <typename T> class Blob;
template <typename T> class BlobPtr;
template <typename T> class ConstBlobPtr;
template <typename T>
bool operator==(const Blob<T> &, const Blob<T> &);
template <typename T>
bool operator!=(const Blob<T> &, const Blob<T> &);
template <typename T>
bool operator<(const Blob<T> &, const Blob<T> &);
template <typename T>
bool operator<=(const Blob<T> &, const Blob<T> &);
template <typename T>
bool operator>(const Blob<T> &, const Blob<T> &);
template <typename T>
bool operator>=(const Blob<T> &, const Blob<T> &);
void haneld_out_of_memory();
template <typename T>
class Blob
{
friend class BlobPtr<T>;
friend class ConstBlobPtr<T>;
friend bool operator== <T>(const Blob<T> &, const Blob<T> &);
friend bool operator< <T>(const Blob<T> &, const Blob<T> &);
friend bool operator> <T>(const Blob<T> &, const Blob<T> &);
public:
typedef T value_type;
typedef typename std::vector<T>::size_type size_type;
Blob();
Blob(std::initializer_list<T> il);
template <typename It>
Blob(It b, It e);
Blob(const Blob & sb);
Blob(Blob && sb);
Blob & operator=(const Blob & rhs);
Blob & operator=(Blob && rhs);
T & back();
const T & back() const;
T & front();
const T & front() const;
T & operator[](size_type i);
const T & operator[](std::size_t) const;
size_type size() const { return data->size(); }
bool empty() const { return data->empty(); }
void push_back(const T &t) { data->push_back(t); }
void push_back(T && t) { data->push_back(std::move(t)); }
void pop_back();
BlobPtr<T> begin();
BlobPtr<T> end();
ConstBlobPtr<T> begin() const;
ConstBlobPtr<T> end() const;
ConstBlobPtr<T> cbegin() const;
ConstBlobPtr<T> cend() const;
private:
std::shared_ptr<std::vector<T>> data;
void check(size_type i, const std::string &msg) const;
};
template <typename T>
BlobPtr<T> operator+(const BlobPtr<T> & bp, int i);
template <typename T>
BlobPtr<T> operator-(const BlobPtr<T> & bp, int i);
template <typename T>
int operator-(const BlobPtr<T> & lhs, const BlobPtr<T> & rhs);
template <typename T>
bool operator==(const BlobPtr<T> &lhs, const BlobPtr<T> &rhs);
template <typename T>
bool operator!=(const BlobPtr<T> &lhs, const BlobPtr<T> &rhs);
template <typename T>
bool operator<(const BlobPtr<T> &lhs, const BlobPtr<T> &rhs);
template <typename T>
bool operator>(const BlobPtr<T> &lhs, const BlobPtr<T> &rhs);
template <typename T>
bool operator<=(const BlobPtr<T> &lhs, const BlobPtr<T> &rhs);
template <typename T>
bool operator>=(const BlobPtr<T> &lhs, const BlobPtr<T> &rhs);
template <typename T>
class BlobPtr
{
friend BlobPtr<T> operator+ <T>(const BlobPtr<T> & bp, int i);
friend BlobPtr<T> operator- <T>(const BlobPtr<T> & bp, int i);
friend bool operator== <T>(const BlobPtr<T> &lhs, const BlobPtr<T> &rhs);
friend bool operator< <T>(const BlobPtr<T> &lhs, const BlobPtr<T> &rhs);
friend bool operator> <T>(const BlobPtr<T> &lhs, const BlobPtr<T> &rhs);
friend int operator- <T>(const BlobPtr<T> & lhs, const BlobPtr<T> & rhs);
public:
BlobPtr() : curr(0) { }
BlobPtr(Blob<T> &a, std::size_t sz = 0) try : wptr(a.data), curr(sz) { }
catch(const std::bad_alloc & e) { haneld_out_of_memory(); }
T & operator[](std::size_t n) const;
T & operator*() const;
T * operator->() const;
BlobPtr & operator++();
BlobPtr & operator--();
BlobPtr operator++(int);
BlobPtr operator--(int);
BlobPtr & operator+=(int i);
BlobPtr & operator-=(int i);
private:
std::shared_ptr<std::vector<T>> check(std::size_t, const std::string &) const;
std::weak_ptr<std::vector<T>> wptr;
std::size_t curr;
};
template <typename T>
ConstBlobPtr<T> operator+(const ConstBlobPtr<T> & bp, int i);
template <typename T>
ConstBlobPtr<T> operator-(const ConstBlobPtr<T> & bp, int i);
template <typename T>
int operator-(const ConstBlobPtr<T> & lhs, const ConstBlobPtr<T> & rhs);
template <typename T>
bool operator==(const ConstBlobPtr<T> &lhs, const ConstBlobPtr<T> &rhs);
template <typename T>
bool operator!=(const ConstBlobPtr<T> &lhs, const ConstBlobPtr<T> &rhs);
template <typename T>
bool operator<(const ConstBlobPtr<T> &lhs, const ConstBlobPtr<T> &rhs);
template <typename T>
bool operator>(const ConstBlobPtr<T> &lhs, const ConstBlobPtr<T> &rhs);
template <typename T>
bool operator<=(const ConstBlobPtr<T> &lhs, const ConstBlobPtr<T> &rhs);
template <typename T>
bool operator>=(const ConstBlobPtr<T> &lhs, const ConstBlobPtr<T> &rhs);
template <typename T>
class ConstBlobPtr
{
friend ConstBlobPtr<T> operator+ <T>(const ConstBlobPtr<T> & bp, int i);
friend ConstBlobPtr<T> operator- <T>(const ConstBlobPtr<T> & bp, int i);
friend bool operator== <T>(const ConstBlobPtr<T> &lhs, const ConstBlobPtr<T> &rhs);
friend bool operator< <T>(const ConstBlobPtr<T> &lhs, const ConstBlobPtr<T> &rhs);
friend bool operator> <T>(const ConstBlobPtr<T> &lhs, const ConstBlobPtr<T> &rhs);
friend int operator- <T>(const ConstBlobPtr<T> & lhs, const ConstBlobPtr<T> & rhs);
public:
ConstBlobPtr() : curr(0) { }
ConstBlobPtr(const Blob<T> &a, std::size_t sz = 0) try : wptr(a.data), curr(sz) { }
catch(const std::bad_alloc & e) { haneld_out_of_memory(); }
const T & operator[](std::size_t n) const;
const T & operator*() const;
const T * operator->() const;
ConstBlobPtr & operator++();
ConstBlobPtr & operator--();
ConstBlobPtr operator++(int);
ConstBlobPtr operator--(int);
ConstBlobPtr & operator+=(int i);
ConstBlobPtr & operator-=(int i);
private:
std::shared_ptr<std::vector<T>> check(std::size_t, const std::string &) const;
std::weak_ptr<std::vector<T>> wptr;
std::size_t curr;
};
template <typename T>
void Blob<T>::check(size_type i,const std::string &msg) const
{
if(i >= data->size())
throw std::out_of_range(msg);
}
template <typename T>
T & Blob<T>::back()
{
check(0, "back on empty Blob");
return data->back();
}
template <typename T>
const T & Blob<T>::back() const
{
check(0, "back on empty Blob");
return data->back();
}
template <typename T>
T & Blob<T>::front()
{
check(0, "front on empty Blob");
return data->front();
}
template <typename T>
const T & Blob<T>::front() const
{
check(0, "front on empty Blob");
return data->front();
}
template <typename T>
T & Blob<T>::operator[](size_type i)
{
check(i, "subscript out of range");
return (*data)[i];
}
template <typename T>
const T & Blob<T>::operator[](std::size_t i) const
{
check(i, "subscript out of range");
return (*data)[i];
}
template <typename T>
void Blob<T>::pop_back()
{
check(0, "pop_back on empty Blob");
data->pop_back();
}
template <typename T>
Blob<T>::Blob() : data(std::make_shared<std::vector<T>>()) { }
template <typename T>
Blob<T>::Blob(std::initializer_list<T> il) try : data(std::make_shared<std::vector<T>>(il)) { }
catch(const std::bad_alloc & e)
{
haneld_out_of_memory();
}
template <typename T>
template <typename It>
Blob<T>::Blob(It b, It e) try : data(std::make_shared<std::vector<T>>(b, e)) { }
catch(const std::bad_alloc & err)
{
haneld_out_of_memory();
}
template <typename T>
Blob<T>::Blob(const Blob & sb) : data(std::make_shared<std::vector<T>>(*(sb.data))) { }
template <typename T>
Blob<T>::Blob(Blob && sb) : data(std::move(sb.data)) { }
template <typename T>
Blob<T> & Blob<T>::operator=(const Blob & rhs)
{
data = std::make_shared<std::vector<T>>(*(rhs.data));
return *this;
}
template <typename T>
Blob<T> & Blob<T>::operator=(Blob && rhs)
{
data = std::move(rhs.data);
return *this;
}
template <typename T>
BlobPtr<T> Blob<T>::begin()
{
return BlobPtr<T>(*this);
}
template <typename T>
BlobPtr<T> Blob<T>::end()
{
return BlobPtr<T>(*this, data->size());
}
template <typename T>
ConstBlobPtr<T> Blob<T>::begin() const
{
return ConstBlobPtr<T>(*this);
}
template <typename T>
ConstBlobPtr<T> Blob<T>::end() const
{
return ConstBlobPtr<T>(*this, data->size());
}
template <typename T>
ConstBlobPtr<T> Blob<T>::cbegin() const
{
return ConstBlobPtr<T>(*this);
}
template <typename T>
ConstBlobPtr<T> Blob<T>::cend() const
{
return ConstBlobPtr<T>(*this, data->size());
}
template <typename T>
std::shared_ptr<std::vector<T>> BlobPtr<T>::check(std::size_t i, const std::string & msg) const
{
std::shared_ptr<std::vector<T>> ret = wptr.lock();
if(!ret)
throw std::runtime_error("unbound BlobPtr");
if(i >= ret->size())
throw std::out_of_range(msg);
return ret;
}
template <typename T>
T & BlobPtr<T>::operator*() const
{
std::shared_ptr<std::vector<T>> p = check(curr, "dereference past end");
return (*p)[curr];
}
template <typename T>
T * BlobPtr<T>::operator->() const
{
return this->operator*();
}
template <typename T>
BlobPtr<T> BlobPtr<T>::operator++(int)
{
BlobPtr ret = *this;
++*this;
return ret;
}
template <typename T>
BlobPtr<T> BlobPtr<T>::operator--(int)
{
BlobPtr ret = *this;
--*this;
return ret;
}
template <typename T>
BlobPtr<T> & BlobPtr<T>::operator++()
{
check(curr, "increment past end of BlobPtr");
++curr;
return *this;
}
template <typename T>
BlobPtr<T> & BlobPtr<T>::operator--()
{
--curr;
check(curr, "decrement past begin of BlobPtr");
return *this;
}
template <typename T>
BlobPtr<T> & BlobPtr<T>::operator+=(int i)
{
curr += i;
return *this;
}
template <typename T>
BlobPtr<T> & BlobPtr<T>::operator-=(int i)
{
curr -= i;
return *this;
}
template <typename T>
BlobPtr<T> operator+(const BlobPtr<T> & bp, int i)
{
BlobPtr<T> ret = bp;
ret += i;
return ret;
}
template <typename T>
BlobPtr<T> operator-(const BlobPtr<T> & bp, int i)
{
BlobPtr<T> ret = bp;
ret -= i;
return ret;
}
template <typename T>
int operator-(const BlobPtr<T> & lhs, const BlobPtr<T> & rhs)
{
return lhs.curr - rhs.curr;
}
template <typename T>
T & BlobPtr<T>::operator[](std::size_t n) const
{
std::shared_ptr<std::vector<T>> p = check(curr + n, "dereference past end");
return (*p)[curr + n];
}
template <typename T>
std::shared_ptr<std::vector<T>> ConstBlobPtr<T>::check(std::size_t i, const std::string & msg) const
{
std::shared_ptr<std::vector<T>> ret = wptr.lock();
if(!ret)
throw std::runtime_error("unbound BlobPtr");
if(i >= ret->size())
throw std::out_of_range(msg);
return ret;
}
template <typename T>
const T & ConstBlobPtr<T>::operator*() const
{
std::shared_ptr<std::vector<T>> p = check(curr, "dereference past end");
return (*p)[curr];
}
template <typename T>
const T * ConstBlobPtr<T>::operator->() const
{
return this->operator*();
}
template <typename T>
ConstBlobPtr<T> ConstBlobPtr<T>::operator++(int)
{
ConstBlobPtr ret = *this;
++*this;
return ret;
}
template <typename T>
ConstBlobPtr<T> ConstBlobPtr<T>::operator--(int)
{
ConstBlobPtr ret = *this;
--*this;
return ret;
}
template <typename T>
ConstBlobPtr<T> & ConstBlobPtr<T>::operator++()
{
check(curr, "increment past end of BlobPtr");
++curr;
return *this;
}
template <typename T>
ConstBlobPtr<T> & ConstBlobPtr<T>::operator--()
{
--curr;
check(curr, "decrement past begin of BlobPtr");
return *this;
}
template <typename T>
ConstBlobPtr<T> & ConstBlobPtr<T>::operator+=(int i)
{
curr += i;
return *this;
}
template <typename T>
ConstBlobPtr<T> & ConstBlobPtr<T>::operator-=(int i)
{
curr -= i;
return *this;
}
template <typename T>
ConstBlobPtr<T> operator+(const ConstBlobPtr<T> & bp, int i)
{
ConstBlobPtr<T> ret = bp;
ret += i;
return ret;
}
template <typename T>
ConstBlobPtr<T> operator-(const ConstBlobPtr<T> & bp, int i)
{
ConstBlobPtr<T> ret = bp;
ret -= i;
return ret;
}
template <typename T>
int operator-(const ConstBlobPtr<T> & lhs, const ConstBlobPtr<T> & rhs)
{
return lhs.curr - rhs.curr;
}
template <typename T>
const T & ConstBlobPtr<T>::operator[](std::size_t n) const
{
std::shared_ptr<std::vector<T>> p = check(curr + n, "dereference past end");
return (*p)[curr + n];
}
template <typename T>
bool operator==(const Blob<T> & lhs, const Blob<T> & rhs)
{
return *(lhs.data) == *(rhs.data);
}
template <typename T>
bool operator!=(const Blob<T> & lhs, const Blob<T> & rhs)