转载链接:https://blog.csdn.net/ttomchy/article/details/83275417
本人需要 故留存、分享
重要的在于home与sln位于同一目录下,要访问home文件夹下的txt文件
必须:string path = “home\data.txt”; /同一目录下(sln)的home的使用方法:
切记切记!!!!!!
//**读取文件--并计算文件中存在多少数据。**
#include <iostream>
#include <fstream>
#include <ctime>
#include <vector>
#include <string>
using namespace std;
vector<string> readtxt_name(string file)
{
ifstream infile;
infile.open(file.data());
cout << "infile.is_open() is : " << infile.is_open() << endl;
if (infile.is_open() == 0){
cout << "file not existed" << endl;
}
vector<string> txt_name;
string str;
while (getline(infile, str))
{
txt_name.push_back(str);
}
cout << txt_name.size() << endl;
infile.close();
return txt_name;
}
int main()
{
vector<string> txt_name;
vector<string> final_path_txt_name;
string path = "home\\data.txt"; /同一目录下(sln)的home的使用方法:
std::string str_temp;
char ss[20];
txt_name = readtxt_name(path);
for (auto it = txt_name.begin(); it != txt_name.end(); ++it){
str_temp = *it;
strcpy(ss, str_temp.c_str());
string path_final = "home\\";
path_final = path_final + str_temp;
std::cout << " path_final is: " << path_final << std::endl;
final_path_txt_name.push_back(path_final);
}
std::cout << "final_path_txt_name.size is :" << final_path_txt_name.size() << std::endl;
return 0;
}