Please use code tags and format code when posting code samples... as an example, here is what your code looks like when this is done.
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include<string.h>
int main()
{
//char n[20];
int n;
char filePath[30];
const char filepa[20]="/home/shashi/sun/";
unsigned char funcall[100] = "mplayer ";
FILE *fp1;
long sock,i; /* Socket descriptor */
// const char dirname[20] = " moon/";
char song[30];
char temp[20];
char songname[20] = "x.mp3 ";
char tmp_funccall[100];
FILE *file;
unsigned long buf1[200000];
ent = readdir(d);
while(ent){
//while((ent->d_name) != 1){
x = (strcmp(ent->d_name,"..")) && (strcmp(ent->d_name,"."));
if(x != 0)
{
printf("\n%s\n",ent->d_name);
strcpy(song,ent->d_name);
printf("\n%s",song);
strcpy(filePath,filepa);
strcat(filePath,song);
printf("\n%s\n",filePath);
file = fopen(filePath,"r");
if (file == NULL)
{
printf("\nno file");
exit(0);
}
while(!feof(file)){
fread(&buf1,60000,1,file);
fp1 = fopen("x.mp3","w");
fwrite(buf1,1,60000,fp1);
//sleep(1);
fclose(fp1);
}
}
ent = readdir(d);
}
closedir(d);
//exit(0);
//return 0;
}
Now, that said you have some problems with this portion of the program:
while(!feof(file)){
fread(&buf1,60000,1,file);
fp1 = fopen("x.mp3","w");
fwrite(buf1,1,60000,fp1);
//sleep(1);
fclose(fp1);
}
The first problem is that you are opening and closing the file within the body of that loop which means that each time you execute it, the previous file gets erased and overwritten with the new data. There will only ever be one file with one single chunk of data because of this.
The second problem is your use of
feof to control the execution of the loop. The problem is that the EOF flag is not set until you attempt to read beyond the end-of-file. As long as a read operation has got a chunk of data,
feof is going to return false,
this includes the very last successful read of data from a given file. What happens in your code is that when the last chunk of data is read from the input file (you are physically at the end of the file), the EOF test still returns false and you enter the loop one more time than is necessary even though you should be stopping. Once you are in the loop it is too late and you attempt another
fread (which fails and sets the EOF flag to true) which likely leaves the buffer unchanged since the previous run. Since you're already in the loop, you keep going processing data writing to the output file. The solution to this is to test the read operation instead of EOF.