用C ++从网站上阅读文本

时间:2011-04-26 20:57:06

标签: c++ dropbox

我正在编写类似聊天应用程序的东西,它在我的“公共”文件夹中的DropBox上的.txt文件中写入和读取。我想知道我究竟能做到这一点。

到目前为止我的代码是:

#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;
    } 
}

当我编写代码时,我在本地测试它,但仍然写入它,以便用户可以看到我正在更新它。

所以,非常感谢任何帮助,谢谢!

编辑 - 我的问题是,我实际上是如何写入和读取Dropbox文件的。它的公开网址是http://dl.dropbox.com/u/17570838/chat.txt /我如何从在线网址加载文件。

P.S它将被用作测试的聊天应用程序,它显然不是很好,因为它不会自动更新。

1 个答案:

答案 0 :(得分:1)

查看libcurl库。它支持许多文件传输协议,包括HTTP。还有curlpp这是libcurl的C ++包装器。

有关示例,请参阅此相关question

通过HTTP读取文件应该相当简单。唉,我不知道Dropbox使用什么机制上传文件(WebDAV?)。

相关问题