使用seekg,tellg函数更改文件内部数据的顺序

时间:2018-11-21 05:00:28

标签: c++ winsock seekg

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

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

  1. 打开一个内部包含URL(reddit.com,stackoverflow.com,google.com)的.txt文件,然后读取URL并将其放入strVal内。

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

  3. 然后Windows socket编程将开始。

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

  5. 收到请求的答复后,数据将在Buffer内部。

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

示例 here's the result of output.txt

结果很好,程序正在这部分上工作,但是出于程序的最后要求,他们希望我使用seekg,seekp,tellg更改.txt文件中已回答数据的顺序。 ,然后说出话来。

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

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

0 个答案:

没有答案