create file with fstream

For some unknown reason when i try the following code it doesn't create a file.
(the program will simply get two inputs from the user and then use http to obtain the html/php text and then output it to a file)

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <SFML/Network.hpp>
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

#include <fstream>
using std::ofstream;
using std::ifstream;

#include <string>
using std::string;

int main()
{
    sf::Http http;
    string page_name, host_name, file_name;

    cout << "What website would you like to read from? (without http://)" << endl;
    cin >> host_name;

    host_name = "http://" + host_name;

    http.setHost(host_name);

    cout << "What page would you like to read from? (with a .html or .php)" << endl;

    cin >> page_name;

    file_name = host_name + " - " + page_name + ".html";

    sf::Http::Request request(page_name);

    ofstream fout(file_name, std::ios::out);


    if (!fout.is_open())
    {
        cout << "Couldn't create file" << endl;
        return 0;
    }

    sf::Http::Response response = http.sendRequest(request);

    sf::Http::Response::Status status = response.getStatus();

    if (status == sf::Http::Response::Ok)
    {
        fout << response.getBody();
        fout.close();
    }

    else
    {
        cout << "Error " << status << endl;
    }
}
   if (!fout.is_open())
    {
        cout << "Couldn't create file" << endl;
        return 0;
    }

    sf::Http::Response response = http.sendRequest(request);

    sf::Http::Response::Status status = response.getStatus();

    if (status == sf::Http::Response::Ok)
    {
        fout << response.getBody();
        fout.close();
    }

    else
    {
        cout << "Error " << status << endl;
    }
}


it keeps failing at the is_open check.

please help
Last edited on
You file path starts with http://.
oh :(

i was literally checking if any characters weren't allowed in a file name but i forgot it started with http://
it worked by the way
Topic archived. No new replies allowed.