Jump to content
  • Advertisement
Sign in to follow this  
McGrane

Can you return an array?

This topic is 4873 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Quote:
Original post by Noir
so you must fix that function to:

char *load( char *ptr)
{
...
}


Returning a pointer to a local variable ... recipe for disaster.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!