c++ io(cin读取多行数字,文件读取)

cin/cin.get/cin.getline与输入缓冲区

从键盘读入的几种方式

#include <iostream>
#include <string>

using namespace std;

int main(){
    char a[20];
    cout<<"cin读取----------------"<<endl;
    //输入一个数字或者字符
    cout<<"    读取两个数字:"<<endl;
    int i,j;
    cin>>i>>j;
    cout<<i<<","<<j<<endl;
    //接收一个字符串,遇“空格”、“TAB”、“回车”就结束
    cout<<"    读取字符串:"<<endl;
    cin>>a;
    cout<<a<<endl;


    cout<<"cin.get读取-------------"<<endl;
    //cin.get(字符变量名),用来接收字符,只获取一个字符,可以接收空格,遇回车结束
    //cin.get()从输入缓冲区读取时不忽略分隔符(空白符),直接将其读取
    cout<<"    读取一个字符:"<<endl;
    cin.ignore(INT32_MAX, '\n');//忽略缓存中的字符,清空缓存
    char c;
    cin.get(c);
    cout<<c<<endl;
    //cin.get(数组名,接收字符数目),用来接收字符串,可以接收空格,遇回车结束。
    cout<<"    读取一个字符串"<<endl;
    cin.ignore(INT32_MAX, '\n');//忽略缓存中的字符,清空缓存
    char d[10];
    cin.get(d,5);
    cout<<d<<endl;


    cout<<"cin.getline()读取-------------"<<endl;
    //接收一个字符串,可以接收空格并输出,cin.getline(接收字符串的变量,接收字符个数,结束字符)
    cin.ignore(INT32_MAX, '\n');//忽略缓存中的字符,清空缓存
    char b[20];
    cin.getline(b,10);
    cout<<b<<endl;


    cout<<"getline读取----------------"<<endl;
    //接收一个字符串,可以接收空格并输出,需包含“#include<string>”
    //istream &getline( char *buffer, streamsize num, char delim );
    string str;
    getline(cin,str);
    cout<<str<<endl;


    return  0;

}

从键盘读入数组

#include <iostream>
#include <cstdio>
#include <vector>

using namespace std;

/*
1,5
2,3
4,5,6
2,7,9
4,9,10
*/
int main()
{
    int n;
    cin >> n;
    vector<vector<int>> arrays;
    for (int i = 0; i < n; i++)
    {
        vector<int> input;
        int number;
        while (cin >> number)
        {
            input.push_back(number);
            if (cin.get() == '\n') //按下回车键推出循环
                break;
        }
        arrays.push_back(input);
    }

    cout << "\n打印:\n";
    for (auto nums : arrays)
    {
        for (auto num : nums)
        {
            cout << num;
            cout << ",";
        }
        cout << endl;
    }

    return 0;
}

从文件读取数据的几种方式

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void ReadFile1(string filename){
    //逐词读取,词之间用空格区分
    ifstream f(filename);
    string s;
    if(f.is_open()){
        while(!f.eof())
        {   
            f>>s; 
            cout<<"read1:"<<s<<endl;
        }
    }
}

void ReadFile2(string filename){
    //逐行读取,将行读入字符数组,行之间用回车换行
    ifstream f(filename);
    const int LINE_LENGTH=100;
    char str[LINE_LENGTH];
    if(f.is_open()){
        while(!f.eof())
        {
            f.getline(str,LINE_LENGTH);
            cout<<"read2:"<<str<<endl;
        }
    }
}

void ReadFile3(string filename){
    //逐行读取,将行读入字符数组,行之间用回车换行
    ifstream f(filename);
    string str;
    char delim=' ';
    if(f.is_open()){
        while(!f.eof())
        {
            // getline(f,str);
            getline(f,str,delim);
            cout<<"read2:"<<str<<endl;
        }
    }
}



int main(){
    string filename="test.txt";

    cout<<"##################################"<<endl;
    ReadFile1(filename);
    cout<<"##################################"<<endl;
    ReadFile2(filename);
    cout<<"##################################"<<endl;
    ReadFile3(filename);
    
    return 0;
}

从文件读取数组

#include <iostream>
#include <vector>
#include <fstream>
#include <zconf.h>
#include <string>
#include <sstream>

std::vector<std::vector<double>> readMatrixFile(const char *fileName)
{
    std::vector<std::vector<double>> matrixALL{};
    int row = 0;

    std::ifstream fileStream;
    std::string tmp;
    int count = 0;                           // 行数计数器
    fileStream.open(fileName, std::ios::in); //ios::in 表示以只读的方式读取文件

    if (fileStream.fail()) //文件打开失败:返回0
    {
        throw std::logic_error("read file fail");
    }
    else //文件存在
    {
        while (getline(fileStream, tmp, '\n')) //读取一行
        {
            std::cout << tmp << std::endl;
            if (count == 0)
            {
                row = std::stoi(tmp);
            }
            else
            {
                std::vector<double> tmpV{};
                std::istringstream is(tmp);
                for (int i = 0; i < row; i++)
                {
                    std::string str_tmp;
                    is >> str_tmp;
                    tmpV.push_back(std::stod(str_tmp));
                }
                matrixALL.push_back(tmpV);
            }
            count++;
        }
        fileStream.close();
    }

    return matrixALL;
}

int main()
{
    std::vector<std::vector<double>> matrixALL = readMatrixFile("../matrix.txt");
    for (int i = 0; i < matrixALL.size(); i++)
    {
        for (int j = 0; j < matrixALL[0].size(); ++j)
        {
            std::cout << matrixALL[i][j];
        }
        std::cout << std::endl;
    }
    return 0;
}

写入数组到文件

#include <fstream>
#include <iostream>
#include <vector>

using namespace std;


int main()
{
    vector<int> nums={1,2,3};

	ofstream out("输出.txt");
    for(auto num:nums)
	    out<<num<<" ";
    out<<"\nend.";
        
	out.close();

    return 0;
}
#include <iostream>
#include <iomanip>
using namespace std;
 
int  main()
{
	int x = 1000;
	double y = 1.23456789;
	cout << "默认x值:" << x << endl;
	cout << "十进制:" << dec << x << endl; //dec oct hex 会一直作用。
	cout << "八进制:" << oct << x << endl;
	cout << "十六进制:" << hex << x << endl;
	cout << "十六进制(大写字母):" << hex << uppercase << x << endl << endl;
	cout << "默认y值(左对齐且有效数字位数为6):" << y << endl;
	cout << "宽度为10并右对齐:" << setw(10) << right << y << endl;
	cout << "宽度为8:" << setw(8) << y << endl;
	cout << "宽度为4:" << setw(4) << y << endl;
	cout << "用*号填充空位(10位宽度):" << setfill('*') << setw(10) << y << endl; //setw(int i) 只对紧随的数据显示有影响。控制多个数据要多个setw()method.
	cout << "设精度为3输出y(不包括小数点):" << setprecision(3) << y << endl; //setprecision(int i) 会一直作用
	cout << "设精度为8输出y(不包括小数点):" << setprecision(8) << y << endl;
	cout << "显示正负号:" << showpos << y << endl;
	cout << "用科学计数法表示y:" << scientific << y << endl;
	cout << "用科学计数法表示y(控制E前数据的小数点后位数):" << scientific
		<< setprecision(3) << y << endl;

    return 0;
}

原文链接: https://www.cnblogs.com/buyizhiyou/p/13723101.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    c++ io(cin读取多行数字,文件读取)

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/203217

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月12日 下午9:26
下一篇 2023年2月12日 下午9:26

相关推荐