Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Making a MP3 player with DJGPP,text file to array problem, please help

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Here is the code I have so far.


#include <stdio.h>
#include <string.h>

#define MAXLIN 150
int main()
{
FILE *f1;
char string[MAXLIN];
char got[MAXLIN];
static char mname[MAXLIN];
int fi = 0;
f1=fopen(&quot;all.m3u&quot;, &quot;r&quot;);
while ( fgets( string, MAXLIN, f1) != NULL)

if (string[0] != '#')
{
strcpy(got,strrchr(string,'\\'));
strcpy(mname,(&got[1]));

printf(&quot;%s&quot;,mname);
fi++;
}
return(0);
}


What I'm trying to do is put the mname variable into an array like umame[fi][0], so that I can then move back and forth through it in a loop,

Here is some of the all.m3u file to show structure:
#EXTM3U
#EXTINF:247,2 Unlimited - Twilight Zone
\\csgld420\MUSIC\My Music\2 Unlimited - Twilight Zone.mp3
#EXTINF:355,A-Ha - Take On Me (Tecno Mix)
\\csgld420\MUSIC\My Music\A-Ha - Take On Me (Tecno Mix).mp3
#EXTINF:216,Abc - How To Be A Millionaire
\\csgld420\MUSIC\My Music\ABC - How To Be A Millionaire.mp3
#EXTINF:209,Abc - Look Of Love
\\csgld420\MUSIC\My Music\ABC - Look of Love.mp3
#EXTINF:265,Abc - When Smokey Sings
\\csgld420\MUSIC\My Music\ABC - When Smokey Sings.mp3
#EXTINF:343,AC\DC - For Those About To Rock
\\csgld420\MUSIC\My Music\ACDC - For Those About To Rock.mp3
#EXTINF:211,AC\DC - You Shook Me All Night Long
\\csgld420\MUSIC\My Music\ACDC - You Shook Me All Night Long.mp3
#EXTINF:211,Ace Of Base - All That She Wants
\\csgld420\MUSIC\My Music\Ace Of Base - All That She Wants.mp3

Now my code gives me almost what I want, but I can't seem to put it into any array, I get various errors.

Here is an example of the output from the code I have so far:

2 Unlimited - Twilight Zone.mp3
A-Ha - Take On Me (Tecno Mix).mp3
ABC - How To Be A Millionaire.mp3
ABC - Look of Love.mp3
ABC - When Smokey Sings.mp3
ACDC - For Those About To Rock.mp3
ACDC - You Shook Me All Night Long.mp3
Ace Of Base - All That She Wants.mp3

Which is fine, but I need to wack the CR and the &quot;.mp3&quot; off the end still.

I want to be able to call the array like this in the end.

printf(&quot;%s&quot;,newname[fi]); /* or newname[32] */
load_amp(newname[fi],0);

I have tried:
strcpy(newname[fi],mname); but it errors and GPF when ran
newname[fi] = mname; same as above

I need the info to look like this in the end:
newname[0] = 2 Unlimited - Twilight Zone
newname[1] = A-Ha - Take On Me (Tecno Mix)
newname[2] = ABC - How To Be A Millionaire
newname[3] = ABC - Look of Love
newname[4] = ABC - When Smokey Sings
newname[5] = ACDC - For Those About To Rock
newname[6] = ACDC - You Shook Me All Night Long
newname[7] = Ace Of Base - All That She Wants

or newname[0][0] = 2 Unlimited - Twilight Zone

I'm not sure,

Please tell me what I'm doing wrong.

If its all wrong, please tell me that as well.
Thank you in advance for any help you can give me.
Scott

 
friend this is what you want ,compile it and enjoy
this is one way with arrays
but it can be done more efficiently with dynamic allocation
but this is ok


#include <stdio.h>
#include <string.h>

#define MAXLIN 250
#define MAXLINE 250 //this refers to the max line you
//have in your mp3 file

int main()
{
FILE *f1;
char string[MAXLIN];
char got[MAXLINE][MAXLIN];
char mname[MAXLIN];
char *ptr;
int fi = 0;
f1=fopen(&quot;all.m3u&quot;, &quot;r&quot;);
while (!feof(f1))
{

fgets(string,MAXLIN,f1) ;
if(string[0]!='#')
{
strcpy(got[fi],strrchr(string,'\\'));
strcpy(mname,got[fi]+1);
ptr=strstr(mname,&quot;.mp3&quot;);
*ptr='\0';
strcat(mname,&quot;\n&quot;);
strcpy(got[fi],mname);
fi++;
}
}
for(int i=0;i<(fi-1);i++)
printf(&quot;line[%d] :%s&quot;,i,got);
return(0);
}


reply if it was helpful
 
here's a lil'sample using dynamic allocation.

Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>


int main(){
    char **p;


	p = (char **) malloc(sizeof(char*));


	if(p != NULL)
		p[0] = strdup(&quot;hola1&quot;);
    else
        return -1;


	printf(&quot;%s\n&quot;,p[0]);

	p = (char **) realloc(p,4*sizeof(char*));
	if(p != NULL){
		p[1] = strdup(&quot;hola2&quot;);
		p[2] = strdup(&quot;hola3&quot;);
		p[3] = strdup(&quot;hola4&quot;);
		//p[4] = strdup(&quot;hola5&quot;); if you add this line when you try to free() the ptr you'll get an error
                                  // i think that error is b/c the memory block size is exceded
    }else
        return -1;


	printf(&quot;\n&quot;);

	printf(&quot;%s\n&quot;,p[0]);
	printf(&quot;%s\n&quot;,p[1]);
	printf(&quot;%s\n&quot;,p[2]);
	printf(&quot;%s\n&quot;,p[3]);


	free(p);

	return 0;
}

Hope it can help ......... ::)
bluenote@uyuyuy.com
(excuse my english)
 
bluenote,

first the reason of error its because of array index
out of bound ,you started from index 1 use index o to
start and the problem finishes

second the programme which you wrote is excellent to start
with but to solve Mac's problem you have to allocate two
blocks of memory from heap using malloc(or calloc) and
make the string as you require.this is more efficient than
using malloc

correct me if i am wrong
bye
yogesh
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top