国产探花免费观看_亚洲丰满少妇自慰呻吟_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;}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 闻喜县| 峡江县| 开江县| 顺昌县| 阿克陶县| 万盛区| 婺源县| 夏河县| 永泰县| 札达县| 翼城县| 福泉市| 罗甸县| 铅山县| 昌平区| 阳原县| 迁安市| 玉屏| 防城港市| 海口市| 嘉荫县| 共和县| 宁阳县| 墨江| 淮北市| 五峰| 满洲里市| 湘西| 邯郸县| 来宾市| 务川| SHOW| 岳普湖县| 华池县| 奉化市| 蓬溪县| 呼玛县| 博爱县| 连南| 莫力| 峨边|