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();
}
Re: How to make my program to understand that it's incorrect?
Quote:
Originally Posted by
prako2
... 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!
Re: How to make my program to understand that it's incorrect?
Quote:
Originally Posted by
VictorN
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.
Re: How to make my program to understand that it's incorrect?
Quote:
Originally Posted by
prako2
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
Re: How to make my program to understand that it's incorrect?
Also have a look at this FAQ
Re: How to make my program to understand that it's incorrect?
Quote:
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
Re: How to make my program to understand that it's incorrect?
Quote:
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
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();
}
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
Re: How to make my program to understand that it's incorrect?
Quote:
Originally Posted by
prako2
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;
Re: How to make my program to understand that it's incorrect?
Thanks, unfortunately I get filesystem error: when using demo_exists(json.emit());
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::path