- 功能:
- 讀取、解析csv文件的內容,再打印出每一個聯繫人的數據;
- 能夠根據客戶輸入的屬性KEY,來實現以KEY爲依據來排序,並將排序結果打印出來;
- 能夠對錯誤格式的文件進行檢測;
- 數據流:
- 首先引用#include<fstream>,包含:ofstream,ifstream,fstream
- 新建一個csv文件(也可以由ofstream生成),可以手動輸入數據也可以通過ofstream類來實現打開和往csv文件寫入數據;由於.csv文件是以‘,’爲分隔符的,在每次讀取到’,’截止,注意是英文的’,’不是中文字符的;
- 讀取.csv文件,使用ifstream進行讀取。
- 設計
- 創建描述客戶信息的結構體Client
- 每次獲取整行數據,並且通過’,’爲分界來獲取每個部分的數據,同時賦值到客戶結構體實例client的對應成員,然後將這個實例push到vectorClient;
- 通過寫sort()的比較函數compId、compAge來實現sort根據客戶的id、age排序。
- 判斷打開文件是否爲.csv文件,則是通過判斷文件的後綴是否符合要求來實現。
- 小點
ofstream是從內存到硬盤,ifstream是從硬盤到內存,其實所謂的流緩衝就是內存空間。
在C++中,有一個stream這個類,所有的I/O都以這個“流”類爲基礎的
ios::in: 文件以輸入方式打開(文件數據輸入到內存)
ios::out: 文件以輸出方式打開(內存數據輸出到文件)
輸入時以整行字符串加上換行符號endl一次寫入。
輸出一個換行符並刷新此流 輸出
#include <iostream>
#include <fstream> //定義讀寫已命名文件的類型
#include <vector>
#include <sstream> //多定義的類型則用於讀寫存儲在內存中的string對象
//atoi()
#include <stdlib.h>
#include <stdio.h>
#include <string.h>//strrchr用到
#include<string>
#include<algorithm>
using namespace std;
struct Client{
int id;
string name;
int age;
string hobby;
};
//ID排序函數:
bool compId(const Client &a, const Client &b){
if (a.id < b.id)
return true;
else ///這裏超級重要!!!
return false;
}
//age排序函數:
bool compAge(const Client &a, const Client &b){
if (a.age < b.age)
return true;
else ///這裏超級重要!!!
return false;
}
int main()
{
//寫文件
ofstream outFile("F:\\學習提升\\c++\\Test\\data1.csv",ios::out); //輸出文件流(輸出到文件)
outFile<<"id"<<","<<"name"<<","<<"age"<<","<<"hobby"<<endl;
outFile<<"2"<<","<<"Lilei"<<","<<14<<","<<"music"<<endl;
outFile<<"5"<<","<<"Katherine"<<","<<36<<","<<"football"<<endl;
outFile<<"1"<<","<<"Bob"<<","<<18<<","<<"pingpong"<<endl;
outFile<<"4"<<","<<"Tom"<<","<<20<<","<<"basketball"<<endl;
outFile<<"3"<<","<<"Jerry"<<","<<56<<","<<"computer"<<endl;
outFile<<"6"<<","<<"ketty"<<","<<25<<","<<"run"<<endl;
const char str[] = "data1.csv";
const char ch = '.';
//通過文件名後綴判斷文件類型()
string str1;
str1 = strrchr(str, ch);//截取 .及之後的內容
cout << "----判斷是不是CSV類型文件----" << endl;
if(str1 == ".csv")
cout<< " 該文件是CSV類型文件" << endl;
else
cout<< "!!!請確認輸入的文件類型" << endl;
//讀文件
ifstream inFile("F:\\學習提升\\c++\\Test\\data1.csv",ios::in);//inFile來自fstream,ifstream爲輸入文件流(從文件讀入)
string lineStr;
vector<Client> vectorClient;
int num = 0;
while(getline(inFile,lineStr)) //getline來自sstream //讀入整行數據到Linestr裏
{
stringstream ss(lineStr);//來自sstream
string str;
if(num > 0)
{ //實現只對數據寫入vector ,不對標題寫入,從而進行排序
//按照逗號分隔
Client client;
int n1 = 1;
while(getline(ss,str,','))
{
//cout<<str<<endl;
switch(n1)
{
case 1:
client.id = atoi(str.c_str());
break;
case 2:
client.name = str;
break;
case 3:
client.age = atoi(str.c_str());
break;
case 4:
client.hobby = str;
break;
}
n1++;
}
vectorClient.push_back(client);//每一行vector數據都放到strArray中去
}
num++;
}
//未排序
cout << "————未排序————" << endl;
//輸出結果
for (vector<Client>::iterator it = vectorClient.begin(); it != vectorClient.end(); it++)
{
cout << "id: " << it->id << " name: " << it->name << " age: " << it->age << " hobby: " << it->hobby << endl;
}
//輸入KEY
cout << "請輸入排序的KEY" <<endl;
cout << "可輸入id,age兩種排序選擇,輸入exit推出" <<endl;
string key;
cin >> key;
int flag = 1;//默認按照id排序
if(key == "age")
{
flag = 2;
}else if(key == "exit")
{
cout << "不排序,即將退出" <<endl;
getchar();//(防止程序一閃而過)
return 0;
}
cout << "————排序後————" << endl;
switch(flag)
{
case 1:
sort(vectorClient.begin(), vectorClient.end(), compId);
cout << "根據 ID 排序" << endl;
break;
case 2:
sort(vectorClient.begin(), vectorClient.end(), compAge);
cout << "根據 年齡 排序" << endl;
break;
}
//排序後 輸出結果
for (vector<Client>::iterator it = vectorClient.begin(); it != vectorClient.end(); it++)
{
cout << "id: " << it->id << " name: " << it->name << " age: " << it->age << " hobby: " << it->hobby << endl;
}
getchar();//(防止程序一閃而過)
return 0;
}