Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

reading a file from a directory

asd2183asd2183 Posts: 6Member
Hi all,

I have written a pgm in c to read a directory and inturn the contents of all the files in packets of 60000.after i run i get output of only one file. dont know what is the problem . Kindly check and tell me , its very urgent

#include
#include
#include
#include

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("
%s
",ent->d_name);
strcpy(song,ent->d_name);
printf("
%s",song);
strcpy(filePath,filepa);
strcat(filePath,song);
printf("
%s
",filePath);


file = fopen(filePath,"r");
if (file == NULL)
{
printf("
no 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;
}


Comments

  • HK_MP5KPDWHK_MP5KPDW Posts: 767Member ✭✭✭
    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.

    [code]
    #include
    #include
    #include
    #include

    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("
    %s
    ",ent->d_name);
    strcpy(song,ent->d_name);
    printf("
    %s",song);
    strcpy(filePath,filepa);
    strcat(filePath,song);
    printf("
    %s
    ",filePath);

    file = fopen(filePath,"r");
    if (file == NULL)
    {
    printf("
    no 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;
    }[/code]

    Now, that said you have some problems with this portion of the program:
    [code]while([color=Red]!feof(file)[/color]){
    fread(&buf1,60000,1,file);
    [color=Red]fp1 = fopen("x.mp3","w");[/color]
    fwrite(buf1,1,60000,fp1);
    //sleep(1);
    [color=Red]fclose(fp1);[/color]
    }[/code]
    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 [italic]feof[/italic] 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, [italic]feof[/italic] is going to return false, [u]this includes the very last successful read of data from a given file[/u]. 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 [italic]fread[/italic] (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.
  • nebgastnebgast Posts: 54Member
    You could also read once before the loop and then put all subsequent reads at the end of the loop after you've processed data from the previous read. In this way feof will work the way you are intending it to.
  • asd2183asd2183 Posts: 6Member
    Thank you, my requirement is to continuosly update the file and should make use of it before it upgrades next time.

    now I changed the path , I have kept the source that is folder containing songs in the directory where the code is present , so it is working fine.
Sign In or Register to comment.