1

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 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;
}
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Mahee
  • 23
  • 6
  • Another point - if you're on a unix system, you could do the filtering before you even get to your program. i.e. `find / -type f -iname "*.txt" > textFiles.txt` – I82Much Apr 07 '11 at 18:40
  • Or, if you already have the list, `grep 'txt$' < files.txt > textfiles.txt`. – Robᵩ Apr 07 '11 at 18:59

4 Answers4

4
  • 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.

Donotalo
  • 12,748
  • 25
  • 83
  • 121
  • how will i make sure that it ends with .txt? – Mahee Apr 07 '11 at 17:16
  • take a substring of the filename and compare if the substring is .txt or not. Look into the string link i've provided to know how to take substring. – Donotalo Apr 07 '11 at 18:00
  • thanks!!. What if i dont know the starting position of the substring as required in string substr ( size_t pos = 0, size_t n = npos ) const; how will i determine the starting position. – Mahee Apr 08 '11 at 21:33
  • i got this after compilation when i implemented the program as you suggested....Unhandled exception at 0x7c81eb33 in file.exe: Microsoft C++ exception: std::out_of_range at memory location 0x0012fc38.. – Mahee Apr 08 '11 at 21:38
  • you can leave the parameters as default. – Donotalo Apr 08 '11 at 22:32
4
  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++ )
Community
  • 1
  • 1
I82Much
  • 26,901
  • 13
  • 88
  • 119
  • 1
    That seems like a waste of memory if only .001% of the file names end in ".TXT". How about `std::istream_iterator` instead of creating the first list? – Robᵩ Apr 07 '11 at 17:52
  • I agree that this is not the most efficient approach. However, it is probably the simplest conceptually, and I'd rather the OP get a working implementation and worry about improving it later rather than worrying prematurely optimizing it. – I82Much Apr 07 '11 at 18:39
0
#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;
}
mauve
  • 1,976
  • 12
  • 18
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);
}
Robᵩ
  • 163,533
  • 20
  • 239
  • 308