iostream用法 C vector進行文本文件讀取?
C vector進行文本文件讀???1)。#include<iostream>#include<vector>使用namespacestdintmain(){inta[7]={1,2,
C vector進行文本文件讀取?
1)。#include<iostream>#include<vector>使用namespacestdintmain(){inta[7]={1,2,3,4,5,6,7}vector<int>(a,a 7)for(inti=0i<va.尺寸()I)cout<<va[I]<< “”}這是一個簡單的遍歷向量,輸出向量的所有元素。2) 這是一個簡單的從屬向量測試.txt數(shù)據(jù)從文本文件中提取到vector<string>va,然后輸出。測試.txt運行結果如下://程序代碼如下:#include<iostream>#include<vector>#include<fstream>#include<string>usingnamespacestintmain(){vector<string>vaifstream()測試.txt)用于(stringsin>>s)va.push返回(inti=0i<va.尺寸()I)cout<<va[I]<< “”}
空格在讀入后也是空格,在讀入回車后會出現(xiàn)換行。get函數(shù)的用法是將文本中的一個字符讀入程序內存。每次你讀它時,它都會自動跳轉到文本的下一個字符。下面是一個示例:#include< iostream>#include< fstream>使用namespace stdint main(){ifstream fin()測試.txt)而(!財政部()){字符a=fin.get公司()字符b=fin.get公司()cout<<A<<<<<endl}返回0}
C逐行讀取TXT文本數(shù)據(jù)如下:1提供輸入和輸出流的getline函數(shù)可以讀取整行。完整的代碼如下:#include<iostream>#include<fstream>使用namespace stdint main(){//define input file stream class object infiefstream infle()測試.txt“,IOS::in)如果(!Infile){//判斷文件是否有cerr<<“打開錯誤?!?lt<endl exit(1)//退出程序}char STR[255]//定義一個字符數(shù)組以接受在(infile){填充線(STR,255)//getline函數(shù)可以讀取整行并保存在STR array cout<<str<<endl}return 1}