Sign in to follow this  
McGrane

Can you return an array?

Recommended Posts

I am just messing around trying to get numbers from a file. What i was doing was
char load( char* ptr )
{
    
    ifstream fin;
    
    char map[5];
    
    fin.open(ptr);
    
    fin>>map;
    
    fin.close();
    
    return map;
    
}

char test;
    
    test = load("Test.txt");
    
    cout<<test;

Any suggestions ?

Share this post


Link to post
Share on other sites
You are returning an object created within a certain scope. Returning the array will point to memory that you should no longer try to read / write. Create the array on the heap, but then this leads to who is responsible for cleaning up the memory.

Share this post


Link to post
Share on other sites
What you could do is have one of the parameters as a pointer to your string and do this:


char load( char* ptr, char **out )
{

ifstream fin;

char map[5];

fin.open(ptr);

fin>>map;

fin.close();
strcpy( *out, map );
}

char *ret = new char [length];
load( "Funkytown", &ret );
delete [] ret;

Share this post


Link to post
Share on other sites
No. You can't return arrays.

Quote:
void load( char* filename, char* out )
{
ifstream fin(filename)
fin >> out;
}


Quote:
string load( char* filename )
{
ifstream fin(filename)
string out;
fin >> out;
return out;
}


Quote:
void load( char* filename, string& out )
{
ifstream fin(filename)
fin >> out;
}


etc

Share this post


Link to post
Share on other sites
std::string is your friend:

#include <fstream>
#include <iostream>
#include <string>
using namespace std;

std::string load(std::string filename)
{
ifstream fin;
fin.open(filename.c_str());
string map;
fin >> map;
fin.close();
return map;
}

int main(){
string a;
a = load("Test.txt");
cout << a;
cin.get();
return 0;
}



...Fixed. Thanks for noticing.

[Edited by - Rob Loach on February 15, 2005 4:45:26 PM]

Share this post


Link to post
Share on other sites
Thanks for all the help so far! ;). I was trying that last submission and i get...
11 C:\Dev-Cpp\Cpp CodeTest\fileinput.cpp
conversion from `const char*' to non-scalar type `std::basic_ifstream<char, std::char_traits<char> >'
requested

Share this post


Link to post
Share on other sites
Quote:
Original post by mcgrane66
Thanks for all the help so far! ;). I was trying that last submission and i get...
11 C:\Dev-Cpp\Cpp CodeTest\fileinput.cpp
conversion from `const char*' to non-scalar type `std::basic_ifstream<char, std::char_traits<char> >'
requested


The constructor is explicit. You need to write ifstream fin(filename.c_str());

Share this post


Link to post
Share on other sites
Same principle as 1D - either pass the output variable as parameter or use some STL container:


bool load( const std::string& fileName, std::vector< std::string > & out ) {

std::ifstream fin;
bool result = false;

fin.open( fileName.c_str() );

if ( fin.is_open() ) {

char line[128];

while ( fin.good() ) {
fin.getline( line, sizeof ( line ) );
out.push_back ( line );
}

fin.close();

result = true;
}

return result;
}


Share this post


Link to post
Share on other sites
Quote:
Original post by mcgrane66
But can anyone now tell me, can i return a 2d array?


2D arrays are generally just manipulated as 1D array, using explicit addressing. i.e. you don't do foo[i][j] but foo[i*row_size+j]. The alternative is to use nested containers (in essence, creating an array of arrays), which is a very different beast from a "2D array", since 2D arrays are a single, contiguous block of memory. The 2D addressing syntax is just ... syntactic sugar.

If you really want multidimensional arrays you can pass around, look at boost::multi_array.

Share this post


Link to post
Share on other sites
I wouldn't recommend returning an STL string from a function, at least not if it's very large. Speed difference between passing in the string as a reference and returning one is quite signifficant...

...and if i may be picky, if you can find the total size for the file, you should .resize() your vector and use the ifstream::read() function to fill it.

I just wanted to point this out before you get into a habit. I did, and recently ran my head into a wall when my .obj loader tried to load a high-poly object, and the load time exceeded...well several minutes...on one object...[grin]

Share this post


Link to post
Share on other sites
Quote:
Original post by Android_s
I wouldn't recommend returning an STL string from a function, at least not if it's very large. Speed difference between passing in the string as a reference and returning one is quite signifficant...


RVO.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this