1. ifstream和ofstream
#include<iostream>
#include<fstream>
int main()
{
//std::ofstream::in File open for reading
//std::ofstream::out File open for writing
//std::ofstream::binary Operations are performed in binary mode rather than text
//std::ofstream::ate The output position starts at the end of the file.
//std::ofstream::app append
//std::ofstream::truncate truncate,Any contents that existed in the file before it is open are discarded.
std::ofstream ofs("test.txt", std::ofstream::out | std::ofstream::app );
//Returns true if either (or both) the failbit or the badbit error state flags is set for the stream.
if (ofs.fail())
{
std::cout << "ofs: open file failed" << std::endl;
exit(-1);
}
ofs << "song:" << std::endl << "name:" << "my songs know what you did in the dark" << std::endl;
ofs.close();
std::ifstream ifs;
ifs.open("test.txt", std::ifstream::in);
if (ifs.fail())
{
std::cout << "ifs: open file failed" << std::endl;
exit(-1);
}
std::string line;
while (std::getline(ifs, line))
{
std::cout << line << std::endl;
line.clear();
}
ifs.close();
system("pause");
return 0;
}
2.关于">>"
#include<iostream>
int main()
{
int input_value;
//因istream& operator>> (int& val),故下面两句等效
std::cin >> input_value;
//std::cin.operator>>(input_value);
std::cout << input_value << std::endl;
system("pause");
return 0;
}
#include<iostream>
int main()
{
int input_value;
int value_2;
//这里能够连续输入时因为“>>”的定义istream& operator>> (int& val); ,返回值仍然为istream,故能够继续输入。
//下面两句等效
std::cin >> input_value >> value_2;
//std::cin.operator>>(input_value).operator>>(value_2);
std::cout << input_value << std::endl;
std::cout << value_2 << std::endl;
system("pause");
return 0;
}
3.istream成员函数
gcount Returns the number of characters extracted by the last unformatted input operation performed on the object.
getline 注意: A null character ('\0'
) is automatically appended to the written sequence if n is greater than zero, even if an empty string is extracted.
get Extracts(提取之后std::cin中就不会再存有对应的输入内容) characters from the stream, as unformatted input:
支持的格式有:
(1) single character
(2) c-string
(3) stream buffer
#include<iostream>
int main()
{
char input[20];
std::cin.getline(input, 10);
//std::cin.get(input, 20);
std::cout << std::cin.gcount() << std::endl;
system("pause");
return 0;
}
ignore 对当前获取到的输入内容,抛弃掉遇到制定的字符串之前的内容,保留指定字符串之后的。
#include<iostream>
int main()
{
char first, last;
first = std::cin.get();
std::cin.ignore(32,' ');
last = std::cin.get();
std::cout << "first:" << first << " last:" << last << std::endl;
system("pause");
return 0;
}
read istream& read (char* s, streamsize n);
Extracts n characters from the stream and stores them in the array pointed to by s.
#include<iostream>
#include<fstream>
int main()
{
std::ifstream ifs("test.txt", std::ifstream::binary);
if (ifs)
{
// change input stream position
ifs.seekg(0, ifs.end);
// return input stream position
int length = ifs.tellg();
ifs.seekg(0, ifs.beg);
char* buffer = new char[length+1];
std::cout << "length: " << length << std::endl;
ifs.read(buffer, length);
buffer[length] = '\0';
std::cout << buffer << std::endl;
ifs.close();
}
system("pause");
return 0;
}
readsome streamsize readsome (char* s, streamsize n);
putback
Attempts to decrease the current location in the stream by one character, making the last character extracted from the stream once again available to be extracted by input operations.
#include <iostream> // std::cin, std::cout
#include <string> // std::string
int main () {
std::cout << "Please, enter a number or a word: ";
char c = std::cin.get();
std::string str;
//填充一个c在输入流的“头”,可以保证输入流在用get提取一个字符后仍然保持不变
std::cin.putback(c);
getline(std::cin, str);
std::cout << "You entered a word: " << str << '\n';
system("pause");
return 0;
}
tellg streampos tellg();
Returns the position of the current character in the input stream. 返回当前位置从开头位置的偏移数。
seekg Sets the position of the next character to be extracted from the input stream.