1个回答
展开全部
// istream::getline & istream::read example
#include <iostream> // std::cin, std::cout
int main () {
const int max_line_len = 4096;
char buffer[max_line_len];
std::ifstream 陵态袭is 尺兄("test.txt", std::ifstream::binary);
闭滑if (is) {
// read line by line
while(is.getline(buffer,max_line_len)) // get a line to buffer
{
std::cout << buffer << std::endl;
}
is.seekg(0,is::beg); // back to file begin
// read data as binary
while(!is.eof())
{
is.read (buffer,length); // read length of bits to buffer.
std::cout.write(buffer,length); // output use cout
}
is.close();
}
return 0;
}
追问
那帮我能做一个排序嘛?
追答
// istream::getline & istream::read example
#include <iostream> // std::cin, std::cout
#include <fstream> // std::ifstream, std::ofstream
#include <vector> // std::vector
#include <algorithm>
typedef std::vector<string> StringVec;
typedef StringVec::iterator StringIter;
int main () {
const int max_line_len = 4096;
StringVec string_vec(0);
char buffer[max_line_len];
std::ifstream is ("test.txt", std::ifstream::binary);
std::ofstream os ("ouput.txt", std::ifstream::binary);
if (is&&os) {
// read line by line
while(is.getline(buffer,max_line_len)) // get a line to buffer
{
std::cout << buffer << std::endl;
string_vec.push_back(string(buffer));
}
std::sort(string_vec.begin(),string_vec.end()); // sort
for( StringIter iter = string_vec.begin(); iter != string_vec.end(); ++iter)
{
os << *iter << std::endl;
}
is.close();
os.close();
}
return 0;
}
sort: http://www.cplusplus.com/reference/algorithm/sort/?kw=sort
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询