![]() |
|
[C] Single vs double code line to open files - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [C] Single vs double code line to open files (/showthread.php?tid=4971594) |
Single vs double code line to open files - J. McCarthy - Sep 1, 2015 So I'm wondering the difference and if either is better because I've seen both in tutorials Ifstream Example Example.open("file.txt") Ifstream Example("file.txt") Am I mistaken or do you no longer need to open and close a file with the second? If you do then do you just re type it or what? * - sqsgkrlhdwg - Sep 1, 2015 (Sep 1, 2015 05:26 AM)Formula1 Wrote: So I'm wondering the difference and if either is better because I've seen both in tutorials PHP Code The first solution (default constructor) is good when you use a class where you can't initialize directly the ifstream object. Otherwise if you open and close in the same scope, you better use the initialization constructor (You don't need to call open) * - Remix - Sep 1, 2015 The first way just calls the open method of the ifstream class and the second way calls the constructor method. No need to copy a long text. * - sqsgkrlhdwg - Sep 1, 2015 (Sep 1, 2015 05:49 AM)iRemix Wrote: The first way just calls the open method of the ifstream class and the second way calls the constructor method. You don't even answer to his question. * - Remix - Sep 1, 2015 (Sep 1, 2015 06:30 AM)Leƒƒe Wrote: You don't even answer to his question. (Sep 1, 2015 05:26 AM)Formula1 Wrote: So I'm wondering the difference Well one of his questions was about the difference. And you just explained what the constructor does. * - J. McCarthy - Sep 1, 2015 You guys both helped, thank you! maybe you can help me here too? So basically I have to make a program read a file in the sequence: 1) 1 2 3 4 5 2) 1 2 3 4 5 3) 1 2 3 4 5 I want to make one write it first and I need to make it read them (and input) through arrays so I made this Code #include <iostream>but how do I make the program stop allowing the user to input after it reaches 10 because right now you can keep going and it just crashes |