Go Job Hunting at the LQ Job Marketplace
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 02-06-2007, 02:21 PM   #1
Registered: Nov 2005
Posts: 66

Rep: Reputation: 15
looping trough files


I am trying to make some sort of music-library program with C++. But now I have this pretty stupid question.

I want to read every id3-tag in a directory. So that means I need to loop recursively trough all of the files. But how would I do that?

Basically I need something like:

string filename;
loop {
   filename = next_file();
   // do something with the file
I was wandering to just do: ls >> file
And then readout the output line by line, but this seems to be very hard, at least harder than it should be.
Old 02-06-2007, 02:30 PM   #2
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
There are two main ways to get lists of files from directories:

1. readdir, opendir, closedir

2. glob.h

See the readdir and glob manual pages for details.

Once you've got the file names, I would guess the best thing would be to use some pre-existing open source tag processing library to do the actual extraction of the tags. I'm sure there are many alternative ID3 tag processing libraries out there - try a few and compare the performance - this is the sort of thing where different implementations can have wildly different performance characteristics.
Old 02-06-2007, 02:31 PM   #3
Registered: Nov 2005
Posts: 66

Original Poster
Rep: Reputation: 15
Yes I already can read id3 tags from files. But until now I needed to type the name of the file's directly into the source code.
Old 02-09-2007, 10:33 AM   #4
Registered: Nov 2005
Posts: 66

Original Poster
Rep: Reputation: 15
1. readdir, opendir, closedir
I have now found a simple examle of how I can display the contents of a directory:

#include <errno.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>

int main()
	DIR *pdir;
	struct dirent *pent;

	pdir=opendir("."); //"." refers to the current dir
	if (!pdir){
		std::cout << "opendir() failure; terminating\n";
	while ((pent=readdir(pdir)))
		std::cout << pent->d_name << "\n";
	if (errno)
		std::cout << "readdir() failure; terminating\n";
The output is as following:
Press Enter to continue!
But how can I see the difference between a normal file and a directory?

Also, I dont seem to have a file called dir.h in my /usr/include/ or anywhere else.

Last edited by BackwardsDown; 02-09-2007 at 10:52 AM.
Old 02-09-2007, 11:39 PM   #5
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
Difference between file and directory can be obtained by reading the contents of the directory. Do a stat on the directory and with that we can identify it as a directory or a file


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Looping a script Keentolearn Linux - Newbie 4 01-30-2007 04:54 PM
See binary files trough terminal ubuntu aeruzcar Linux - Software 3 08-29-2006 08:34 PM
login looping plaircpa Slackware 3 10-31-2005 09:10 PM
View my files in windows trough FC2 Mesopotamic Linux - Newbie 4 09-19-2004 10:23 PM
looping in mplayer zexter Linux - Software 1 12-22-2003 05:39 PM

All times are GMT -5. The time now is 09:18 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration