1

我未能成功解析来自 libCurl FTP 列表的以下回复。

ftplister = fopen("ftp-full-list", "wb"); /* b is binary, needed on win32 */ 

  curl = curl_easy_init();
  if(curl) {
    /* Get a file listing from sunet */ 
    curl_easy_setopt(curl, CURLOPT_URL, furl.c_str() );
    curl_easy_setopt(curl, CURLOPT_USERPWD, usrpwd.c_str());

    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_flist);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, ftplister);

    res = curl_easy_perform(curl);
    /* Check for errors */ 
    if(res != CURLE_OK)
      fprintf(stderr, "curl_easy_perform() failed: %s\n",
              curl_easy_strerror(res));

    /* always cleanup */ 
    curl_easy_cleanup(curl);
  }

  fclose(ftplister); /* close the local file */   
}

write_flist 是

size_t  semperCloudBackup::write_flist(void *ptr, size_t size, size_t nmemb, void *stream)
{
   FILE *writehere = (FILE *)stream;
  return fwrite(ptr, size, nmemb, writehere);
}

给我一个如下所示的输出

03-26-07  05:23AM       <DIR>          html
03-26-07  04:27AM       <DIR>          laptop
03-26-07  03:16AM       <DIR>          images
03-27-07  11:00PM                 6397 index.html
03-26-07  03:45AM                10186 index1.html

我必须 1. 查找它是目录还是文件 2. 将文件与其大小一起存储(我猜是在一个数组中)以对大小进行一些计算。3.分别存放目录名

任何帮助将不胜感激,这里是新手。谢谢

经过大量搜索,我想我在 perl 中找到了一些东西,但我想在 C++ 中找到它

#!/usr/bin/perl
use warnings;
use strict;

while(<DATA>) {
    chomp;
    my @elements = split /\s+/, $_;
    if ( $elements[2] eq '<DIR>' ) {
        print "Directory: ",$elements[3], "\n";
    }
    else {
        print "The size of $elements[3] is $elements[2] bytes.\n";
    }
}

__DATA__
03-26-07  05:23AM       <DIR>          html
03-26-07  04:27AM       <DIR>          ibm-laptop
03-26-07  03:16AM       <DIR>          images
03-27-07  11:00PM                 6397 index.html
03-26-07  03:45AM                10186 index1.html

我还发现了这个ftpparse

要单独阅读每一行(尽管没有完全使用),我使用了以下内容:

#include <fstream>
#include <iostream>
#include <string>

using namespace std;

int main() {
      // a string to store line of text
      string textLine;
      // try to open a file
      ifstream ifs("sample_file.txt", ifstream::in);
      if (ifs.good())   { // if opening is successful
            // while file has lines
            while (!ifs.eof()) {
                  // read line of text
                  getline(ifs, textLine);
                  // print it to the console
                  cout << textLine << endl;
            }
            // close the file
            ifs.close();
      } else
            // otherwise print a message
            cout << "ERROR: can't open file." << endl;

      return 0;
}
4

0 回答 0