开发者

how to extract file names with .txt extension from a text file?

I have a text file named myfile.txt which lists the contents of drive D:\. In my program, I have a functon which will read myfile.txt. It will extract filenames from the .txt开发者_StackOverflow extension. I don't know much C++, so can you make it please "simple"? I am confused about the starting position of the substring as how would I know from where it will start.

#include<iostream>
#include<string>
#include<fstream>

using namespace std;

int main(void)
{
    system("cls");
    string temp;
    fstream file;
    file.open("D:\\myfile.txt", ios::in);

    while( file.good() )
    {     
        file>>temp;
        string str2, str3;
        size_t pos;

        str2 = temp.substr (4,4); // confused with this

        pos = temp.find(".txt");    // position of ".txt" in temp
        str3 = temp.substr (pos);  

        cout << str2 << ' ' << str3 << endl;
    }

    system("pause");
    return 0;
}


  • Read the text file that contains the file name.
  • If the file name ends with .txt, insert it into the array, otherwise discard it.
  • Continue it until you reach at the end of the file.

For reference: ifstream, string.


  1. Load file, extract all lines from it (store in mutable list) ( http://www.linuxquestions.org/questions/programming-9/c-text-file-line-by-line-each-line-to-string-array-126337/ )
  2. Loop through list and delete all the strings that do not end with .txt ( Find if string ends with another string in C++ )


#include <fstream>
#include <vector>
#include <string>

int main (int argc, char* argv[])
{
  if (argc != 2)
    return 1;
  std::ifstream file(argv[1]);
  if (!file.is_open())
    return 2;

  std::vector<std::string> files;
  std::string line;
  while (std::getline(file, line)) {
    if(line.length() < 4)
      continue;
    if(line.substr(line.length() - 4, std::string::npos) == ".txt")
      files.push_back(line);
  }

  /* all files ending with .txt are in "files" */

  return 0;
}


#include <iostream>
#include <iterator>
#include <vector>
#include <algorithm>

// inspried by http://www.cplusplus.com/reference/std/iterator/istream_iterator/
struct getline :
  public std::iterator<std::input_iterator_tag, std::string>
{
    std::istream* in;
    std::string line;
    getline(std::istream& in) : in(&in) {
        ++*this;
    }
    getline() : in(0) {
    }
    getline& operator++() {
        if(in && !std::getline(*in, line)) in = 0;
    }
    std::string operator*() const {
        return line;
    }
    bool operator!=(const getline& rhs) const {
        return !in != !rhs.in;
    }
};

bool doesnt_end_in_txt(const std::string& s) {
    if (s.size() < 4)
        return true;
    if (s.compare(s.size()-4, 4, ".txt") != 0)
        return true;
}

int main() {
    std::vector<std::string> v;
    std::remove_copy_if(getline(std::cin), getline(),
        std::back_inserter(v),
        doesnt_end_in_txt);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜