NoobScratcher
NoobScratcher

Reputation: 139

how to read from a text file contents file name extension in c++?

Hello I want to read from a text file full of directory contents

Here's my example:

below is my text file called MyText.txt

MyText.txt

title.txt,image.png,sound.mp3

I want to be able to read that .txt extension not the filename and I want it to be for file extensions only for example .txt or .mp3 how would I do that in c++?.

When I mean read I mean reference it in a if statement like this:

if(.mp3 exists in a text file)
{
  fprintf(stderr,"sees the mp3 extensions");
}

I'm running Windows 7 32-bit.

I need a more cross platform approach.

Upvotes: 1

Views: 203

Answers (1)

moongoal
moongoal

Reputation: 2956

May I suggest you to read a tutorial on C++ file handling and another one on C++ strings?

There is no a quick solution: you have to read the file using the ifstream class. After reading the file and storing it in one or more strings, you can then use the find and substr string methods to create a queue of discrete filenames. Using the same methods, you can then split the queued elements again, in order to find the extensions and add them to a set. A set does not allow duplicates, so you are sure all the extensions will appear only once.

Upvotes: 2

Related Questions