Hello this is my problem below


How would i do this:

Got a text file called directorycontents.txt in this directorycontents.txt there is a bunch of text each one is a filename with a filename extension i want to be able to go like this if there is a filename extension of specific characters like .txt or .png then do fprintf(stderr,"whateva");

i have looked at istream and fstream and iostream but im not really shore how to use fstream to do this

thanks

any examples would be nice please be simple minded when answer i'm no technical wizard yet.

Recommended Answers

All 2 Replies

ifstream infile;
string line_of_text;
int pos = 0;
vector<string> txt_files;
vector<string> png_files;
vector<string> doc_files;
vector<string> bmp_files;


infile.open("directorycontents.txt");

if(!infile.is_open())
{
     cout << "\aError opening file!";
     cout << "\nFile may have been deleted, moved, or renamed.";
     exit(EXIT_FAILURE);
}

while(infile)
{
     getline(infile, line_of_text);

     if(line_of_text.find(".txt"))
     {
          txt_files.push_back(line_of_text);
     }

     if(line_of_text.find(".png"))
     {
          png_files.push_back(line_of_text);
     }

     if(line_of_text.find(".doc"))
     {
          doc_files.push_back(line_of_text);
     }

     if(line_of_text.find(".bmp"))
     {
          bmp_files.push_back(line_of_text);
     }
}

infile.close();

for(int i=0, size=txt_files.size(); i<size; i++)
{
     cout << txt_files[i] << endl;
}

for(int i=0, size=png_files.size(); i<size; i++)
{
     cout << png_files[i] << endl;
}

for(int i=0, size=doc_files.size(); i<size; i++)
{
     cout << doc_files[i] << endl;
}

for(int i=0, size=bmp_files.size(); i<size; i++)
{
     cout << bmp_files[i] << endl;
}

I'd do something similar to Clinton's idea read a line with getline do the find like he does if found do the fprintf there is no need for vectors based on your post and i'm sure they are beyond your knowledge people here think vectors are the best thing since the wheel and never realize that new students probably have never heard of them and always suggest them to the student's chagrin also not that reading text like yours with no sentence structure is very difficult to read please use proper english syntax including proper punctuation this is not im

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.