69,335
社区成员
发帖
与我相关
我的任务
分享
/*
*************************************************************
怎样用C程序,
1. 现在E盘下有一个Music文件夹,
2. Music文件夹下有很多个音乐文件,
3. 如1.mp3,2.mp3,3.mp3 ,......但是不清楚具体有多少个音乐文件,
也不知道音乐文件名是什么,
4. 现在要实现一个C程序,
5. 在E盘根目录下创建一个和Music里面同名的一个音乐文件,
6. 然后把这个文件移动到Music文件夹下覆盖它同名的那个文件,
7. 然后再把这个文件再移动到E盘根目录下,
8. 再把这个音乐文件的名字改成和Music文件夹剩下的音乐文件中的一个
音乐文件的名字,
9. 然后再把这个音乐文件移动到Music文件夹下覆盖它同名的那个文件,
10. 然后再把这个文件移动到在E盘根目录下,
11. 以此循环,直到Music文件夹下没有文件了。
所有过程用C语言实现 。请教高手
*************************************************************
FileName : testfile.c
FileFunc : for file operate
version : V0.1
Author : jernymy
version : 2009-12-10
*************************************************************
*/
#include <stdio.h>
#include <string.h>
#include <io.h>
#include <conio.h> // getch();
#include <malloc.h>
// extra name
struct _finddata_t tTmpFile;
#define FILE_PATH (const char *)"E:\\"
#define MUSIC_PATH (const char *)"E:\\MUSIC\\"
// Root file name, e:\1111.mp3
char *pchRootFN = NULL;
// Music dir file name, e:\music\1111.mp3
char *pchMusicFN = NULL;
// backup root file name, e:\1111.mp3
char *pchBakFN = NULL;
#define TASSERT_RET(a, b, ret)\
if (a < b)\
{\
printf("\nreturn value : %d is error !\n", a);\
return ret;\
}
// test free file name memory function, use double pointer
void TFreeFileMem(char **chFileName)
{
if (NULL != (*chFileName))
{
free((*chFileName));
(*chFileName) = NULL;
}
}
// create new file at "E:\" or "E:\Music\" with get filename
int TCreateFile(char *pchFileName)
{
FILE *fpFile = NULL;
fpFile = fopen(pchFileName, "w");
if (NULL == fpFile)
{
printf("can not create file: %s, exit !\n", pchFileName);
TFreeFileMem(&pchFileName);
return -1;
}
fclose(fpFile);
return 0;
}
// test get file function, use malloc function
int TGetFileName(const char *pchPath, int nLen, char **ppchFileName)
{
if (NULL == ppchFileName)
{
printf("pointer error, exit !\n");
return -1;
}
// malloc for new file name
(*ppchFileName) = (char *)malloc(nLen * sizeof(char));
if (NULL == (*ppchFileName))
{
printf("malloc for file:(%s%s)failed!\n", pchPath, tTmpFile.name);
return -1;
}
memset((*ppchFileName), 0, nLen * sizeof(char));
sprintf((*ppchFileName), "%s%s", pchPath, tTmpFile.name);
return 1;
}
// test file copy function, means remove
int TFileCopy(const char *pchDstFN, const char *pchSrcFN)
{
int c;
FILE *fpSrc;
FILE *fpDst;
fpSrc = fopen(pchSrcFN, "rb");
if (NULL == fpSrc)
{
printf("open file: %s error, may be not create it ?\n", pchSrcFN);
return -1;
}
fpDst = fopen(pchDstFN, "wb");
if (NULL == fpDst)
{
printf("open file: %s error, may be not create it ?\n", pchDstFN);
fclose(fpSrc);
return -1;
}
// for file copy from fpSrc to fpDst
c = fgetc(fpSrc);
while (EOF != c)
{
fputc(c, fpDst);
c = fgetc(fpSrc);
}
printf("copy file from %s to %s finish\n", pchSrcFN, pchDstFN);
fclose(fpSrc);
fclose(fpDst);
return 1;
}
// test file delete function, means remove
int TFileDelete(const char *pchFileName)
{
if (0 == remove(pchFileName))
{
printf("Removed %s successful.\n\n", pchFileName);
}
else
{
printf("Removed %s failed.\n\n", pchFileName);
}
return 0;
}
// test file rename function
void TFileRename(void)
{
if (0 == rename(pchBakFN, pchRootFN))
{
printf("Rename %s to %s successful.\n", pchBakFN, pchRootFN);
}
else
{
printf("Rename %s to %s failed.\n", pchBakFN, pchRootFN);
}
TFreeFileMem(&pchBakFN);
}
// test file move function
int TFileMove(int nFlag)
{
int nRet;
int nLen;
nLen = strlen(tTmpFile.name);
// get e:\.mp3 file name
nRet = TGetFileName(FILE_PATH, nLen+1+strlen(FILE_PATH), &pchRootFN);
TASSERT_RET(nRet, 0, -1);
if (nFlag) // this flag for rename function is used.
{
TFileRename();
}
// backup for root file name
nRet = TGetFileName(FILE_PATH, nLen+1+strlen(FILE_PATH), &pchBakFN);
TASSERT_RET(nRet, 0, -1);
nRet = TCreateFile(pchRootFN); // create root mp3 file name
TASSERT_RET(nRet, 0, -1);
// get music dir file name, but not create
nRet = TGetFileName(MUSIC_PATH, nLen+1+strlen(MUSIC_PATH), &pchMusicFN);
if (nRet > 0)
{
/* not TFileCopy and TFileDelete used for move function */
TFileCopy(pchMusicFN, pchRootFN); // copy e:\a.mp3 to e:\music\a.mp3
TFileDelete(pchRootFN); // delete e:\a.mp3
TFileCopy(pchRootFN, pchMusicFN); // copy e:\music\a.mp3 to e:\a.mp3
TFileDelete(pchMusicFN); // delete e:\music\a.mp3
TFreeFileMem(&pchMusicFN); // for protect free memory
}
TFreeFileMem(&pchRootFN); // for protect free memory
return 0;
}
int main(void)
{
long hFile;
char chFileType[20] = {0}; // here please input e:\music\*.mp3
int nCount = 0;
printf("Input your fild file type: ");
scanf("%s", chFileType);
/* Find first *.x file in current directory */
hFile = _findfirst(chFileType, &tTmpFile);
if (-1L == hFile)
{
printf( "No %s files in current directory!\n", chFileType);
}
else
{
TFileMove(0); // first move file, not rename function
nCount++;
/* Find the rest of the *.x files */
while(0 == _findnext(hFile, &tTmpFile))
{
TFileMove(1); // other move file, have rename function
nCount++;
}
TFileDelete(pchBakFN); // delete the last ".mp3" file in "e:\"
TFreeFileMem(&pchMusicFN); // for protect free memory, had free before
TFreeFileMem(&pchRootFN); // for protect free memory, had free before
TFreeFileMem(&pchBakFN); // for protect free memory, only free this ok
_findclose(hFile);
}
printf("\ntotal %s number is: %d\n", chFileType, nCount);
getch();
return 0;
}