LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


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

Rep: Reputation: 15
looping trough files


Hi,

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:

Code:
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, 01:30 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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, 01:31 PM   #3
BackwardsDown
Member
 
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, 09:33 AM   #4
BackwardsDown
Member
 
Registered: Nov 2005
Posts: 66

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

Code:
#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";
		exit(1);
	}
	
	errno=0;
	while ((pent=readdir(pdir)))
	{
		std::cout << pent->d_name << "\n";
	}
	
	if (errno)
	{
		std::cout << "readdir() failure; terminating\n";
	exit(1);
	}
	
	closedir(pdir);
}
The output is as following:
Quote:
.
..
Makefile
.deps
readdir.o
.libs
readdir
a_directory
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 09:52 AM.
 
Old 02-09-2007, 10:39 PM   #5
kshkid
Member
 
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
 
  


Reply



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 03:54 PM
See binary files trough terminal ubuntu aeruzcar Linux - Software 3 08-29-2006 07:34 PM
login looping plaircpa Slackware 3 10-31-2005 08:10 PM
View my files in windows trough FC2 Mesopotamic Linux - Newbie 4 09-19-2004 09:23 PM
looping in mplayer zexter Linux - Software 1 12-22-2003 04:39 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:40 PM.

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