回 帖 发 新 帖 刷新版面

主题:C++好手帮我改一下这个程序啊 问题多多  紧急啊~~~

#pragma   warning (disable: 4786)//由于vc6.0对STL支持比较差
#include <iostream>//输入输出及其相关方法
#include <string>//字符串类std::string及其相关方法
#include <list>//STL链表类std::list<T>及其相关的方法
#include <windows.h>
#include<io.h>
#include<stdio.h>


using namespace std;//标识符的各种可见范围

int GetAllFileName(string strPath, list<string> &lsName);//strPath:申明了一个字符串变量strpath来传递一个文件名然后给这个变量然赋值
bool DelFile(list<string> &lsName, string strFilePath, string strFileName);
bool CreFile(string strFilePath, string strFileName);


void filesearch(string path,int layer)
{
    struct _finddata_t filefind;
    string curr=path+"\\*.*";
    int done=0,i,handle;
    if((handle=_findfirst(curr.c_str(),&filefind))==-1)
        return;
    while(!(done=_findnext(handle,&filefind)))
    {
        if(!strcmp(filefind.name,".."))
            continue;
        for(i=0;i<layer;i++)cout<<" -  ";
        if ((_A_SUBDIR==filefind.attrib))
        {     
            cout<<filefind.name<<"(dir)"<<endl;
            curr=path+"\\"+filefind.name;
            filesearch(curr,layer+1);
        }
        else 
        {
            cout<<curr.c_str()<<filefind.name<<endl;
        }
    }    
    _findclose(handle);      
}

int main(int argc, char *argv[])//假设程序编译后产生abc.exe文件,在dos下以abc aaa形式执行

{

    // 获取目录路径
    cout << "请输入文件名:" << endl;    
    char szFilePath[MAX_PATH] = {0};
    cin.getline(szFilePath, MAX_PATH);
    // 获取所有指定目录下的文件名

    list<string>::iterator iter;
    list<string> lsName;
filesearch(szFilePath,0);   
       
  // 删除文件
 int count = GetAllFileName(szFilePath, lsName);
    cout << "请输入你要删除的文件名:" << endl;
    char szDelFile[MAX_PATH] = {0};
    cin.getline(szDelFile, MAX_PATH);
    if (DelFile(lsName, szFilePath, szDelFile))
    {
        cout << "成功删除" << endl;
    }
    else
        cout<<"文件不存在"<<endl;

    // 创建文件
    cout << "请输入你要创建的文件名:" << endl;
    char szCreFile[MAX_PATH] = {0};
    cin.getline(szCreFile, MAX_PATH);
    if (CreFile(szFilePath, szCreFile))
    {
        cout << "创建成功" << endl;
    }

    return 0;
}

int GetAllFileName(string strPath, list<string> &lsName)
{
    if (strPath.empty())
    {
        return -1;
    }

    int nFileCount = 0;
    WIN32_FIND_DATA finder;
    string strFile = strPath + "\\*.*";
    HANDLE hFile = FindFirstFile(strFile.c_str(), &finder);
    if (INVALID_HANDLE_VALUE != hFile)
    {
        do
        {
            lsName.push_back(finder.cFileName);
            nFileCount++;
        }while(FindNextFile(hFile, &finder) != 0);
    }
    FindClose(hFile);

    return nFileCount-2; // 减去当前目录和上级目录
}


bool DelFile(list<string> &lsName, string strFilePath, string strFileName)
{
    bool bRet = false;
    if (lsName.empty() || strFileName.empty())
    {
        return bRet;
    }

    list<string>::iterator iter;

    for (iter = lsName.begin(); iter != lsName.end(); ++iter)
    {
        if (strFileName.compare(*iter) == 0)
        {
            string strTemp = strFilePath + "\\" + strFileName;
            if (DeleteFile(strTemp.c_str()))
            {
                lsName.erase(iter);        // 删除list容器中的文件名
                bRet = true;
                break;
            }
        }
    }

    return bRet;
}

bool CreFile(string strFilePath, string strFileName)
{
    bool bRet = false;
    if (strFilePath.empty() || strFileName.empty())
    {
        return bRet;
    }

    string strTemp = strFilePath + "\\" + strFileName;
    if (CreateFile(strTemp.c_str(),
                    GENERIC_READ | GENERIC_WRITE,  
                    0,
                    NULL,
                    CREATE_NEW,
                    NULL,
                    NULL) != INVALID_HANDLE_VALUE)
    {
        bRet = true;
    }
    
    return bRet;
}

回复列表 (共5个回复)

沙发

删除文件那里有很大问题。。。

板凳

菜鸟新手啥也不懂,求帮助!!

3 楼

看的晕乎乎的,高手快来吧。















SIGNATURE:-----------------------------------
Imagination is more important than knowledge. 
[url=http://www.sortboots.com/ugg-bailey-button-triplet-c-34.html]ugg bailey button triplet[/url]     ,    [url=http://www.sortboots.com/ugg-bailey-button-bomber-c-37.html]ugg bailey button bomber[/url]     ,      [url=http://www.sortboots.com/ugg-classic-short-c-39.html]ugg classic short[/url]
 

4 楼

NOT all that Mrs. Bennet, however, with the assistance of [url=http://www.timberlandforyou.com/]mens timberland boots[/url] her five daughters, could ask on the subject was sufficient to draw from her husband any satisfactory description of Mr. Bingley. They attacked him in various ways; with [url=http://www.timberlandforyou.com/]timberland 6 inch boots[/url] barefaced questions, ingenious suppositions, and distant surmises; but he eluded the skill of them all; and they were at last obliged to accept the second-hand intelligence of their [url=http://www.timberlandforyou.com/]timberland roll top[/url] neighbour Lady Lucas. Her report was highly favourable. Sir William had been delighted with him. He was quite young, wonderfully [url=http://www.timberlandforyou.com/]timberland roll top boot[/url] handsome, extremely agreeable, and, to crown the whole, he meant to be at the next assembly with a large party. Nothing could be more delightful! To be fond of dancing was a certain step towards [url=http://www.timberlandforyou.com/]timberland roll top boot[/url] falling in love; and very lively hopes of Mr. Bingley's heart were entertained.;If I can but see one of my daughters happily settled at Netherfield,; said Mrs. Bennet to her husband, ;and all the others [url=http://www.timberlandforyou.com/]Womens 14-Inch Premium Waterproof Boots[/url] equally well married, I shall have nothing to wish for.;In a few days Mr. Bingley returned Mr. Bennet's visit, and sat about ten minutes with him in his library. He had entertained [url=http://www.timberlandforyou.com/]shop timberland boots[/url] hopes of being admitted to a sight of the young ladies, of whose beauty he had heard much; but he saw only the father. The ladies were somewhat more fortunate, for they had the advantage [url=http://www.timberlandforyou.com/]timberland men boots[/url] of ascertaining, from an upper window, that he wore a blue coat and rode a black horse.An invitation to dinner was soon afterwards dispatched; and already had Mrs. Bennet planned the courses that were [url=http://www.timberlandforyou.com/]timberlands boots[/url] to do credit to her housekeeping, when an answer arrived which deferred it all. Mr. Bingley was obliged to be in town the following day, and consequently unable to accept the honour of their [url=http://www.timberlandforyou.com/]timberland boots on sale[/url] invitation, c. Mrs. Bennet was quite disconcerted. She could not imagine what business he could have in town so soon after his arrival in Hertfordshire; and she began to fear that he might [url=http://www.timberlandforyou.com/]cheap timberland boots[/url] be always flying about from one place to another, and never settled at Netherfield as he ought to be. Lady Lucas quieted her fears a little by starting the idea of his being gone to London only to [url=http://www.timberlandforyou.com/]timberland boots on sale[/url] get a large party for the ball; and a report soon followed that Mr. Bingley was to bring twelve ladies and seven gentlemen with him [url=http://www.timberlandforyou.com/]womens timberland boots[/url] to the assembly.
[url=http://www.timberlandforyou.com/]timberland 14 inch boots[/url] 
[url=http://www.timberlandforyou.com/]timberland working boots[/url] 
[url=http://www.timberlandforyou.com/]mens timberland boots[/url] zxj

5 楼

我在vs2005下编译了一遍,
GetAllFileName函数里面的lsName.push_back(finder.cFileName);编译提示finder.cFileName 是char* 不能转化为 string,
改为
lsName.push_back(string(finder.cFileName));
编译通过,测试之后功能没什么问题。

我来回复

您尚未登录,请登录后再回复。点此登录或注册