Jeg har et lille problem..
Altså jeg har en Server
Serveren spørger om du vil skrive til en fil.
Du skal svare ja / nej
når du har gjort det kan jeg ikke finde ud af hvordan man sletter det gamle indhold af stringen og vil have brugeren til at skrive nyt indhold ned i tekst dokumentet..
Server clienten:
#include "socket.h"
#include <process.h>
#include <string>
#include <iostream>
#include <sstream>
#include <windows.h>
#include <stdio.h>
using namespace std;
unsigned __stdcall Answer(void* a) {
Socket* s = (Socket*) a;
ofstream Writefile;
ifstream Readfile;
while (1) {
std::string r = s->ReceiveLine();
if (r.empty()) break;
cout << "Command Recieved: " << r << "Executing command..." << endl;
string::size_type n = r.find('*');
if(n != string::npos)
{
s->SendLine("Connection established");
cout << "User connected!" << endl;
}
else
{
s->SendLine("Ready to use");
const char *p;
p = r.c_str();
if(p == "1")
{
Writefile.open("lektier.txt", ios::app);
if(Writefile.is_open())
{
s->SendLine("Enter homeworks");
cin.get();
Writefile << p << endl;
s->SendLine("Has been written to file");
}
else
{
s->SendLine("Could not open Text file!");
exit(-1);
}
}
if(p == "read")
{
Readfile.open("lektier.txt");
string Input;
if(Readfile.is_open())
{
while(!Readfile.eof())
{
getline(Readfile, Input);
s->SendLine(Input);
cout << Input << endl;
}
}
else
{
s->SendLine("Error reading Text file");
cout << "Error.." << endl;
}
}
}
}
delete s;
return 0;
}
int main(int argc, char* argv[]) {
string PORT;
int portNumber;
bool success;
cout << "Enter port to listen on: ";
while(!success)
{
getline(cin, PORT);
stringstream SS(PORT);
success = SS >> portNumber;
if(!success)
{
cout << "Invalid port number" << endl;
}
}
system("cls");
cout << "Listening on port: " << portNumber << endl;
SocketServer in(portNumber, 5);
while (1) {
Socket* s=in.Accept();
unsigned ret;
_beginthreadex(0,0,Answer,(void*) s,0,&ret);
}
return 0;
}
User clienten:
#include "socket.h"
#include <process.h>
#include <string>
#include <iostream>
#include <sstream>
#include <windows.h>
#include <stdio.h>
using namespace std;
int main() {
SetConsoleTitle("Christian Command Client");
string IP, PORT;
const char *ipAddress;
int port;
bool success;
cout << "Enter IP address to connect to: ";
getline(cin, IP);
ipAddress = IP.c_str();
cout << endl << "Enter Port: ";
while(!success)
{
getline(cin, PORT);
stringstream SS(PORT);
success = SS >> port;
if(!success)
{
cout << "Could not connect to port.." << endl;
}
}
system("cls");
cout << "Ip: " << IP << endl;
cout << "Port: " << port << endl;
try {
SocketClient s(ipAddress, port);
s.SendLine("*");
while (1) {
string l = s.ReceiveLine();
if (l.empty()) break;
cout << l << endl << endl;
string answer;
string InputHomeworks;
cout << "Would you like to Write new homeworks? Yes / No" << endl;
getline(cin, answer);
if(answer == "Yes" || answer == "yes")
{
system("cls");
cout << "Enter new homeworks" << endl;
getline(cin, InputHomeworks);
s.SendLine(InputHomeworks);
}
cout << "Would you like to Read homeworks? Yes / No" << endl;
getline(cin, answer);
if(answer == "Yes" || answer == "yes")
{
}
}
catch (const char* s) {
cerr << s << endl;
}
catch (std::string s) {
cerr << s << endl;
}
catch (...) {
cerr << "unhandled exception\n";
}
return 0;
}
Koden er ikke skrevet færdigt endnu fordi jeg er gået død i hvordan man kan skrive mere til samme string senere..
Det er helveds svært at forklare, men håber i forstår det.