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

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

Recommended Answers

All 2 Replies

// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  string line;// define a string for the line buffer
  ifstream myfile ("example.txt");
  if (myfile.is_open())// Check if the file is open
  {
    while ( myfile.good() )//Check if the state of the stream is good for i/o operations.
    {
      
	  getline (myfile,line);//get contents of line and put them in the line buffer
	  //looking for .mex
	  if(line.find(".mex")!=string::npos)
	  cout << line << endl;
    }
    myfile.close();
  }
  else cout << "Unable to open file"; 
  cin.get();
  return 0;
}

Hope this helps.

Hello thank you so much for that seriously i can learn from this code and do wonders you are a star and deserve a pat on the back! :)

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.