|
11-12-2011, 12:45
|
|
|
|
חבר מתאריך: 21.12.04
הודעות: 30,021
|
|
[עזרה] בעיה עם תוכנית בשפת C - העתקת קבצים
היי חברה'.
יש לי בעיה שמציקה לי כבר שבוע. אני מנסה לכתוב תוכנית פשוטה שתעתיק קבצים מתיקיה א' לתיקיה ב', מתוך רשימה בקובץ טקסט.
מה הצלחתי לעשות?
להעתיק את הקבצים תו אחרי תו. זה עובד יפה, אבל מאוד מאוד איטי. יש כנראה יותר מדי קריאות לדיסק:
קוד PHP:
#include <sys/stat.h> #include <sys/types.h> #include <cstdlib> #include <iostream> #include <sys/file.h>
using namespace std;
int main(int argc, char *argv[]) { FILE * listFile, * sourceFile, * destFile; char filename[100], ch, dir_path[150], * buffer, destination[250]; int lSize, cur_pos; if (argc == 2) { listFile = fopen (argv[1], "r"); } else { listFile = fopen ("files.txt","r"); } if (listFile != NULL) { if (fgets ( dir_path, sizeof dir_path, listFile ) != NULL ) /* read album description */ { dir_path[strlen(dir_path)-1]='\0'; cout << "Trying to create directory '" << dir_path << "'.\n"; if (!mkdir(dir_path)) { cout << "Directory '" << dir_path << "' successfuly created.\n"; while ( fgets ( filename, sizeof filename, listFile ) != NULL ) /* read a line */ { filename[strlen(filename)-1]='\0'; sourceFile = fopen(filename,"r"); if (sourceFile != NULL) { // obtain file size: fseek (sourceFile , 0 , SEEK_END); lSize = ftell (sourceFile); rewind (sourceFile); sprintf(destination, "%s%s%s", dir_path , "\\" , filename); destFile = fopen(destination, "wb"); if (destFile != NULL) { buffer = (char*) malloc (sizeof(char)*lSize); cur_pos = 0; while(cur_pos < lSize) { fread (buffer, 1, 1, sourceFile); fwrite(buffer, 1, 1, destFile); cur_pos = ftell(sourceFile); } cout << "\tOK\n"; free(buffer); } else { cout << "Error"<< endl; } fclose ( destFile ); } else { cout << "Error, source file cannot be opened: " << filename << endl; } fclose ( sourceFile ); } } else { cout << "Error creating directory. Either you do not have permissions\nor directory already exist." << endl; } } else { cout << "Error getting album description. Selection file empty?" << endl; } } else { cout << "Error! No selection list file found (default: snlist.txt)"; } fclose ( listFile ); system("PAUSE"); return 0; }
ניסיתי לשנות את לולאת הקריאה והכתיבה שתיקח יותר מ char אחד בכל פעם (למעשה, ניסיתי לקרוא בבת-אחת את כל הקובץ, ואז לכתוב אותו בבת-אחת לקובץ החדש) אבל זה לא עובד לי.
קוד PHP:
#include <sys/stat.h> #include <sys/types.h> #include <cstdlib> #include <iostream> #include <sys/file.h>
using namespace std;
int main(int argc, char *argv[]) { FILE * listFile, * sourceFile, * destFile; char filename[100], ch, dir_path[150], * buffer, destination[250]; int lSize, cur_pos; if (argc == 2) { listFile = fopen (argv[1], "r"); } else { listFile = fopen ("files.txt","r"); } if (listFile != NULL) { if (fgets ( dir_path, sizeof dir_path, listFile ) != NULL ) /* read album description */ { dir_path[strlen(dir_path)-1]='\0'; cout << "Trying to create directory '" << dir_path << "'.\n"; if (!mkdir(dir_path)) { cout << "Directory '" << dir_path << "' successfuly created.\n"; while ( fgets ( filename, sizeof filename, listFile ) != NULL ) /* read a line */ { filename[strlen(filename)-1]='\0'; sourceFile = fopen(filename,"r"); if (sourceFile != NULL) { // obtain file size: fseek (sourceFile , 0 , SEEK_END); lSize = ftell (sourceFile); rewind (sourceFile); cout << "source file size: " << lSize << endl; cout << "Copying file " << filename << " ..."; sprintf(destination, "%s%s%s", dir_path , "\\" , filename); destFile = fopen(destination, "wb"); if (destFile != NULL) { buffer = (char*) malloc (lSize); while (fgets (buffer, lSize, sourceFile ) != NULL) { fputs (buffer, destFile ); cout << sizeof (buffer); } cout << "\tOK\n"; free(buffer); } else { cout << "Error"<< endl; } fclose ( destFile ); } else { cout << "Error, source file cannot be opened: " << filename << endl; } fclose ( sourceFile ); } } else { cout << "Error creating directory. Either you do not have permissions\nor directory already exist." << endl; } } else { cout << "Error getting album description. Selection file empty?" << endl; } } else { cout << "Error! No selection list file found (default: snlist.txt)"; } fclose ( listFile ); system("PAUSE"); return 0; }
אשמח אם מישהו יתקן אותי איפה טעיתי, או יציע לי פתרון אחר להעתקת קבצים בצורה מהירה בשפות ++C / C.
תודה מראש
נערך לאחרונה ע"י Narxx בתאריך 11-12-2011 בשעה 12:48.
|
|