I have some code to read data from files, but when I run this code, it's returning more than I expected. Can someone help me to fix that?
Txt file:
test_line1
test_line2
test_line3
Console output:
test_line1
test_line2
test_line3rs\pc\█×u«║2
Code:
#include <iostream>
#include <fstream>
using namespace std;
char *getDataFromFile(char *file_patch)
{
fstream file;
file.open(file_patch, ios::in | ios::binary);
if (file.is_open())
{
file.seekg(0, file.end);
int length = file.tellg();
file.seekg(0, file.beg);
char *data = new char[length];
file.read(data, length);
file.close();
return data;
}
file.close();
}
int main(int argc, char **argv)
{
cout << getDataFromFile("test.txt") << endl;
system("PAUSE");
return 0;
}
I was trying to create a void
function with addres
in input, but still it is not working.
Also, I tried to create a char
table instead of char
array, it worked but I couldn't return it anymore.