Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

Single vs double code line to open files

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?
(Sep 1, 2015 05:26 AM)J. McCarthy Wrote:  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?

PHP Code

<?php 
initialization constructor ifstream 
(const char* filename, ios_base::openmode mode = ios_base::in);
    
Constructs an ifstream object, initially associated with the file identified by its first argument (filename), open with the mode specified by mode.
    
Internally, its istream base constructor is passed a pointer to a newly constructed filebuf object (the internal file stream buffer). Then, filebuf::open is called with filename and mode as arguments.
    If 
the file cannot be opened, the stream's failbit flag is set. 

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)
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.
(Sep 1, 2015 05:49 AM)Remix Wrote:  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.

You don't even answer to his question.
(Sep 1, 2015 06:30 AM)sqsgkrlhdwg Wrote:  You don't even answer to his question.

(Sep 1, 2015 05:26 AM)J. McCarthy Wrote:  So I'm wondering the difference

Well one of his questions was about the difference.
And you just explained what the constructor does.
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>
#include <fstream>

using namespace std;

int main()
{
    int random[10];
    int i=0;
    ofstream myfirstFile("First.txt");
    while(cin >> random[i]){
    myfirstFile << random[i] << endl;
    i++; }

    return 0;
}

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


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by Formula 1Dec 6, 2019 01:11 AM 23 708 Dec 6, 2019 01:11 AM
Last Post: itcorp
[.NET] by Haters Ganna HateNov 7, 2017 03:10 PM 11 385 Nov 7, 2017 03:10 PM
Last Post: 0xDEAD10CC
[Java] by RaitzNov 4, 2017 02:04 PM 7 183 Nov 4, 2017 02:04 PM
Last Post: ZeratulTheWizard