本文介绍了如何使用outputfile显示文件的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在结尾处添加outputFile的文件名?
我有什么试过:
#include< iostream>
#include< ctime>
#include< sys / types.h>
#include< sys / stat.h>
#include< cerrno>
#include< cstring>
#include< fstream>
using namespace std;
int main(int argc,char ** argv)
{std :: string x;
std :: ofstream outputFile;
ifstream inFile;
std :: ifstream infile(Team.txt);
inFile.open(test.txt);
std :: string line;
while(std :: getline(infile,line)){
std :: cout<< line<<的std :: ENDL;
}
std :: string dir = x;
if(!inFile){
cout<< 无法打开文件;
return(0); //终止,错误
}
while(inFile>> x){
dir = x;
}
inFile.close();
struct stat fileInfo;
if(stat(dir.c_str(),& fileInfo)!= 0){//使用stat()获取信息
std :: cerr< ;< 错误:<< strerror(errno)<< \\\
;
return(0);
}
outputFile<< 类型::;
if((fileInfo.st_mode& S_IFMT)== S_IFDIR){//来自sys / types.h
cout<< Directory\\\
;
} else {
outputFile<< File\\\
;
}
outputFile.open(program3data.txt);
outputFile<< 大小:<<
fileInfo.st_size<< \\\
; //大小(字节)
outputFile<< 设备:<<
(char)(fileInfo.st_dev +'A')<< \\\
; //设备编号
outputFile<< 创建:<<
std :: ctime(& fileInfo.st_ctime); //创建时间
outputFile<< 已修改:<<
std :: ctime(& fileInfo.st_mtime); //上次修改时间
}
解决方案
How do I add file name for outputFile at the end?
What I have tried:
#include <iostream> #include <ctime> #include <sys/types.h> #include <sys/stat.h> #include <cerrno> #include <cstring> #include <fstream> using namespace std; int main(int argc, char** argv ) {std::string x; std::ofstream outputFile; ifstream inFile; std::ifstream infile("Team.txt"); inFile.open("test.txt"); std::string line; while (std::getline(infile, line)) { std::cout << line << std::endl; } std::string dir=x; if (!inFile) { cout << "Unable to open file"; return(0); // terminate with error } while (inFile >> x) { dir=x; } inFile.close(); struct stat fileInfo; if (stat(dir.c_str(), &fileInfo) != 0) { // Use stat( ) to get the info std::cerr << "Error: " << strerror(errno) << '\n'; return(0); } outputFile << "Type: : "; if ((fileInfo.st_mode & S_IFMT) == S_IFDIR) { // From sys/types.h cout << "Directory\n"; } else { outputFile << "File\n"; } outputFile.open("program3data.txt"); outputFile << "Size : " << fileInfo.st_size << '\n'; // Size in bytes outputFile << "Device : " << (char)(fileInfo.st_dev + 'A') << '\n'; // Device number outputFile << "Created : " << std::ctime(&fileInfo.st_ctime); // Creation time outputFile << "Modified : " << std::ctime(&fileInfo.st_mtime); // Last mod time }
解决方案
这篇关于如何使用outputfile显示文件的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!