用c++从网站上读取文本

Reading text from a website in C++

本文关键字:读取 取文本 网站 c++      更新时间:2023-10-16

我目前正在编写一个聊天应用程序之类的东西,它从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) - Entern";
    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文件。它的公共URL是http://dl.dropbox.com/u/17570838/chat.txt/我如何从在线URL加载文件

p。S:这只是一个测试用的聊天应用,它显然不是很好,因为它不会自动更新。

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

请参阅相关问题的示例。

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