C++ read函數讀入int整形數據
更新時間:2016年07月17日 16:08:11 投稿:hebedich
這篇文章主要介紹了C++ read函數讀入int整形數據的相關資料,需要的朋友可以參考下
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::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; }
您可能感興趣的文章:
相關文章
Java C++ 題解leetcode1619刪除某些元素后數組均值
這篇文章主要為大家介紹了Java C++ 題解leetcode1619刪除某些元素后數組均值示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09VC中實現GB2312、BIG5、Unicode編碼轉換的方法
這篇文章主要介紹了VC中實現GB2312、BIG5、Unicode編碼轉換的方法,該功能非常實用,需要的朋友可以參考下2014-07-07