qeldroma
06-04-2004, 23:18
Vorneweg, ich bin C-Neuling..
Ich sitze jetzt seit zwei Tagen dran eine verdammte Funktion zu schrieben, die mir dynamisch ein Array generiert. Das Programm soll in diesem Schritt ein Array mit MP3-Tags und den dazugehörigen Filepointern enthalten.
Dazu lasse ich in der Funktion "readArray" einen Pointer auf ein leeres Array übergeben, welches dann dynamisch vergrößert wird, entsprechend der Anzahl an MP3s'. So soll dann am Schluß in der "main()"-Funktion ein Array entsprechend meiner Einleitung zur Verfügung stehen, also ein Array mit Structs welche einen Filepointer und eine weitere Struct enthalten.
Jedoch komme ich beim "pointern" völlig durcheinander, ich weiß einfach nicht, wie ich dann in der Funktion das Array fülle. Außerdem muß noch ein Fehler in meinem "malloc()" sein, denn das Array scheint nicht neu angelegt zu werden..
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#define MODUS ,0711
#include <dirent.h>
/***********************************************
** Global Variables
***********************************************/
struct id3tag {
char ident[4];
char title[31];
char artist[31];
char album[31];
char year[5];
char comment[31];
char genre;
};
struct file_tag {
FILE *file;
struct id3tag tag;
};
/***********************************************
** Func Headers
***********************************************/
int getTag(char *, struct id3tag *);
int readArray(char *, struct file_tag *[]);
void cpString(char *, char *);
/***********************************************
** Functions
***********************************************/
int getTag(char *filename, struct id3tag *dummy){
FILE *fp;
int count=0;
if((fp = fopen( filename, "rb"))== NULL){
printf("Error while reading!\n");
return -1;
}else{
fseek(fp, -128, 2);
while(count<3) {dummy->ident[count++] = getc(fp);}
count=0;
while(count<30) {dummy->title[count++] = getc(fp);}
count=0;
while(count<30) {dummy->artist[count++] = getc(fp);}
count=0;
while(count<30) {dummy->album[count++] = getc(fp);}
count=0;
while(count<4) {dummy->year[count++] = getc(fp);}
count=0;
while(count<30) {dummy->comment[count++]= getc(fp);}
dummy->genre = getc(fp);
//Zuletzt noch die "String-Abschluß-Null
dummy->ident[3]=0;
dummy->title[30]=0;
dummy->artist[30]=0;
dummy->album[30]=0;
dummy->year[4]=0;
dummy->comment[30]=0;
}
return 0;
}
void cpString(char *first, char *second){
int i;
if(strlen(first)==strlen(second)){
for(i=0;i<strlen(first);i++){
first[i]=second[i];
}
}
}
int readArray(char *dirname, struct file_tag *firstFileTag[]){
DIR *dir;
struct dirent *dirzeiger;
int count=0;
int *zahlen;
struct id3tag dummy = {
"NOP","","","","","",0
};
//Initialisierung
//Wenn Verzeichnis vorhanden, zähle Files in count
if( (dir=opendir(dirname)) == NULL){
fprintf(stderr,"Fehler bei opendir.....\n");
exit (0);
}
while((dirzeiger=readdir(dir)) != NULL) count++;
rewinddir(dir);
//Hauptfunktion
//Übergebenes Array vergrößern entsprechend count, anschl. befüllen
firstFileTag=(struct file_tag *)calloc(count,sizeof(struct file_tag));
printf("Insgesamt %d MP3s im Verzeichnis %s\n",count,dirname);
count=0;
chdir(dirname);
while((dirzeiger=readdir(dir)) != NULL){
if(strcasestr (dirzeiger->d_name,".mp3") != NULL){
printf("----------------------------------\nFile: %s\n",dirzeiger->d_name);
getTag(dirzeiger->d_name, &dummy);
cpString(*firstFileTag[1]->tag.ident,dummy->ident);
// Auskommentiert sind im Folgenden einige Fehlversuche...
/* firstFileTag[count].tag.ident = dummy->ident;
firstFileTag[count].tag.title = dummy->title;
firstFileTag[count]->tag->title = dummy->artist;
firstFileTag[count]->tag->title = dummy->album;
firstFileTag[count]->tag->title = dummy->year;
firstFileTag[count]->tag->title = dummy->comment;
firstFileTag[count++]->tag->title = dummy->genre;
*/ }
}
//Schluß
if(closedir(dir) == -1) printf("Fehler beim Schliessen von %s\n",dirname);
return 0;
}
/***********************************************
** Main
***********************************************/
int main(int argc, char **argv)
{
struct file_tag Files[];
int tt;
if( argc != 2 ){
fprintf(stderr, "Benutzung: id3tree <Verzeichnis>\n");
return 0;
}else{
tt=readArray(argv[1], &Files);
}
return EXIT_SUCCESS;
}
Kann mir jemand helfen?
Ich sitze jetzt seit zwei Tagen dran eine verdammte Funktion zu schrieben, die mir dynamisch ein Array generiert. Das Programm soll in diesem Schritt ein Array mit MP3-Tags und den dazugehörigen Filepointern enthalten.
Dazu lasse ich in der Funktion "readArray" einen Pointer auf ein leeres Array übergeben, welches dann dynamisch vergrößert wird, entsprechend der Anzahl an MP3s'. So soll dann am Schluß in der "main()"-Funktion ein Array entsprechend meiner Einleitung zur Verfügung stehen, also ein Array mit Structs welche einen Filepointer und eine weitere Struct enthalten.
Jedoch komme ich beim "pointern" völlig durcheinander, ich weiß einfach nicht, wie ich dann in der Funktion das Array fülle. Außerdem muß noch ein Fehler in meinem "malloc()" sein, denn das Array scheint nicht neu angelegt zu werden..
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#define MODUS ,0711
#include <dirent.h>
/***********************************************
** Global Variables
***********************************************/
struct id3tag {
char ident[4];
char title[31];
char artist[31];
char album[31];
char year[5];
char comment[31];
char genre;
};
struct file_tag {
FILE *file;
struct id3tag tag;
};
/***********************************************
** Func Headers
***********************************************/
int getTag(char *, struct id3tag *);
int readArray(char *, struct file_tag *[]);
void cpString(char *, char *);
/***********************************************
** Functions
***********************************************/
int getTag(char *filename, struct id3tag *dummy){
FILE *fp;
int count=0;
if((fp = fopen( filename, "rb"))== NULL){
printf("Error while reading!\n");
return -1;
}else{
fseek(fp, -128, 2);
while(count<3) {dummy->ident[count++] = getc(fp);}
count=0;
while(count<30) {dummy->title[count++] = getc(fp);}
count=0;
while(count<30) {dummy->artist[count++] = getc(fp);}
count=0;
while(count<30) {dummy->album[count++] = getc(fp);}
count=0;
while(count<4) {dummy->year[count++] = getc(fp);}
count=0;
while(count<30) {dummy->comment[count++]= getc(fp);}
dummy->genre = getc(fp);
//Zuletzt noch die "String-Abschluß-Null
dummy->ident[3]=0;
dummy->title[30]=0;
dummy->artist[30]=0;
dummy->album[30]=0;
dummy->year[4]=0;
dummy->comment[30]=0;
}
return 0;
}
void cpString(char *first, char *second){
int i;
if(strlen(first)==strlen(second)){
for(i=0;i<strlen(first);i++){
first[i]=second[i];
}
}
}
int readArray(char *dirname, struct file_tag *firstFileTag[]){
DIR *dir;
struct dirent *dirzeiger;
int count=0;
int *zahlen;
struct id3tag dummy = {
"NOP","","","","","",0
};
//Initialisierung
//Wenn Verzeichnis vorhanden, zähle Files in count
if( (dir=opendir(dirname)) == NULL){
fprintf(stderr,"Fehler bei opendir.....\n");
exit (0);
}
while((dirzeiger=readdir(dir)) != NULL) count++;
rewinddir(dir);
//Hauptfunktion
//Übergebenes Array vergrößern entsprechend count, anschl. befüllen
firstFileTag=(struct file_tag *)calloc(count,sizeof(struct file_tag));
printf("Insgesamt %d MP3s im Verzeichnis %s\n",count,dirname);
count=0;
chdir(dirname);
while((dirzeiger=readdir(dir)) != NULL){
if(strcasestr (dirzeiger->d_name,".mp3") != NULL){
printf("----------------------------------\nFile: %s\n",dirzeiger->d_name);
getTag(dirzeiger->d_name, &dummy);
cpString(*firstFileTag[1]->tag.ident,dummy->ident);
// Auskommentiert sind im Folgenden einige Fehlversuche...
/* firstFileTag[count].tag.ident = dummy->ident;
firstFileTag[count].tag.title = dummy->title;
firstFileTag[count]->tag->title = dummy->artist;
firstFileTag[count]->tag->title = dummy->album;
firstFileTag[count]->tag->title = dummy->year;
firstFileTag[count]->tag->title = dummy->comment;
firstFileTag[count++]->tag->title = dummy->genre;
*/ }
}
//Schluß
if(closedir(dir) == -1) printf("Fehler beim Schliessen von %s\n",dirname);
return 0;
}
/***********************************************
** Main
***********************************************/
int main(int argc, char **argv)
{
struct file_tag Files[];
int tt;
if( argc != 2 ){
fprintf(stderr, "Benutzung: id3tree <Verzeichnis>\n");
return 0;
}else{
tt=readArray(argv[1], &Files);
}
return EXIT_SUCCESS;
}
Kann mir jemand helfen?