Pass content from .txt file to character array in c ++

2

I have to pass a text file to an array of characters. I have done the following function:

char* loadData(string fileName, int& longitud)
{
    char* arrayTemp = NULL;
    ifstream file;
    file.open(fileName);
    if(file.fail())
    {
        perror(fileName.c_str());
        system("pause");
        exit(1);
    }

    string line;
    string line2;
    while(getline(file, line))
    {       
        line2 += line;
    }
    longitud = line2.size() + 1;
    arrayTemp = new char[longitud];
    for(int i = 0; i <longitud; i++)
    {
        arrayTemp[i] = line2[i];
    }
    file.close();

    return arrayTemp;
}

But something goes wrong because when printing the resulting array of, for example, loading a file that contains the following:

1.- Alberto XXX 
2.- Ana     XXX
3.- Emilio  XXX
4.- Ignacio XXX

the result is

1.- Alberto XXX 2.- Ana     XXX3.- Emilio  XXX4.- Ignacio XXX

that is, there is no line break.

The Main function looks like this:

int main(){
    int longitud = 0;
    char* arrayTemp = loadData("Level1.txt", longitud);

    for(int i = 0; i < longitud; i++)
    {
        cout << arrayTemp[i];
        if(arrayTemp[i] == '
char* loadData(string fileName, int& longitud)
{
    char* arrayTemp = NULL;
    ifstream file;
    file.open(fileName);
    if(file.fail())
    {
        perror(fileName.c_str());
        system("pause");
        exit(1);
    }

    string line;
    string line2;
    while(getline(file, line))
    {       
        line2 += line;
    }
    longitud = line2.size() + 1;
    arrayTemp = new char[longitud];
    for(int i = 0; i <longitud; i++)
    {
        arrayTemp[i] = line2[i];
    }
    file.close();

    return arrayTemp;
}
') { cout << endl; } } delete[] arrayTemp; return 0; }

As always I give a thousand thanks in advance to whom I can take a look:).

    
asked by Battyhal 08.10.2017 в 21:30
source

2 answers

2

getline( ) does not place the '\n' in the string it extracts; This character is lost along the way.

The easiest thing is that you put it on your:

while(getline(file, line))
{
  line2 += '\n';
  line2 += line;
}

By doing that, you can simplify your main( ) :

int main( ) {
  int longitud = 0;
  char* arrayTemp = loadData( "Level1.txt", longitud );

  cout << arrayTemp << endl;

  delete[] arrayTemp;
  return 0;
}

Since we're here, you can use the strncpy( ) function to copy the string, saving yourself a loop:

#include <cstring>

char* loadData( string fileName, int &longitud ) {
  ...
  longitud = line2.size( ) + 1;
  arrayTemp = new char[longitud];

  strncpy( arrayTemp, line2.c_str( ), longitud );

  file.close( );

  return arrayTemp;
}
    
answered by 09.10.2017 / 05:35
source
0

In this part:

cout << arrayTemp[i];
    if(arrayTemp[i] == '
cout << arrayTemp[i];
    if(arrayTemp[i] == '%pre%') // es '\n'
    {
        cout << endl;
    }
') // es '\n' { cout << endl; }

The '\ 0' completely indicates the end of the chain, it is not the line break actually

    
answered by 09.10.2017 в 02:05