C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28695
Number of posts: 94715

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
reading a file from a directory Posted by asd2183 on 17 Jan 2011 at 3:19 AM
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 <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;
}


Report
Re: reading a file from a directory Posted by HK_MP5KPDW on 18 Jan 2011 at 11:05 AM
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.
Report
Re: reading a file from a directory Posted by nebgast on 18 Jan 2011 at 7:40 PM
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.
Report
Re: reading a file from a directory Posted by asd2183 on 23 Jan 2011 at 3:54 AM
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.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.