std::istream
typedef basic_istream<char> istream;
An input stream object can read and interpret input from character sequences. Specific members are provided to perform these input operations (see functions below).
A standard object cin
is an object of this type.
std::istream::getline
istream& getline (char* s, streamsize n );
istream& getline (char* s, streamsize n, char delim );
Extracts characters from the stream as unformatted input and stores them as c
a string s
into until the extracted characters are delimiting characters, n
or characters have been written s
(including the terminating null character).
// istream::getline example
#include <iostream> // std::cin, std::cout
int main () {
char name[256], title[256];
std::cout << "Please, enter your name: ";
std::cin.getline (name,256);
std::cout << "Please, enter your favourite movie: ";
std::cin.getline (title,256);
std::cout << name << "'s favourite movie is " << title;
return 0;
}
Please, enter your name: sjn
Please, enter your favourite movie: op
sjn's favourite movie is op
This example shows how to cin
get lines from the standard input stream ( )
std::ostream
typedef basic_ostream<char> ostream;
Output stream objects can write sequences of characters and represent other types of data. Specific members are provided to perform these output operations (see functions below).
Standard objects cout
, cerr
and clog
are objects of this type.
std::ostream::operator<<
arithmetic types (1)
ostream& operator<< (bool val);
ostream& operator<< (short val);
ostream& operator<< (unsigned short val);
ostream& operator<< (int val);
ostream& operator<< (unsigned int val);
ostream& operator<< (long val);
ostream& operator<< (unsigned long val);
ostream& operator<< (float val);
ostream& operator<< (double val);
ostream& operator<< (long double val);
ostream& operator<< (void* val);
stream buffers (2)
ostream& operator<< (streambuf* sb );
manipulators (3)
ostream& operator<< (ostream& (*pf)(ostream&));
ostream& operator<< (ios& (*pf)(ios&));
ostream& operator<< (ios_base& (*pf)(ios_base&));
// example on insertion
#include <iostream> // std::cout, std::right, std::endl
#include <iomanip> // std::setw
int main() {
int val = 65;
std::cout << std::right; // right-adjusted (manipulator)
std::cout << std::setw(10); // set width (extended manipulator)
std::cout << val << std::endl; // multiple insertions
return 0;
}
output
65
C++ reads txt files
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#include <cassert>
using namespace std;
void txt_to_vectordouble(vector<float>& res, string pathname)
{
ifstream infile;
infile.open(pathname.data()); //将文件流对象与文件连接起来
assert(infile.is_open()); //若失败,则输出错误消息,并终止程序运行
string s;
while (getline(infile, s)) {
istringstream is(s); //将读出的一行转成数据流进行操作
double d;
while (!is.eof()) {
is >> d;
res.push_back(d);
}
s.clear();
}
infile.close(); //关闭文件输入流
}
int main()
{
vector<float> data;
txt_to_vectordouble(data, "./1.txt");
for (auto a : data) {
cout << a << endl;
}
return 0;
}