I am currently coding a chat application-like-thing, and it writes and reads from a .txt file on DropBox in my 'public' folder. I was wondering how exactly I could achieve this.
My code so far is:
#include <iostream>
#include <fstream>
#include <Windows.h>
#include <cstdlib>
using namespace std;
int main () {
string output;
string outputty;
string Chat;
string option;
string line;
ofstream players;
ifstream chat;
fstream chatw;
cout << "(1) - Enter\n";
cin >> option;
if (option == "1") {
players.open ("C:/Users/Pebsie/Dropbox/Public/data.txt");
string name;
cout << "\nWhat is your name?\n";
cin >> name;
players << name;
players.close();
cout << "Entering..";
开发者_开发知识库 chatw.open ("C:/Users/Pebsie/Dropbox/Public/chat.txt", fstream::out | fstream::app | fstream::ate);
chatw << "***" << name << " Entered the game*** " << endl;
chatw.close();
while (1) {
system("cls");
ifstream myfile ("C:/Users/Pebsie/Dropbox/Public/chat.txt");
if (myfile.is_open()) {
while ( myfile.good() )
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
}
cin >> option;
if (option == "/refresh") {
//DO NOTHING
}
else if (option == "/clearchat") {
chatw.open ("C:/Users/Pebsie/Dropbox/Public/chat.txt", fstream::out);
chatw << name << " cleared the chat." << endl;
chatw.close();
}
else {
chatw.open ("C:/Users/Pebsie/Dropbox/Public/chat.txt", fstream::out | fstream::app | fstream::ate);
chatw << name << ": " << option << endl;
chatw.close();
}
}
system("pause");
chatw.open ("C:/Users/Pebsie/Dropbox/Public/chat.txt", fstream::out | fstream::app | fstream::ate);
chatw << "***" << name << " Left the game..*** " << endl;
chatw.close();
return 0;
}
}
When I code it, I test it locally, but still write to it so that the users can see what I'm updating to it.
So, any help would be greatly appreciated, thanks!
EDIT- My question, is how I would actually be able to write to and read from the dropbox file. It's public URL is http://dl.dropbox.com/u/17570838/chat.txt / how I could load the file from the online URL.
P.S It's just going to be used as a chat app for a test, it obviously isn't very good as it doesn't auto update.
Check out the libcurl library. It supports many file transfer protocols, including HTTP. There is also curlpp which is a C++ wrapper around libcurl.
See this related question for an example.
Reading the file via HTTP should be fairly straightforward. Alas, I don't know what mechanism Dropbox uses to upload files (WebDAV?).
精彩评论