Understanding pointers with file i/o in c++ -


I'm trying to get a better understanding of the indicators. My class assignment prototype void OpenFile (const char * fileName, Ifstream and inFile had to create a function .

  Zero openfile (const char * filename, first and infix) {inFile.open (FILENAME, ios_base :: in); If (! InFile.is_open ()) {cerr & lt; & Lt; "Could not open file" & lt; & Lt; Filename & lt; & Lt; "\ N"; Exhaust (EXIT_FAILURE); } And {cout & lt; & Lt; "File Open Success"; }} // FILENAME is defined as: const char * const FILENAME = "file.txt"; // The function is said in the main: OpenFile (FILENAME, inFile);  

I think I do not understand const char * and & amp; For two arguments I am finding that the first argument is a const char * because the assignment defines FILENAME in this way but will I have to use an indicator in this case? I have just done

  const char FILENAME = "file.txt";  

And to put the first parameter in my openfile only moves to const char. And then, why do I need reference for the second parameter of my function? Thanks to the confusion when using pointers and when using references! First of all, let me know that your function looks a little strange if you pass in a parameter file. But then within FILENAME, and fileName is used only for error output. I think this is not exactly right.

Second, there are only four characters (Character) from the const char * issue and as soon as there is only one letter const char FILENAME = "file.txt" will not work as such. Instead you need the array of characters (const char FILENAME [] = ...), or in C, use an indicator in the memory space of the text, i.e.

As you use STL, it can be a good idea to convert just four ditch * and instead of std :: string. Third, pointer versus context is often an issue, at the end of the taste they both do the same and within the compiler they are actually the same one difference in the usage is that you can not give a blank (empty) reference, but You can give a null pointer. Therefore the recommendation is usually to use context if you actually have to pass an object, and for one thing which may be optional, a pointer (in that case you have to handle ptr == NULL). Apart from this, four strings of four * and manually assigned memory are usually sent through the pointer.


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -