Hi everybody I am not expert in C, and i want to do somethings by help of c programming i hope you can help me. I have a directory in my hard disk which containd thousands of video files. I want to extract the file names one by one and create a folder somewhere else and copy the file in that folder. Can you guide me how to do it in C language?:worried:
There's nothing in C itself; you will have to use a platform-specific system call. Trouble is, you haven't said what platform.........
I am on windows platform. One solution that came in my mind is using "system" function in C to ask OS to do my request as can be seen below: int result; result = system("dir c:\\video > c:\\video\\dirlist.txt"); FILE *fp; if((fp=fopen("c:\\video\\dirlist.txt", "r"))==NULL) ... By parsing the content of txt file i can achieve my request. But C has other functions such as exec, fork and popen. I am looking for the most efficient one. I should do the following scenario: 1-pars the .txt file content and get the video file name. 2- run an exe file (a program that extract pictures from video) over the video file and send the resulted images in a directory. 3- process the images in the directory. The above process should be in a loop and iterated for more that 1000 times. So the compiler should wait to finish each part of process and then continue next step. I hope i have explained the problem well. Whats your advice?
Well starting a new process to read a directory and write it to a file, then to read that file, is definitely not the most efficient approach. If you're prepared to put up with that overhead then it really doesn't matter which variant on system you use. Anyway if you're on Windows you can use the WinAPI functions for parsing a directory, but they're tricky to figure out and even I just copy stuff from MSDN. Here's a function from one of my projects: Code: void recurseDir(const char *dir,int recfunc) { WIN32_FIND_DATA FindFileData; HANDLE hFind; int bFin=0; char startDir[1024]; strcpy_s(startDir,1024,dir); strcat_s(startDir,1024,"\\*.*"); hFind=FindFirstFile(startDir,&FindFileData); while (!bFin) { if (hFind==INVALID_HANDLE_VALUE) bFin=1; else { if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { // skip . and .. if (strcmp(FindFileData.cFileName,".") && strcmp(FindFileData.cFileName,"..")) { char loc[1024]; strcpy_s(loc,1024,dir); strcat_s(loc,1024,"\\"); strcat_s(loc,1024,FindFileData.cFileName); if (recurseFuncData.skipSkipDirs && skipDir(FindFileData.cFileName,loc)) { //printf("Skip dir '%s'\n",loc); } else { //printf("Recurse into directory '%s'\n",loc); recurseFuncData.dirCount++; recurseDir(loc,recfunc); } } } else { //printf("Found file '%s'\n",FindFileData.cFileName); //recurseFuncData.fileCount++; strcpy_s(recurseFuncData.fromDir,1024,dir); if (!recurseFunc(recfunc,&FindFileData)) bFin=1; } } if (!FindNextFile(hFind,&FindFileData)) bFin=1; } FindClose(hFind); } This won't compile as is - you don't have the recurseFuncData structure or the skipDir() and recurseFunc() functions. I've left them in so you can see how it works and you'll need to at least comment them out to make it work. If you don't want to recurse over a whole directory tree then just remove the if (strcmp...) block, leaving the FILE_ATTRIBUTE_DIRECTORY block empty (or negate the test and move the else block into the if block).
Thanks for your advice. The story is that i should use a video processing open source program called "ffmpe". it is a command line program. I want to run ffmpeg for more than one thousand time by "system" command in C in windows platform. the model of calling ffmpeg is like below. IndexFunction is a very complicated and memory consumptive function. while(){ system("d:\\ffmpeg -i ..........); IndexFunction(); } Do you think it will work? Do you have any suggestion and advice? Would you let me know how can i free the memory used by ffmpeg after each run?
Well if you can construct programatically commands like "d:\\ffmpeg -i ..." and they work from the command line (which would be the place to test them) then there should be no problem launching them from system(). What makes you think the memory used by ffmpeg won't be freed?
if you are going though all the 1000 files looping a ffmpeg, you'll run into memory i think becuase somtimes ffmoeg has that bad habbit of locking files and not finishing the process when there are multiple instances i dont know why, but better manage the code carefully
Yeah but you would only hit that if you ran multiple system() calls simultaneously via multithreading, not sequentially in a loop like Code: for (i=0; i<1000; i++) system("ffmpeg..."); Locks and allocated memory are freed when the process exits, so this is a leak: Code: int main() { char *foo=malloc(10000000); foo=""; only until this point: Code: return 0; } Memory leaks and locked resources are only an issue when processes insist on not exiting.
thanks for giving advices. But i still need to know that when i run the below code: when exe file is running and pass the stream process to the ffmpeg through "system" command, naturally then call "IndexFunction" and continue the program process to perform the function lines of codes. But i need a halt for finishing ffmpeg command then after finishing this process stream , the main program process continues and goes to "IndexFunction". I want something like interpreter here, step by step performing requestes. and after finishing one process the next step process start. for this purpose what should i do in C?
In the symbolic above loop we have two main process: 1-"ffmpeg" process: ffmpeg is a program which has been called by "system" and controled by O.S. This program take a long time to get end. 2-"indexFunction" process which is a function defined by me in C code. This function calculate indexes according to an algorithm and take a long time to be completed to. my question is that when the above program is running, when it pass the ffmpeg to O.S to run the ffmpeg.exe, then before that ffmpeg process get to its end, the program run the function "IndexingFunction" or no? I am interested to start "IndexFunction" when the "ffmpeg" finishes its process. How can i do this? How can i halt the running whole program when it get to "system(d:\ffmpeg...) and when this line finished then continue the rest of the program lne? (something like interpreters)
In another word I need a function in C that runs an external executive program (like "system" command) which return a distinct value when the executive program process has been ended.
In IEEE reference i have read the below text about system() function in C: And also it mentioned that the child process should be terminated when called by system(): As can be seen , you are right , but i am right too. The child process should be terminated before continuing the main process. But if an error signal or interrupt happen, then seems that program may loose something in this case. Thats why i want to find a robust method for calling a child process.
Signal Errors are important for my programs. BTW how can i implement a formatted string which converted from an integer in c or c++? i want something like printf("%3d",number) but i need to strcat the string to another string
system() is pretty robust; what *exactly* doesn't work as you want if you use it? Specify something more exact than "the program may lose something" - what exactly DOES it lose under exactly what circumstances? I've no way of knowing what "more robust" call is going to work if I don't know exactly what system() can't cope with. printf is sufficient and you can do a combined printf and strcat in one easy operation under the right circumstances: Code: Untested! My compiler is in the middle of something else ATM. char str[128]; int n=15; strcpy(str,"The number is "); sprintf(str+strlen(str),"%d",n); **WARNING** CHECK FOR BUFFER OVERFLOW **WARNING** puts(str); // output should be: The number is 15
Thanks. What i want for converting int to string or char*, actually is a formatted string. I need to show in three character string which if the number is less than 3 character, it filled with zero at left. like: string 001 for 1 string 023 for 23 string 431 for 431 ant i want it in the middle of another string. like: strcat(str1, int-2-string); whats your suggestion? I have seen it is very easy in C sharp but in C or C++ it seems i should write some lines for this formatted output. am i right?