请教怎样用C程序实现

sunrier 2009-12-10 09:55:29
怎样用C程序,现在E盘下有一个Music文件夹,Music文件夹下有很多个音乐文件,如1.mp3,2.mp3,3.mp3 ,......但是不清楚具体有多少个音乐文件,也不知道音乐文件名是什么,现在要实现一个C程序,在E盘根目录下创建一个和和Music里面同名的一个音乐文件,然后把这个文件移动到Music文件夹下覆盖它同名的那个文件,然后再把这个文件再移动到E盘根目录下,再把这个音乐文件的名字改成和Music文件夹剩下的音乐文件中的一个音乐文件的名字,然后再把这个音乐文件移动到Music文件夹下覆盖它同名的那个文件,然后再把这个文件移动到在E盘根目录下,以此循环,直到Music文件夹下没有文件了。所有过程用C语言实现 。请教高手
...全文
246 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaoshun123 2009-12-13
  • 打赏
  • 举报
回复
以后应该联系一下自己的表达能力。或者想想有没有什么好的形式表达。

这个“然后”也太晕人了。
jkyst 2009-12-13
  • 打赏
  • 举报
回复
看混了.学习来了.
Jaffxy 2009-12-13
  • 打赏
  • 举报
回复
这种题目,就是故意转悠,想把别人转晕,达到搞倒的目的,然后在把自己转晕,再别自己搞活。哈哈 。
fanchangyong 2009-12-13
  • 打赏
  • 举报
回复
这个,可以用system函数,来掉cmd命令.
DayDayUpCQ 2009-12-13
  • 打赏
  • 举报
回复
学习,支持。。。。。。。。。。。。。。。。。。
no109 2009-12-13
  • 打赏
  • 举报
回复
8楼神牛
sunrier 2009-12-12
  • 打赏
  • 举报
回复
8楼顶 非常感谢
jernymy 2009-12-11
  • 打赏
  • 举报
回复
删除E:\music\*.mp3, 使用的是先copy到E:\,然后在逐个覆盖,重命名等的方式。

操作方式,程序运行后,提示输入要删除的文件,请输入路径:

例如:e:\music\*.mp3

回车就可以了,前提是e:\music这个目录存在,并且里边有mp3,目前code里边没有写灵活,请先测试



/*
*************************************************************
怎样用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;
}


Wolf0403 2009-12-11
  • 打赏
  • 举报
回复
1、分步骤,1、2、3,写出来
2、画流程图
3、根据流程图,写伪代码
4、根据伪代码,编写代码,出现问题见 4.1 - 4.3,未解决跳5,解决跳4直到结束
4.1 搜索 msdn http://msdn.microsoft.com/zh-cn/library/default.aspx
4.2 搜索 Google,建议针对 vckbase (中文)、Code Project(English) 站点搜索
4.3 搜索论坛
5、搜索没有解决问题:提炼遇到的问题,脱离应用程序本身的内容成为纯粹的技术问题,找到关键问题代码段,然后论坛提问
shiweifu 2009-12-11
  • 打赏
  • 举报
回复
学习
csgdseed 2009-12-11
  • 打赏
  • 举报
回复
像绕口令
z569362161 2009-12-11
  • 打赏
  • 举报
回复
学习的来了。
macrossdzh 2009-12-11
  • 打赏
  • 举报
回复
8楼很强大!
xiaoyuchen3v 2009-12-10
  • 打赏
  • 举报
回复
这个……应该不是很困难吧……
没懂
sunrier 2009-12-10
  • 打赏
  • 举报
回复
恩,是的
selooloo 2009-12-10
  • 打赏
  • 举报
回复
有点像绕口令。。
localxiao 2009-12-10
  • 打赏
  • 举报
回复
找API中关于文件操作的函数去。。。

里面都有吧
sunrier 2009-12-10
  • 打赏
  • 举报
回复
请问哪里不明白?
garfieldking 2009-12-10
  • 打赏
  • 举报
回复
不晓得是在下语文没学好还是楼猪语文没学好。。。。。。

69,335

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧