• Errors with C++...
    18 replies, posted
[code]#include <iostream> #include <string> #include <fstream> fstream myfile; void writetofile( string file ) { myfile.open( file ); myfile << "Writing to a .txt..."; myfile.close(); } int main() { writetofile( "C:\Users\snipped\Documents\example.txt" ); //no username for you :O } [/code] After I took a looonngggg break from C++, I've forgotten a lot of stuff. Why doesn't this work? My compiler gives me an error upon me trying to compile this(yes, the file exists).
Don't use a global variable for your file. You aren't using any classes from the std namespace. Use std::ofstream for writing. Use RAII for closing the file. Escape the backslashes in the path ("\" becomes "\\"). If you would use Visual Studio all of these errors would show up before you even compiled.
Oh, thanks, I'll try that
[QUOTE=MegaTronJohn;48265098]Oh, thanks, I'll try that[/QUOTE] Your main function is missing the return statement too.
[QUOTE=evil-tedoz;48265840]Your main function is missing the return statement too.[/QUOTE] I don't think it's required in C++?
[QUOTE=HumbleTH;48266305]I don't think it's required in C++?[/QUOTE] Yes it is. Any function or method with a return type other than void needs a return value. OP: Your main() has an invalid signature too. It should be "int main(int argc, char* argv[])"
[QUOTE=CRASHFORT;48265077][...] Escape the backslashes in the path ("\" becomes "\\"). [...][/QUOTE] You can also just use "/". Every major operating system including Windows understands that, even if the latter doesn't default to it.
[QUOTE=Rocket;48266850]Nope on both! From the [url=http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4296.pdf]C++14 Standard[/url]:[/QUOTE] I somehow doubt OP is using a C++14 compliant compiler.
[QUOTE=Rocket;48277797]I only cited the C++14 standard because it's pretty much the only one available. However, I can guarantee that it is not a recent feature. I know it works on VS2005, and I doubt OP is using anything older than that.[/QUOTE] I'm fairly certain at least the second part is a legacy feature from C.
OP, try Flush() before Close()?
[QUOTE=CRASHFORT;48265077]Don't use a global variable for your file. [B]You aren't using any classes from the std namespace.[/B] Use std::ofstream for writing. Use RAII for closing the file. Escape the backslashes in the path ("\" becomes "\\"). If you would use Visual Studio all of these errors would show up before you even compiled.[/QUOTE] Piggybacking off of this You can either use [B]using namespace std;[/B] or you can write [B]std::[/B] before anything in the standard namespace
[QUOTE=FlakTheMighty;48280511]Piggybacking off of this You can either use [B]using namespace std;[/B] or you can write [B]std::[/B] before anything in the standard namespace[/QUOTE] There is also a third option for those who don't like writing [B]std::[/B] before everything, but don't want to pollute the global namespace with the entire contents of the [B]std[/B] namespace. [code]using std::cout; using std::ofstream;[/code]
[QUOTE=BackwardSpy;48280549]There is also a third option for those who don't like writing [B]std::[/B] before everything, but don't want to pollute the global namespace with the entire contents of the [B]std[/B] namespace. [code]using std::cout; using std::ofstream;[/code][/QUOTE] Oh neat, I didn't know about that. That's good to know.
Always happy to teach someone something!
[QUOTE=BackwardSpy;48280616]Always happy to teach someone something![/QUOTE] This of course applies to all namespaces unless I'm mistaking.
[QUOTE=P1raten;48283047]This of course applies to all namespaces unless I'm mistaking.[/QUOTE] It most certainly does.
I can't believe nobody has said this, but ignore basically everything in this thread and get a good C++ book, learn to read compiler errors and use your debugger. Questions like this aren't practical, you can't just ask "why isn't this working" every time you encounter a problem. Programming is 99% problem solving.
Sorry, you need to Log In to post a reply to this thread.