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:).