fstream: try catch and string input

Hello!

I have just converted from Java and am trying to start learning by translating some of my old code. I want to write a FileHandler class that can read/write strings and objects to files. I am starting out with writing and reading text to/from a file.

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
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

static string readFile() {
    ifstream myfile("example.txt");
    string returnString = "";
    if (myfile.is_open()) {
        string line;
        while (!myfile.eof()) {
            getline(myfile, line);
            returnString += line;
        }
        myfile.close();
        return returnString;

    } else {
        return NULL;
    }
}

int main(int argc, char** argv) {
    try {
        cout << readFile();
    } catch (exception e) {
        cout << "Failed reading file\n";
    }
    return (EXIT_SUCCESS);
}


Firstly, the try-catch:
In Java a function can be declared "throws exceptions", thus telling the compiler that when used, the function must be surrounded with try-catch. Is there anything similar in c++?
And is the use of catch(exception e) the same as catch(...)? (in Java catch(Exception e) catches every possible exception)

Secondly, input:
I want to open myFile using a string, but when doing this I can not compile my program!

what is the difference between:

 
ifstream myfile("example.txt");


and
1
2
string str = "example.txt";
ifstream myfile(str);

?

Thanks in advance!
Last edited on
In Java a function can be declared "throws exceptions", thus telling the compiler that when used, the function must be surrounded with try-catch. Is there anything similar in c++?
Yes but this feature is deprecated
And is the use of catch(exception e) the same as catch(...)? (in Java catch(Exception e) catches every possible exception)
In C++ you can throw any type, not only std::exception instances. catching a std::exception will catch only those, catching an ellipsis will catch everything
I want to open myFile using a string, but when doing this I can not compile my program!
on the current standard file streams constructors take C string arguments. To get a C string from a C++ string call str.c_str() ( this is going to be fixed with the next version of C++ )
this feature is deprecated


Why? and how else should one do this?

and how is this done?static string readFile(const string url) throws exception{} is not compiled

catching an ellipsis will catch everything

Ok, thanks!

on the current standard file streams constructors take C string arguments
Ok, thanks!
Why?
because you can't know everything that may be thrown by the function called within your function.
and how else should one do this?
static string readFile(const string url) throw ( exception )
On some compiler it will have no effect. If you omit that means that the function can throw anything
Topic archived. No new replies allowed.