如何写入txt文件。 [英] How do I write to txt file.

查看:95
本文介绍了如何写入txt文件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(outputFile<< files [i]<<<< fileInfo.st_size<<'\ n';;)不会写入files.txt。



我的尝试:



The (outputFile << files[i]<<" " << fileInfo.st_size << '\n';) wont write to "files.txt".

What I have tried:

#include <iostream>
#include <ctime>
#include <sys/types.h>
#include <sys/stat.h>
#include <cerrno>
#include <cstring>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <vector>
#include <string>
#include <fstream>
using namespace std;

/*function... might want it in some class?*/
int getdir (string dir, vector<string> &files)
{
    DIR *dp;
    struct dirent *dirp;
    if((dp  = opendir(dir.c_str())) == NULL) {
        cout << "Error(" << errno << ") opening " << dir << endl;
        return errno;
    }

    while ((dirp = readdir(dp)) != NULL) {
        files.push_back(string(dirp->d_name));
    }
    closedir(dp);

    return 0;
}

int main(int argc, char** argv )
{
    std::string dir = string(".");
    vector<string> files = vector<string>();

    getdir(dir,files);
std::ofstream outputFile;
outputFile.open("Files.txt");//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    for (unsigned int i = 2;i < files.size();i++) {

    struct stat fileInfo;

   if (stat(files[i].c_str(), &fileInfo) != 0) {
      std::cerr << "Error: " << strerror(errno) << '\n';

   }


   if ((fileInfo.st_mode & S_IFMT) == S_IFDIR)

outputFile << files[i]<<" " << fileInfo.st_size << '\n';//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
cout << files[i]<<" " << fileInfo.st_size << '\n';
}
cin.get();
}

推荐答案

if ((fileInfo.st_mode & S_IFMT) == S_IFDIR)



你真的需要这个条件吗?由于这种情况,只有目录名称将写入文件。


Do you really need this condition? Due to this condition, only directories names will be written to the file.


这篇关于如何写入txt文件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆