Return char array from function

Hello everyone!

I have problem, i need to put file content(letters, numbers) in array and then return this array to main. As i understand there is no such a chance to just write return array; i tried to make pointer, but i have some problems with my code. Maybe someone can help me, here is code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
 
char* download_file_to_array (const char* file_name){
    int m = 1;
    char symbol;   
       
	int const file_length = 1000; //file have 1000 symbols
	
	char loaded_data_array_primary[1000] = {}; // save main array to compare with
	
    FILE* myfile;
    myfile = fopen(file_name, "r"); 

    if(myfile == NULL) //Always test the file open.
    {
        cout<<"Error opening output file"<<endl;
        system("pause");
        return 0;
    }
    else {
      while(!feof(myfile)) // download file to array
      {
        symbol = fgetc (myfile);                   
        if(m <= file_length)
        {
            loaded_data_array_primary[m] = symbol;
            m++;
        }
      }   
      fclose(myfile); 
      char *pointer = loaded_data_array_primary;
      return pointer;
    }
}
 
 
string get_similar_file (const char* file_name)
{      
  char *pointer = download_file_to_array(file_name);
  cout << pointer[4] << endl;
  cout << pointer[3] << endl;
  cout << pointer[2] << endl;
}
 
 
int main()
{
  get_similar_file ("1.txt");
  system("pause");
  return 0;
}
If you create the array within the function like that, as a local variable (i.e. created without using new) then when the function ends, that memory will be reused for something else and you will be left with a pointer to some memory that is no longer the array.

Create the array using new, or create the array outside the function and pass in a pointer to it, and have the function fill the array in.
Use a std::string to hold the sequence of bytes in the file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <fstream>
#include <string>
#include <iterator>

std::string download_file_to_array( const char* file_name )
{
    std::ifstream file( file_name ) ;
    std::istreambuf_iterator<char> bof(file), eof ;
    return std::string( bof, eof ) ;
}

int main()
{
    std::string bytes = download_file_to_array( "1.txt" ) ;
    if( bytes.size() > 4 ) std::cout << bytes[4] << bytes[3] << bytes[2] << '\n' ;
}
Big thanks to you friends, JLBorges i need an array :( Moschops, could you correct my code and make it with "new", i don't understand where should i put it, to correct work.
> JLBorges i need an array

When there is a std::string, why do you need an array of char?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <fstream>
#include <string>
#include <iterator>
#include <cstring>

char* download_file_to_array( const char* file_name )
{
    std::ifstream file( file_name ) ;
    std::istreambuf_iterator<char> bof(file), eof ;
    std::string temp( bof, eof ) ;
    if( temp.empty() ) return nullptr ;
    else
    {
        char* array = new char[temp.size()+1] ;
        std::strcpy( array, temp.c_str() ) ;
        return array ;
    }
}

int main()
{
    char* array = download_file_to_array( "1.txt" ) ;
    if( array && std::strlen(array) > 4 )
         std::cout << array[4] << array[3] << array[2] << '\n' ;
    // ...     
    delete[] array ;
    array = nullptr ;
    // ...
}


Thank you very much, this is what i was looking for. I just need to work with array in future.
Topic archived. No new replies allowed.