changing the order of the data inside of a file using seekg, tellg function












0














I'm new to c++ programming. I have a small excercise which enables the use of the following: using of windows socket programming, using of window api functions (CreateFile, ReadFile, and WriteFile), and using of seekg, seekp, tellg, and tellp functions.



Here's what's happening in my code,




  1. Open a .txt file that has URLs inside (reddit.com,stackoverflow.com,google.com) and then read the URLs and put inside of strVal.


  2. Then I split the value of the data inside of strVal using delimiter "," then the loop starts.


  3. Then the windows socket programming will start.


  4. Inside of the loop it will get the HTTP request of each URLs and will ask the request type(POST or GET).


  5. Once the answer from the request was received, the data will be inside of Buffer.


  6. Then I will open a .txt file to put the received request of the 3 domains.



Example here's the result of output.txt



The result is fine and the program is working on this part but for the last requirement of the program, they want me to change the order of the answered data inside of the .txt file using seekg, seekp, tellg, and tellp.



I tried to implement those functions and here's my code.



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()


I just opened .txt file where the data inserted then just read the data using seekg then put it inside of a buffer then output. There's no sense because what if there's another domain inserted inside of the .txt file.



Now, my concern is on this part. Based on my code how can I change the order of the data inside of the txt file using seekg, seekp, tellg, and tellp?



Here's my overall code,



    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.1rnHost: ";
string http2 = "rnConnection: closernrn";
//post
string http3 = "POST / HTTP/1.1rnHost: ";
string http4 = "rnConnection: closernrn";
//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, ",rn");

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 requestn";
cout << "[1] HTTP request using GETn";
cout << "[2] HTTP request using POSTn";
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, ",rn");

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









share|improve this question
























  • It is convention that the contents of the link need to be within the question/post. So, you may put the content from this link "example here's the result of output.txt" within the post; use edit.
    – prasad_
    Nov 21 at 6:23










  • IMHO, your question is too extensive. You should try to focus on what is really missing. Where is "2020" coming from as the size of ab and dc?
    – Sigismondo
    Nov 21 at 13:45
















0














I'm new to c++ programming. I have a small excercise which enables the use of the following: using of windows socket programming, using of window api functions (CreateFile, ReadFile, and WriteFile), and using of seekg, seekp, tellg, and tellp functions.



Here's what's happening in my code,




  1. Open a .txt file that has URLs inside (reddit.com,stackoverflow.com,google.com) and then read the URLs and put inside of strVal.


  2. Then I split the value of the data inside of strVal using delimiter "," then the loop starts.


  3. Then the windows socket programming will start.


  4. Inside of the loop it will get the HTTP request of each URLs and will ask the request type(POST or GET).


  5. Once the answer from the request was received, the data will be inside of Buffer.


  6. Then I will open a .txt file to put the received request of the 3 domains.



Example here's the result of output.txt



The result is fine and the program is working on this part but for the last requirement of the program, they want me to change the order of the answered data inside of the .txt file using seekg, seekp, tellg, and tellp.



I tried to implement those functions and here's my code.



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()


I just opened .txt file where the data inserted then just read the data using seekg then put it inside of a buffer then output. There's no sense because what if there's another domain inserted inside of the .txt file.



Now, my concern is on this part. Based on my code how can I change the order of the data inside of the txt file using seekg, seekp, tellg, and tellp?



Here's my overall code,



    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.1rnHost: ";
string http2 = "rnConnection: closernrn";
//post
string http3 = "POST / HTTP/1.1rnHost: ";
string http4 = "rnConnection: closernrn";
//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, ",rn");

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 requestn";
cout << "[1] HTTP request using GETn";
cout << "[2] HTTP request using POSTn";
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, ",rn");

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









share|improve this question
























  • It is convention that the contents of the link need to be within the question/post. So, you may put the content from this link "example here's the result of output.txt" within the post; use edit.
    – prasad_
    Nov 21 at 6:23










  • IMHO, your question is too extensive. You should try to focus on what is really missing. Where is "2020" coming from as the size of ab and dc?
    – Sigismondo
    Nov 21 at 13:45














0












0








0







I'm new to c++ programming. I have a small excercise which enables the use of the following: using of windows socket programming, using of window api functions (CreateFile, ReadFile, and WriteFile), and using of seekg, seekp, tellg, and tellp functions.



Here's what's happening in my code,




  1. Open a .txt file that has URLs inside (reddit.com,stackoverflow.com,google.com) and then read the URLs and put inside of strVal.


  2. Then I split the value of the data inside of strVal using delimiter "," then the loop starts.


  3. Then the windows socket programming will start.


  4. Inside of the loop it will get the HTTP request of each URLs and will ask the request type(POST or GET).


  5. Once the answer from the request was received, the data will be inside of Buffer.


  6. Then I will open a .txt file to put the received request of the 3 domains.



Example here's the result of output.txt



The result is fine and the program is working on this part but for the last requirement of the program, they want me to change the order of the answered data inside of the .txt file using seekg, seekp, tellg, and tellp.



I tried to implement those functions and here's my code.



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()


I just opened .txt file where the data inserted then just read the data using seekg then put it inside of a buffer then output. There's no sense because what if there's another domain inserted inside of the .txt file.



Now, my concern is on this part. Based on my code how can I change the order of the data inside of the txt file using seekg, seekp, tellg, and tellp?



Here's my overall code,



    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.1rnHost: ";
string http2 = "rnConnection: closernrn";
//post
string http3 = "POST / HTTP/1.1rnHost: ";
string http4 = "rnConnection: closernrn";
//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, ",rn");

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 requestn";
cout << "[1] HTTP request using GETn";
cout << "[2] HTTP request using POSTn";
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, ",rn");

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









share|improve this question















I'm new to c++ programming. I have a small excercise which enables the use of the following: using of windows socket programming, using of window api functions (CreateFile, ReadFile, and WriteFile), and using of seekg, seekp, tellg, and tellp functions.



Here's what's happening in my code,




  1. Open a .txt file that has URLs inside (reddit.com,stackoverflow.com,google.com) and then read the URLs and put inside of strVal.


  2. Then I split the value of the data inside of strVal using delimiter "," then the loop starts.


  3. Then the windows socket programming will start.


  4. Inside of the loop it will get the HTTP request of each URLs and will ask the request type(POST or GET).


  5. Once the answer from the request was received, the data will be inside of Buffer.


  6. Then I will open a .txt file to put the received request of the 3 domains.



Example here's the result of output.txt



The result is fine and the program is working on this part but for the last requirement of the program, they want me to change the order of the answered data inside of the .txt file using seekg, seekp, tellg, and tellp.



I tried to implement those functions and here's my code.



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()


I just opened .txt file where the data inserted then just read the data using seekg then put it inside of a buffer then output. There's no sense because what if there's another domain inserted inside of the .txt file.



Now, my concern is on this part. Based on my code how can I change the order of the data inside of the txt file using seekg, seekp, tellg, and tellp?



Here's my overall code,



    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.1rnHost: ";
string http2 = "rnConnection: closernrn";
//post
string http3 = "POST / HTTP/1.1rnHost: ";
string http4 = "rnConnection: closernrn";
//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, ",rn");

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 requestn";
cout << "[1] HTTP request using GETn";
cout << "[2] HTTP request using POSTn";
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, ",rn");

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






c++ winsock seekg






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 10:42









Krishi H

16415




16415










asked Nov 21 at 5:00









frrelmj

24




24












  • It is convention that the contents of the link need to be within the question/post. So, you may put the content from this link "example here's the result of output.txt" within the post; use edit.
    – prasad_
    Nov 21 at 6:23










  • IMHO, your question is too extensive. You should try to focus on what is really missing. Where is "2020" coming from as the size of ab and dc?
    – Sigismondo
    Nov 21 at 13:45


















  • It is convention that the contents of the link need to be within the question/post. So, you may put the content from this link "example here's the result of output.txt" within the post; use edit.
    – prasad_
    Nov 21 at 6:23










  • IMHO, your question is too extensive. You should try to focus on what is really missing. Where is "2020" coming from as the size of ab and dc?
    – Sigismondo
    Nov 21 at 13:45
















It is convention that the contents of the link need to be within the question/post. So, you may put the content from this link "example here's the result of output.txt" within the post; use edit.
– prasad_
Nov 21 at 6:23




It is convention that the contents of the link need to be within the question/post. So, you may put the content from this link "example here's the result of output.txt" within the post; use edit.
– prasad_
Nov 21 at 6:23












IMHO, your question is too extensive. You should try to focus on what is really missing. Where is "2020" coming from as the size of ab and dc?
– Sigismondo
Nov 21 at 13:45




IMHO, your question is too extensive. You should try to focus on what is really missing. Where is "2020" coming from as the size of ab and dc?
– Sigismondo
Nov 21 at 13:45

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405526%2fchanging-the-order-of-the-data-inside-of-a-file-using-seekg-tellg-function%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405526%2fchanging-the-order-of-the-data-inside-of-a-file-using-seekg-tellg-function%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Feedback on college project

Futebolista

Albești (Vaslui)