国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++ read函數讀入int整形數據

2020-05-23 14:01:50
字體:
來源:轉載
供稿:網友

Read函數定義

通過read函數將文件中的數據按照一定的長度讀取出來并且存放在新的數組中。用于從文件中讀取數據。

函數原型istream& read (char* s, streamsize n);

參數char* s取出數據的流向的char類型數組指針,streamsize n表示數組的長度

#include<iostream>using namespace std;int read()//read函數主體部分{  int x=0,f=1;char ch=getchar();  while(ch<'0'||ch>'9')  {    if(ch=='-')f=-1;    ch=getchar();  }  while(ch>='0'&&ch<='9')  {    x=x*10+ch-'0';    ch=getchar();  }  return x*f;}int main(){  int n=read()//這就是讀入了n(注意只能用來讀入int類型的數據,long long還需更改)  system("pause");  return 0;}

Read函數使用例子

#include <iostream> // std::cout#include <fstream> // std::ifstreamint main () {std::ifstream is ("test.txt", std::ifstream::binary);if (is) {// get length of file:is.seekg (0, is.end);int length = is.tellg();is.seekg (0, is.beg);char * buffer = new char [length];std::cout << "Reading " << length << " characters... ";// read data as a block:is.read (buffer,length);if (is)std::cout << "all characters read successfully.";elsestd::cout << "error: only " << is.gcount() << " could be read";is.close();// ...buffer contains the entire file...delete[] buffer;}return 0;}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 托克托县| 岚皋县| 衡水市| 寿阳县| 略阳县| 宝应县| 年辖:市辖区| 个旧市| 古交市| 宜丰县| 石阡县| 全州县| 含山县| 民乐县| 当阳市| 高密市| 广灵县| 内黄县| 博客| 色达县| 望奎县| 襄樊市| 重庆市| 德格县| 阳信县| 阳东县| 开阳县| 武陟县| 彩票| 方城县| 玉环县| 塘沽区| 临沭县| 松桃| 游戏| 阿拉善盟| 綦江县| 罗平县| 手机| 金山区| 谢通门县|