0

我是 C++ 编程的新手。我有一个小练习,可以使用以下内容:使用 windows 套接字编程,使用窗口 api 函数(CreateFile、ReadFile 和 WriteFile),以及使用 seekg、seekp、tellg 和 tellp 函数。

这是我的代码中发生的事情,

  1. 打开一个.txt包含 URL 的文件(reddit.com、stackoverflow.com、google.com),然后读取 URL 并将strVal.

  2. strVal然后我使用分隔符拆分数据的值,","然后循环开始。

  3. 然后开始windows socket编程。

  4. 在循环内部,它将获取每个 URL 的 HTTP 请求并询问请求类型(POSTGET)。

  5. 一旦收到请求的答复,数据将在 Buffer 中。

  6. 然后我将打开一个.txt文件来放置收到的 3 个域的请求。

这里的例子 是 output.txt 的结果

结果很好,程序正在处理这部分,但对于程序的最后一个要求,他们希望我.txt使用 seekg、seekp、tellg 和 tellp 更改文件内已回答数据的顺序。

我试图实现这些功能,这是我的代码。

fstream fs0 ("C:\\Documents and Settings\\Administrator\\My Documents\\output.txt", ios::in | ios::out | ios::app);
ofstream fs1 ("C:\\Documents and Settings\\Administrator\\My Documents\\output1.txt", ios::out | ios::app);

//1st
fs0.seekg(2020, ios::beg);

char ab[2020];
fs0.read(ab, sizeof(ab));

//2nd
fs0.seekg(0, ios::cur);

char dc[2020];
fs0.read(dc, sizeof(dc));

//3rd 
fs0.seekg(0, ios::beg);

char abc[2000];
fs0.read(abc, sizeof(abc));

fs1.write(ab, sizeof(ab));
fs1.write(dc, sizeof(dc));
fs1.write(abc, sizeof(abc));

fs0.close();
fs1.close()

我刚刚打开.txt插入数据的文件,然后使用 seekg 读取数据,然后将其放入缓冲区然后输出。没有意义,因为如果.txt文件中插入了另一个域怎么办。

现在,我关心的是这部分。根据我的代码,如何使用 seekg、seekp、tellg 和 tellp 更改 txt 文件中数据的顺序?

这是我的整体代码,

    int main()
    {
    //getting the data
    HANDLE openFile;
    //HANDLE openFile1;
    HANDLE putdataFile;
    BOOL rewriFile;
    //BOOL rewriFile1;
    char *strVal;
    //char *strVal1;

    char* point;
    //char* point1;
    DWORD dwNoBytetoRead = 0;
    //DWORD dwNoBytetoRead1 = 0;
    DWORD dwNoByteWritten = 0;
    char dataextracted[] = "C:\\Documents and Settings\\Administrator\\My Documents";
    //40 dash
    string dash1 = "\n--------------------";
    string dash2 = "--------------------\n";

    //connecting to the server
    SOCKET Socket;
    SOCKADDR_IN SockAddr;
    struct hostent *host;
    char buffer[10000];
    int dataLen;
    int i = 0;
    //get
    string http1 = "GET / HTTP/1.1\r\nHost: ";
    string http2 = "\r\nConnection: close\r\n\r\n";
    //post
    string http3 = "POST / HTTP/1.1\r\nHost: ";
    string http4 = "\r\nConnection: close\r\n\r\n";
    //winsock startup
    WSAData wsaData;

    //1 or 2
    string input;

    //start of program.................................

    //open file
    openFile = CreateFile(L"C:\\Documents and Settings\\Administrator\\My Documents\\url.txt", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    //allocating memory
    LARGE_INTEGER fs;
    GetFileSizeEx(openFile, &fs);
    unsigned long long fSize = fs.QuadPart;
    strVal = (char*) malloc (fSize + 1);
    memset(strVal, 0, fSize + 1);

    //reading the content
    rewriFile = ReadFile(openFile, strVal, fSize, &dwNoBytetoRead, NULL);
    cout << "The URL/s " << strVal << endl << endl;

    CloseHandle(openFile);

    //split string
    point = strtok(strVal, ",\r\n");

    while (point != NULL) {
    //get
    string httpRequestget = http1 + point + http2;
    //post
    string httpRequestpost = http3 + point + http4;
    //checking kung successful yung wsastartup
    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
        cout << "WSAStartup failed.\n";
        system("pause");
        //return 1;
    }

    //connects to domain
    Socket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
        host = gethostbyname(point);

    SockAddr.sin_port=htons(80);
    SockAddr.sin_family=AF_INET;
    SockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr);

    //just to check kung connected or may error pala
      if(connect(Socket,(SOCKADDR*)(&SockAddr),sizeof(SockAddr)) != 0){
    cout << "Could not connect";
    system("pause");
    //return 1;
      }else{
      cout << "You are now connected to " << point << endl;
      }

    //choice 1 or 2
    cout << "Options to request\n";
    cout << "[1] HTTP request using GET\n";
    cout << "[2] HTTP request using POST\n";
    cout << "Choose an option: ";
    cin >> input;
    cout << endl;

    if (input == "1"){
    // Sending a HTTP-GET-Request to the Web Server
    int sentBytes = send(Socket, httpRequestget.c_str(), strlen(httpRequestget.c_str()),0);
    } else if (input == "2") {
    // Sending a HTTP-POST-Request to the Web Server
    int sentBytes = send(Socket, httpRequestpost.c_str(), strlen(httpRequestpost.c_str()),0);
    }

    // Receiving and Displaying an answer from the Web Server
    ZeroMemory(buffer, sizeof(buffer));
    recv(Socket, buffer, sizeof(buffer), 0);

    //put data in a file
    putdataFile = CreateFile(L"C:\\Documents and Settings\\Administrator\\My Documents\\output.txt", FILE_APPEND_DATA, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    string dash3 = dash1 + point + dash2;
    //write dash
    rewriFile = WriteFile(putdataFile, dash3.c_str(), strlen(dash3.c_str()), &dwNoByteWritten, NULL);

    //write the content
    rewriFile = WriteFile(putdataFile, buffer, 2000, &dwNoByteWritten, NULL);

    CloseHandle(putdataFile);

    // Cleaning up Windows Socket Dependencies
     closesocket(Socket);
     WSACleanup();

        point = strtok(NULL, ",\r\n");

    }
    cout << endl << "Data is downloaded here: " << dataextracted << endl << endl;

    fstream fs0 ("C:\\Documents and Settings\\Administrator\\My Documents\\output.txt", ios::in | ios::out | ios::app);
    ofstream fs1 ("C:\\Documents and Settings\\Administrator\\My Documents\\output1.txt", ios::out | ios::app);

    //1st
    fs0.seekg(2020, ios::beg);

    char ab[2020];
    fs0.read(ab, sizeof(ab));

    //2nd
    fs0.seekg(0, ios::cur);

    char dc[2020];
    fs0.read(dc, sizeof(dc));

    //3rd 
    fs0.seekg(0, ios::beg);

    char abc[2000];
    fs0.read(abc, sizeof(abc));

    fs1.write(ab, sizeof(ab));
    fs1.write(dc, sizeof(dc));
    fs1.write(abc, sizeof(abc));

    fs0.close();
    fs1.close();
    system("pause");l

    return 0;
      }
4

0 回答 0