C++ read函數(shù)讀入int整形數(shù)據(jù)
Read函數(shù)定義
通過read函數(shù)將文件中的數(shù)據(jù)按照一定的長度讀取出來并且存放在新的數(shù)組中。用于從文件中讀取數(shù)據(jù)。
函數(shù)原型istream& read (char* s, streamsize n);
參數(shù)char* s取出數(shù)據(jù)的流向的char類型數(shù)組指針,streamsize n表示數(shù)組的長度
#include<iostream> using namespace std; int read()//read函數(shù)主體部分 { 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類型的數(shù)據(jù),long long還需更改) system("pause"); return 0; }
Read函數(shù)使用例子
#include <iostream> // std::cout #include <fstream> // std::ifstream int 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."; else std::cout << "error: only " << is.gcount() << " could be read"; is.close(); // ...buffer contains the entire file... delete[] buffer; } return 0; }
相關(guān)文章
C++實現(xiàn)LeetCode(67.二進(jìn)制數(shù)相加)
這篇文章主要介紹了C++實現(xiàn)LeetCode(67.二進(jìn)制數(shù)相加),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07Java C++ 題解leetcode1619刪除某些元素后數(shù)組均值
這篇文章主要為大家介紹了Java C++ 題解leetcode1619刪除某些元素后數(shù)組均值示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09VC中實現(xiàn)GB2312、BIG5、Unicode編碼轉(zhuǎn)換的方法
這篇文章主要介紹了VC中實現(xiàn)GB2312、BIG5、Unicode編碼轉(zhuǎn)換的方法,該功能非常實用,需要的朋友可以參考下2014-07-07