Passing String to a Function
I know the following will look weird but I am simply not able to understand how string passing works.
#include<iostream>
#include<ctype.h>
#include<fstream>
using namespace std;
void myfilemaker(char *fn);
int main(void){
char fn[20];
cin>>fn;
myfilemaker(&fn[0]);
system("pause");
}
void myfilemaker(char &fn[0]){
ofstream.myfile;
myfile.open(fn[]);
myfile<<"Something";
myfile.close;
return;
}
Please tell me how to pass the string. I have tried tweaking the fn variable a bit but nothing seems to work.
Re: Passing String to a Function
You don't need to treat fn as an array except at its declaration. Declare it as a char* in your function parameters, and just pass around fn, nothing more.
Re: Passing String to a Function
Hey, there. For starters, always make sure that your function prototype and the function definition have the same data type in their parameters (i.e. both are char*-types.) You had the right idea in the prototype, but in the function definition things got a little scrambled.
For starters, when you make the ofstream "myFile", the syntax should be:
ofstream myFile;
not
ofstream.myFile;
Also, remember that the myFile.open() function takes a pointer to the string as a parameter. It looks like you were trying to pass the function the whole array, but that's overkill and it's improper syntax. Since your c-string is named "fn", the pointer to the c-string is simply "fn", so your myfilemaker() function should read:
Code:
void myfilemaker(char* fn)
{
ofstream myfile(fn);
myfile<<"Something";
myfile.close();
}
Also, just to get in the habit of good programmin', try to make your main() function return a value of 0, that's what generally symbolizes a successful execution. If you want to save some system resources, as will, use cin.get() instead of pausing the system, it's substantially less memory-intensive and performs the same function.
Re: Passing String to a Function
These two are not the same. I hope you can spot the difference.
Code:
void myfilemaker(char *fn);
void myfilemaker(char &fn[0])
I don't want to reiterate what was already said. Keep in mind that C-like strings, i.e. char*, are only a sequence of characters that ends with a null-termination character, or 0 (or '\0'). Though they are actually arrays or characters, they are called null-terminated strings. To work with such strings, you pass the address of the first character in the array. A function that takes a null-terminated string as input, actually takes a pointer to char*, because that is the type for the address of the first character in the array.
On, the other hand, if you code in C++ you should use std::string class, and not care about the char*. http://www.codeguru.com/cpp/cpp/stri...cle.php/c13267
Re: Passing String to a Function
@CILU:
Thanks! I will remember this one.
Re: Passing String to a Function
Quote:
Originally Posted by
gameman144
your myfilemaker() function should read:
Code:
void myfilemaker(char* fn)
{
ofstream myfile(fn);
myfile<<"Something";
myfile.close();
}
Note that since myfile is about to go out of scope anyway, the .close() call is redundant and unnecessary. The file will be closed when the ofstream's destructor is called.
Re: Passing String to a Function
Yes, but it's neither a harm, nor a bad practice.