2011-04-26 91 views
0

我目前正在编写一个聊天类应用程序,它会在我的'公共'文件夹中的DropBox上的.txt文件中写入和读取。我想知道我究竟能做到这一点。从C++的网站阅读文本

到目前为止我的代码是:

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

当我的代码,我在本地测试了一下,但还是写它,使用户可以看到我更新它。

所以,任何帮助将不胜感激,谢谢!

编辑 - 我的问题是,我将如何实际能够写入和从保存箱文件读取。这是公开的URL是http://dl.dropbox.com/u/17570838/chat.txt /我怎么能从在线URL加载文件。

P.S它只是作为一个聊天应用程序进行测试,它显然不是很好,因为它不会自动更新。

+0

因此,您正在构建它以让Dropbox执行文件的所有同步并让Dropbox的基础架构执行网络遍历?这是一个有趣的方法。我可以向你保证,作为一个聊天机制失败几乎可以保证,但这是一个有趣的方法。考虑也要求程序员的设计帮助。 – jcolebrand 2011-04-26 21:00:52

+1

问题是什么?很难说出这里要问什么。你需要编写程序的帮助,还是需要修复一个缺陷?请澄清。 – 2011-04-26 21:01:07

+2

什么是具体问题?什么不工作? – 2011-04-26 21:01:47

回答

1

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

查看此例相关的question

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