Thread: Directory access problem...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    673

    Directory access problem...

    Ok, I believe it has something to do with the dirent.h code, but I may be mistaken.
    This is the function,
    Code:
    void X_Crypt::PurgeReports()
    {
        DIR *d;
        dirent *dir;
    
        std::string file;
    
        d = opendir("C:\\C++Projects\\X_Crypt\\reports\\");
        if (d)
        {
            while ((dir = readdir(d)) != NULL)
            {
                file = dir->d_name;
                if ( file.substr(file.size()-4) == ".xtr" )
                {
                    file = "C:\\C++Projects\\X_Crypt\\reports\\" + file;
                    remove(file.c_str());
                }
            }
        }
        closedir(d);
    }
    and it produces this Runtime error
    Code:
    This application has requested the Runtime to terminate in an unusual way.
    I cant seem to find what the error is in the code. I am very thankful for any assistance you provide.

    [edit]
    I figured out that the error is the
    Code:
    if ( file.substr(file.length()-4) == ".xtr" )
    , but I don't see what is wrong with the code.
    Last edited by Raigne; 09-14-2007 at 01:06 AM. Reason: Found the problem but I dont know how to fix it

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Raigne View Post
    I figured out that the error is the
    Code:
    if ( file.substr(file.length()-4) == ".xtr" )
    , but I don't see what is wrong with the code.
    What if teh length is shorter than 4 in the first place - that would cause a negative [1] substr - bad idea!

    [1] Or if the result is looked at as an unsigned number, it would be a REALLY HUGE number - not any better.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    thanks mat, I was thinking about it last night, and I figured out the scope is going negative for the ".", and ".." directory. wasnt thinking about it reading the directories to. Thank you for the assistance.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. accessing all files in a folder.
    By pastitprogram in forum C++ Programming
    Replies: 15
    Last Post: 04-30-2008, 10:56 AM
  2. Couple errors please help :-D
    By JJJIrish05 in forum C Programming
    Replies: 9
    Last Post: 03-06-2008, 02:54 AM
  3. Replies: 5
    Last Post: 12-03-2003, 05:47 PM
  4. Replies: 6
    Last Post: 07-30-2003, 03:08 AM
  5. Network problem
    By dirkduck in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 07-26-2002, 12:36 AM