dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: How to make my program to understand that it's incorrect?

  1. #1
    Join Date
    Aug 2019
    Posts
    56

    How to make my program to understand that it's incorrect?

    I want to loop my program to create a folder in Dropbox. Once it successed, to get out of loop. For example, if "fld0", "fld1", "fld2" exist, make fld3 and get out of loop. But the problem is that my program thinks that it created a folder "fld0" successfully, even if "fld0" was created before. In my code I can actually change
    HTML Code:
    json.UpdateBool("autorename", false);
    to true. But I want to have a variable equal to "fld%d" of successfully created the new folder (if "fld0", "fld1", "fld2" exist, get equality of "fld3"). How it can be done?

    HTML Code:
    #include "pch.h"
    #include <iostream>
    #include <fstream>
    #include<istream>
    #include<string>
    #include <Windows.h>
    #include <tchar.h>
    #include <CkRest.h>
    #include <CkJsonObject.h>
    #include <CkStream.h>
    #include <CkDateTime.h>
    #include <CkDtObj.h>
    #include <CkGlobal.h>
    #include <CkRest.h>
    #include <CkJsonObject.h>
    #include <CkStringBuilder.h>
    #include <CkCrypt2.h>
    
    using namespace std;
    
    
    
    void ChilkatSample(void)
    {
        // The Chilkat API can be unlocked for a fully-functional 30-day trial by passing any
        // string to the UnlockBundle method.  A program can unlock once at the start. Once unlocked,
        // all subsequently instantiated objects are created in the unlocked state. 
        // 
        // After licensing Chilkat, replace the "Anything for 30-day trial" with the purchased unlock code.
        // To verify the purchased unlock code was recognized, examine the contents of the LastErrorText
        // property after unlocking.  For example:
        CkGlobal glob;
        bool success = glob.UnlockBundle("");
        if (success != true) {
            std::cout << glob.lastErrorText() << "\r\n";
            return;
        }
    
        int status = glob.get_UnlockStatus();
        if (status == 2) {
            std::cout << "Unlocked using purchased unlock code." << "\r\n";
        }
        else {
            std::cout << "Unlocked in trial mode." << "\r\n";
        }
    
        // The LastErrorText can be examined in the success case to see if it was unlocked in
        // trial more, or with a purchased unlock code.
        std::cout << glob.lastErrorText() << "\r\n";
    }
    
    void ChilkatSample0(void)
    {
    
        CkRest rest;
        bool success;
    
        //  URL: https://api.dropboxapi.com/2/files/create_folder_v2
        bool bTls = true;
        int port = 443;
        bool bAutoReconnect = true;
        success = rest.Connect("api.dropboxapi.com", port, bTls, bAutoReconnect);
        if (success != true) {
            std::cout << "ConnectFailReason: " << rest.get_ConnectFailReason() << "\r\n";
            std::cout << rest.lastErrorText() << "\r\n";
            //return;
        }
    
        //  See the Online Tool for Generating JSON Creation Code
        CkJsonObject json;
    
        int i = 0;
        char folder[100];
        for (;;)
        {
            sprintf(folder, "/fld%d", i++);
    
            json.UpdateString("path", folder);
            json.UpdateBool("autorename", false);
    
            rest.AddHeader("Authorization", "Bearer access_token");
            rest.AddHeader("Content-Type", "application/json");
    
            CkStringBuilder sbRequestBody;
            json.EmitSb(sbRequestBody);
            CkStringBuilder sbResponseBody;
            success = rest.FullRequestSb("POST", "/2/files/create_folder_v2", sbRequestBody, sbResponseBody);
    
            if (success == true) {
                std::cout << rest.lastErrorText() << "\r\n";
                return;
        }
        }
    
    
    int main()
    {
        ChilkatSample();
        ChilkatSample0();
    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,456

    Re: How to make my program to understand that it's incorrect?

    Quote Originally Posted by prako2 View Post
    ... But the problem is that my program thinks that it created a folder "fld0" successfully, even if "fld0" was created before. In my code I can actually change
    HTML Code:
    json.UpdateBool("autorename", false);
    to true. But I want to have a variable equal to "fld%d" of successfully created the new folder (if "fld0", "fld1", "fld2" exist, get equality of "fld3"). How it can be done?
    You could enumerate all the files/folders beginning with "fld" with following digits in the directory, then find the biggest number after "fld". After that - create a new folder!
    Victor Nijegorodov

  3. #3
    Join Date
    Aug 2019
    Posts
    56

    Re: How to make my program to understand that it's incorrect?

    Quote Originally Posted by VictorN View Post
    You could enumerate all the files/folders beginning with "fld" with following digits in the directory, then find the biggest number after "fld". After that - create a new folder!
    I'm not sure I understand you right but that's what I'm trying to do. The problem is that I don't know how to scan the existing folders.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,456

    Re: How to make my program to understand that it's incorrect?

    Quote Originally Posted by prako2 View Post
    I'm not sure I understand you right but that's what I'm trying to do. The problem is that I don't know how to scan the existing folders.
    Is it Windows? Then use Win32 API: FindFirstFile, FindNextFile, https://docs.microsoft.com/en-us/win...in-a-directory
    If you can use MFC - then CFileFind Class
    Victor Nijegorodov

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,456

    Re: How to make my program to understand that it's incorrect?

    Also have a look at this FAQ
    Victor Nijegorodov

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,814

    Re: How to make my program to understand that it's incorrect?

    The problem is that I don't know how to scan the existing folders.
    You do it here http://forums.codeguru.com/showthrea...entry-to-tchar using std::filesystem
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.2.5)

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,814

    Re: How to make my program to understand that it's incorrect?

    The problem is that I don't know how to scan the existing folders.
    You don't need to. You can use the std::filesystem exists() function to determine if a file exists or not. So you have a loop with initial suffix 0. If that file doesn't exist, OK. If it does exist, make the suffix 1 then 2 then 3 etc and continue with the loop until exists() returns false. Then you have the name for the new file. See https://en.cppreference.com/w/cpp/filesystem/exists
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.2.5)

  8. #8
    Join Date
    Aug 2019
    Posts
    56

    Re: How to make my program to understand that it's incorrect?

    2kaud, why do you think that filesystem will work in Dropbox. In both cases it says that folder does not exist. I set break point at line: if (success == true) { and here is my code:

    HTML Code:
    #include "pch.h"
    #include <iostream>
    #include <fstream>
    #include<istream>
    #include<string>
    #include <Windows.h>
    #include <tchar.h>
    #include <iostream>
    #include <fstream>
    #include <cstdint>
    #include <filesystem>
    #include <CkRest.h>
    #include <CkJsonObject.h>
    #include <CkStream.h>
    #include <CkDateTime.h>
    #include <CkDtObj.h>
    #include <CkGlobal.h>
    #include <CkRest.h>
    #include <CkJsonObject.h>
    #include <CkStringBuilder.h>
    #include <CkCrypt2.h>
    
    using namespace std;
    
    namespace fs = std::filesystem;
    
    CkJsonObject json;
    CkRest rest;
    bool success;
    
    //  URL: [url]https://api.dropboxapi.com/2/files/create_folder_v2[/url]
    bool bTls = true;
    int port = 443;
    bool bAutoReconnect = true;
    
    int i = 0;
    char folder[100];
    
    void ChilkatSample(void)
    {
    	// The Chilkat API can be unlocked for a fully-functional 30-day trial by passing any
    	// string to the UnlockBundle method.  A program can unlock once at the start. Once unlocked,
    	// all subsequently instantiated objects are created in the unlocked state. 
    	// 
    	// After licensing Chilkat, replace the "Anything for 30-day trial" with the purchased unlock code.
    	// To verify the purchased unlock code was recognized, examine the contents of the LastErrorText
    	// property after unlocking.  For example:
    	CkGlobal glob;
    	bool success = glob.UnlockBundle("");
    	if (success != true) {
    		std::cout << glob.lastErrorText() << "\r\n";
    		return;
    	}
    
    	int status = glob.get_UnlockStatus();
    	if (status == 2) {
    		std::cout << "Unlocked using purchased unlock code." << "\r\n";
    	}
    	else {
    		std::cout << "Unlocked in trial mode." << "\r\n";
    	}
    
    	// The LastErrorText can be examined in the success case to see if it was unlocked in
    	// trial more, or with a purchased unlock code.
    	std::cout << glob.lastErrorText() << "\r\n";
    }
    
    void demo_exists(const fs::path& p, fs::file_status s = fs::file_status{})
    {
    	std::cout << p;
    	if (fs::status_known(s) ? fs::exists(s) : fs::exists(p))
    	{
    		/*json.UpdateString("path", "/fld");
    		json.UpdateBool("autorename", false);
    
    		rest.AddHeader("Authorization", "Bearer ACCESS");
    		rest.AddHeader("Content-Type", "application/json");
    
    		CkStringBuilder sbRequestBody;
    		json.EmitSb(sbRequestBody);
    		CkStringBuilder sbResponseBody;
    		success = rest.FullRequestSb("POST", "/2/files/create_folder_v2", sbRequestBody, sbResponseBody);*/
    		std::cout << " exists\n";
    	}
    		
    	else
    		std::cout << " does not exist\n";
    }
    
    void ChilkatSample0(void)
    {
    
    	
    	success = rest.Connect("api.dropboxapi.com", port, bTls, bAutoReconnect);
    	if (success != true) {
    		std::cout << "ConnectFailReason: " << rest.get_ConnectFailReason() << "\r\n";
    		std::cout << rest.lastErrorText() << "\r\n";
    		//return;
    	}
    
    	//  See the Online Tool for Generating JSON Creation Code
    	
    
    	for (;;)
    	{
    		sprintf(folder, "/fld%d", i++);
    		demo_exists("/fld");
    		
    
    
    		
    
    		if (success == true) {
    			std::cout << rest.lastErrorText() << "\r\n";
    			return;
    		}
    	}
    }
    
    	int main()
    	{
    		ChilkatSample();
    		ChilkatSample0();
    	}
    Last edited by prako2; August 24th, 2019 at 05:18 PM.

  9. #9
    Join Date
    Aug 2019
    Posts
    56

    Re: How to make my program to understand that it's incorrect?

    HTML Code:
    json.UpdateString("path", "/fld");
    		cout << json.UpdateString.c_str();
    		json.UpdateBool("autorename", false);
    How to cout json.UpdateString. It says: error C2228: left of '.c_str' must have class/struct/union

  10. #10
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,814

    Re: How to make my program to understand that it's incorrect?

    Quote Originally Posted by prako2 View Post
    HTML Code:
    json.UpdateString("path", "/fld");
    		cout << json.UpdateString.c_str();
    		json.UpdateBool("autorename", false);
    How to cout json.UpdateString. It says: error C2228: left of '.c_str' must have class/struct/union
    Code:
    json.UpdateString("path", "/fld");
    cout << json.emit() << endl;
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.2.5)

  11. #11
    Join Date
    Aug 2019
    Posts
    56

    Re: How to make my program to understand that it's incorrect?

    Thanks, unfortunately I get filesystem error: when using demo_exists(json.emit());

  12. #12
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,814

    Re: How to make my program to understand that it's incorrect?

    Code:
    demo_exists(json.emit());
    You asked in post #9 re using cout. demo_exists() requires a first parameter of type path! What works in one situation doesn't necessarily work in another. It all depends upon the required type and the passed type. I'm no expert in json. I suggest you read the documentation for the json API's and that of std::filesystem/path and find a json function/method that will provide the required data in a type that can converted to type filesystem:ath
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.2.5)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)