讀取文字檔案中文每一行並存儲到txt
阿新 • • 發佈:2019-02-18
關鍵程式碼,1 讀取TXT文字檔案的每一行;
2 讀取中文字串
3 儲存中文字串到文字檔案,這次居然可以用%s實現,注意程式碼,不知道下次能不能行。與wchar的區別。
#include "stdafx.h" #include<iostream> #include <sstream> #include<stdio.h> #include<vector> #include <fstream> #include<string> #include <map> #include<stdlib.h> #include<io.h> #include<algorithm> #include <iomanip> #include <windows.h> using namespace std; int main() { map<string, int> mapwstrings; //定義全域性統計字串頻率變數mapwords; FILE* fp = fopen("QuestionLong.txt", "r"); //FILE* fp = fopen("liang.txt", "r"); FILE* funique = fopen("quchong.txt", "a+"); char StrLine[1024]; while (!feof(fp)) { fgets(StrLine, 1024, fp); //讀取一行 mapwstrings[StrLine]++; //printf("%s", StrLine); //輸出 } vector<pair<string, int> > vec(mapwstrings.begin(), mapwstrings.end()); //_wsetlocale(0, L"chs"); for (int i = 0; i < vec.size(); i++) { fprintf(funique, "%s", vec[i].first.c_str());//(wchar_t)daanCh[i]); } fclose(fp); //關閉檔案 fclose(funique); return 0; }