mtzoumas Δημοσ. 28 Ιουλίου 2008 Δημοσ. 28 Ιουλίου 2008 Γεία σας, έχω φτιάξει ένα πρόγραμμα στην c το οποίο θα διαβάζει κάποια directories από κάποιο text αρχείο, θα τα ανοίγει και θα κάνει μετονομασία τα αρχεία. Το πρόβλημα μου όμως είναι ότι όσα directories και αν έχω στο αρχείο το πρόγραμμα μου ανοίγει μόνο το τελευταίο στη λίστα ενώ αν στο αρχείο υπάρχει μόνο ένα δεν υπάρχει κανένα πρόβλημα. Ξέρετε τι μπορεί να φταίει????
mtzoumas Δημοσ. 28 Ιουλίου 2008 Μέλος Δημοσ. 28 Ιουλίου 2008 Αυτός είναι ο κώδικάς μου >#include <stdlib.h> #include <stddef.h> #include <stdio.h> #include <sys/types.h> #include <dirent.h> #include <string.h> #define no_of_files 200 #define no_of_chars 200 #define dirs 200 struct File_Renamer { char directory[no_of_chars]; char filename[no_of_chars]; char temp[no_of_chars]; }; struct File_Renamer list_of_dirs[dirs]; main() { char delims[] = "_"; char *result = NULL; char buf[400]={"\0"}; int j,i=0,files[dirs]={0}; int count_dirs=1; DIR *dp; struct dirent *ep; FILE *fp; fp=fopen("input.txt", "r"); //dirfiles=fopen("output.txt", "w"); /* count number of dirs */ while (!feof(fp)) { int rc = fgetc(fp); if (rc =='\n') count_dirs ++; } rewind(fp); printf("count_dirs= %d\n\n",count_dirs); for (i=0; i < count_dirs; i++) { fgets (list_of_dirs[i].directory , no_of_chars , fp); puts (list_of_dirs[i].directory); /* Read and print the contents of the current directory */ dp = opendir (list_of_dirs[i].directory); if (dp != NULL) { printf("Current items on \n\n"); while (ep = readdir (dp)) { if(strcmp(ep->d_name,".")==1 && strcmp(ep->d_name,"..")==1 && strcmp(ep->d_name,"FileRenamer.exe")!=0) { files[count_dirs]++; sprintf(list_of_dirs[files[count_dirs]-1].filename,"%s",ep->d_name); sprintf(buf, "%d) %s",files[count_dirs], list_of_dirs[files[count_dirs]-1].filename); puts (buf); } } (void) closedir (dp); printf("\n___________________________________________________________\n"); printf("\n%d items was found\n",files[count_dirs]); printf("\n___________________________________________________________\n"); } else { perror ("Couldn't open or find the directory"); printf("\n"); } } fclose (fp); /* Replace _ with space */ /*for (i=0; i < count_dirs; i++) { for(j=0;j<files[count_dirs];j++) { i=0; strcpy(list_of_dirs[j].temp,list_of_dirs[j].filename); result = strtok( list_of_dirs[j].filename, delims ); sprintf(buf,"%s",result ); while( result != NULL ) { if(i!=0) sprintf(buf,"%s %s",buf,result ); result = strtok( NULL, delims ); //printf("%s\n",buf); strcpy(list_of_dirs[j].filename,buf); i++; } sprintf(buf,"rename %c%s%c %c%s%c",0x22,list_of_dirs[j].temp,0x22,0x22,list_of_dirs[j].filename,0x22 ); system(buf); } printf("\n\n"); for(j=0;j<files[count_dirs];j++) { printf("%s\n",list_of_dirs[j].filename); } printf("\n\n"); } */ system("pause"); }
Directx Δημοσ. 28 Ιουλίου 2008 Δημοσ. 28 Ιουλίου 2008 dp = opendir (list_of_dirs.directory); Αυτό αποτυγχάνει διότι το .directory έχει μετά την ονομασία του (πχ. DIR1) το \n terminator (DIR1\n). Για ρίξε του μια ματιά δοκιμάζοντας την αφαίρεση του trailing \n: > fgets (list_of_dirs[i].directory , no_of_chars , fp); // DIRECTX: Remove trailing \n from .directory name if(strlen(list_of_dirs[i].directory) >=1 && list_of_dirs[i].directory[strlen(list_of_dirs[i].directory)-1] == '\n') list_of_dirs[i].directory[strlen(list_of_dirs[i].directory)-1] = '\0'; puts (list_of_dirs[i].directory); Καλή τύχη.
mtzoumas Δημοσ. 28 Ιουλίου 2008 Μέλος Δημοσ. 28 Ιουλίου 2008 ευχαριστώ φίλε για τη βοήθεια. δεν είχα σκεφτεί ότι μπορεί να έφταιγε αυτό.
Προτεινόμενες αναρτήσεις
Αρχειοθετημένο
Αυτό το θέμα έχει αρχειοθετηθεί και είναι κλειστό για περαιτέρω απαντήσεις.