opening a file via function

If I am opening an existing txt file via a function, do I need to pass any info from the function to a variable? or is just opening the file enough?
I think you're going to need to be a little more specific in what exactly it is you're trying to do.
I have a text file and I need to open it via function and then read it via a different function. I can open it but I'm wondering how to read from the file once its opened. Can I just read from it normally?
Is this like what you are thinking of?
1
2
3
4
5
6
7
8
9
void foo(ifstream);
void funky(){
   std::ifstream data("myfile.txt");
   foo(data);
}
void foo(ifstream data){
   std::string info("");
   data >> info;
}
The stream needs to be passed by reference:
void readFile(std::ifstream & file);
For some reason, I am having more problems with functions than I thought I would. Why doesnt this work?


#include <iostream>
#include <string>

using namespace std;

void InputFileName();

int main()
{

InputFileName();

void InputFileName (string& a);
string InData;
{
//string InData;
cout << "Enter File Name to gather info from (including extension)::" << endl;
cin >> InData;

}


return 0;
}
There are a few things wrong with the above code, such as giving two contradictory declarations for the function:
1
2
void InputFileName();
void InputFileName (string& a);

... and trying to define one function inside another.

I recommend you go slowly and carefully through the tutorial pages on functions
http://www.cplusplus.com/doc/tutorial/functions/
http://www.cplusplus.com/doc/tutorial/functions2/
ok, im just not understanding why this isnt working?

#include <iostream>
#include <string>

using namespace std;
string InputFileName();

int main()
{
string InputFile;
InputFileName();
cout << InputFile;
return 0;
}
string InputFileName()
{

string InputFile;
cout << "Enter input file name : " << endl;
cin >> InputFile;

return InputFile;
}
If I try to use a void function and pass it by reference, it wont compile and I cant figure out where the error is. with this, I can at least get it to run but the cout statement wont work. im getting frustrated!!!!
Go to the links that chervil sent. First you must understand how functions work before we can help otherwise you will just copy/paste.. since you will not understand how it is working.

Do you understand why this works
1
2
3
4
5
6
7
8
9
10
void msg( ); //void msg( void );

int main()
{
    msg();
}
void msg()
{
    std::cout << "Hello World!" << std::endl;
}


and this does not work
1
2
3
4
5
6
7
int main()
{
    void msg()
    {
         std::cout << "Hello World!" << std::endl;
    }
}


Because you are trying to do the second method which looks completely pointless to me...

****
If you don't want to use a prototype also you can just declare before the main function.

1
2
3
4
5
6
7
8
9
void msg()
{
    std::cout << "..." << std::endl;
}

int main()
{
    msg();
}
Last edited on
i understand how functions are supposed to work and I have read the tutorial OVER and OVER but I am not seeing what I am doing wrong. I want a function that gets a file name from the user:

void GetFileName()
{
string DataFileName;
cout << "Enter the name of the file to gather info from (including extension): ";
cin >> DataFileName;
//return (DataFile);
}//end GetFileName

this works fine but I cant pass the DataFileName. I tried adding "string& name"
void GetFileName (string& name) but I get an error.

I would like to pass DataFileName to a function that opens DataFileName.

I'm using xcode if that matters.
Look at the very first example in the tutorial.
1
2
3
4
5
6
int addition (int a, int b)
{
    int r;
    r=a+b;
    return (r);
}

Notice the function is declared with a type of int, not void int addition
and then uses the return statement to tell the function which value it is to return.

From the same example, look at the code in main()
1
2
    int z;
    z = addition (5,3);

Note the variable z is assigned the result when the function is called.

You could use precisely the same mechanism to handle a std::string instead of an int.

The alternative, as you seem to have considered, is to pass a variable by reference, as shown in part 2 of the tutorial.
so with that logic?

int OpenFile (string a)
{
string name;
ifstream DataFileName;
name=DataFileName.open (a);
return DataFile;
}

string NameA;
NameA=OpenFile(a)
please give me a little bigger nudge, I've been working on this little portion ALL DAY!!!!!!!!
Not exactly, that's a mixture of different types: int, string and ifstream, almost none of which match the context in which they are used.
Consider this:
1
2
3
4
5
6
7
8
9
10
11
12
13
string getName()
{
    string name;
    cout << "enter name of file" << endl;
    cin >> name;
    return name;
}

int main()
{
    string NameA;
    NameA = getName();
}


Or if you wanted to pass the string by reference, it might look like this:
1
2
3
4
5
6
7
8
9
10
11
void getName(string & name)
{
    cout << "enter name of file" << endl;
    cin >> name;
}

int main()
{
    string NameA;
    getName(NameA);
}

I've not used the ifstream at all in these examples, because I wanted to keep things very simple. But as I mentioned in an earlier post, if you want to pass an ifstream parameter to or from a function it will have to be done using the second method (by reference).
Thank you, after thinking about things for a while, I started to think I had it backwards. Im an now moving forward!!!
Topic archived. No new replies allowed.