C++ 在函数C++;
我想将下面代码中变量C++ 在函数C++;,c++,windows,console-application,C++,Windows,Console Application,我想将下面代码中变量name中的数据保存到我的输出文件data.txt似乎足够简单 不,既然我正在扫描.exe的根“目录中的文件,然后很容易地将它们输出到cmd中,为什么我还要努力简单地输出到文件中呢 我看了看链接。他们帮了一点忙 如有任何建议,将不胜感激 原始处女 #包括“stdafx.h” #包括 #包括 #包括 #包括 #包括 使用名称空间std; std::vector get_文件名(std::experimental::filesystem::path) { 命名空间stdfs=st
name
中的数据保存到我的输出文件data.txt
似乎足够简单
不,既然我正在扫描.exe
的根“
目录中的文件,然后很容易地将它们输出到cmd
中,为什么我还要努力简单地输出到文件中呢
我看了看链接。他们帮了一点忙
如有任何建议,将不胜感激
原始处女
#包括“stdafx.h”
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
std::vector get_文件名(std::experimental::filesystem::path)
{
命名空间stdfs=std::实验::文件系统;
std::矢量文件名;
常量stdfs::目录\迭代器结束{};
for(stdfs::directory_iterator iter{path};iter!=end;++iter)
{
if(stdfs::is_regular_file(*iter))
向后推(iter->path().string());
}
返回文件名;
}
void Dirloop(){
对于main()
中的(const auto&name:get_filename(“.”)std::cout调用get_filename
。然后可以将向量作为参数传递给Dirloop()
和Outfile()
void Dirloop(const std::vector和filename){
for(const auto&名称:文件名){
std::coutoutputFile您在DirLoop
函数中声明name
,但您试图在Outfile
中使用它。更改Outfile
以name
作为参数,并从DirLoop
调用它。它还应该在std::ios::app
模式下打开文件,以便添加到fi谢谢,我得到了错误:C2440:“初始化”:在VC 2015中无法从…转换任何想法?我认为它缺少一个转换字符?输入错误,get\u filenames
应该是get\u filenames()
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
#include <experimental/filesystem>
#include <fstream>
using namespace std;
std::vector<std::string> get_filenames(std::experimental::filesystem::path path)
{
namespace stdfs = std::experimental::filesystem;
std::vector<std::string> filenames;
const stdfs::directory_iterator end{};
for (stdfs::directory_iterator iter{ path }; iter != end; ++iter)
{
if (stdfs::is_regular_file(*iter))
filenames.push_back(iter->path().string());
}
return filenames;
}
void Dirloop(){
for (const auto& name : get_filenames(".")) std::cout << name << '\n';
}
void Outfile() {
std::ofstream outputFile("data.txt", std::ios::out);
outputFile << name << std::endl;
outputFile.close();
cout << "Generated data.txt!\n";
}
int main()
{
Dirloop();
Outfile();
std::getchar();
return 0;
}
void Dirloop(const std::vector<std::string> &filenames){
for (const auto& name : filenames) {
std::cout << name << '\n';
}
}
void Outfile(const std::vector<std::string> &filenames) {
std::ofstream outputFile("data.txt", std::ios::out);
for (const auto& name : filenames) {
outputFile << name << '\n';
}
outputFile.close();
cout << "Generated data.txt!\n";
}
int main()
{
std::vector<std::string> filenames = get_filenames(".");
Dirloop(filenames);
Outfile(filenames);
std::getchar();
return 0;
}