写入文本文件的数据部分损坏且无法恢复
data written to text file is partially corrupt and irretrievable
template <class T>
void savetext(T *a, char const *b) //writes to text file inside .sln however the text file is corrupt
{
ofstream fout(b, ios::out);
for (int i = 0; a[i] != '[=10=]'; i++)
fout << a[i] << endl;
cout << "Text file successfully written to." << endl;
}
template <class T>
void gettext(T *a, char const *b) //this is where the error occurs: inside the text file it puts the right values along with piles of junk. Why is this?
{
ifstream fin(b, ios::in);
if (fin.is_open())
{
cout << "File opened. Now displaying .txt data..." << endl;
for (int i = 0; a[i]!= '[=10=]'; i++)
{
fin >> a[i];
}
cout << "Data successfully read." << endl;
}
else
{
cout << "File could not be opened." << endl;
}
}
int main()
{
const int n1 = 5, n2 = 7, n3 = 6;
int a[n1], x, y, z;
float b[n2] = {};
char c[n3] = "";
//Begin writing files to text files which I name herein.
cout << "Writing data to text 3 text files." << endl;
savetext(a, "integer.txt");
savetext(b, "float.txt");
savetext(c, "string.txt");
cout << endl;
//Retrieve the text in the files and display them on console to prove that they work.
cout << "Now reading files, bitch!" << endl;
gettext(a, "integer.txt");
gettext(b, "float.txt");
gettext(c, "string.txt");
cout << endl;
return 0;
system("PAUSE");
}
大家好,晚上好。我有一个 C++ 程序,目前正在将数据(整数、浮点数和字符)写入 3 个单独的文本文件。但是,当它将数据写入文本文件时,会发生两件事:数据在文本文件中,但一堆难以理解的 wingdings 文本和大数字和大负数也是如此——我从未输入过的数据。
因此,我无法从文本文件中检索信息,也无法显示文本文件的信息。如何我能解决那个问题吗?谢谢你。
您的第一个问题是您正在将未初始化的数据写入文件。
template <class T>
void savetext(T *a, char const *b) //writes to text file inside .sln however the text file is corrupt
{
ofstream fout(b, ios::out);
for (int i = 0; a[i] != '[=10=]'; i++)
fout << a[i] << endl;
cout << "Text file successfully written to." << endl;
}
template <class T>
void gettext(T *a, char const *b) //this is where the error occurs: inside the text file it puts the right values along with piles of junk. Why is this?
{
ifstream fin(b, ios::in);
if (fin.is_open())
{
cout << "File opened. Now displaying .txt data..." << endl;
for (int i = 0; a[i]!= '[=10=]'; i++)
{
fin >> a[i];
}
cout << "Data successfully read." << endl;
}
else
{
cout << "File could not be opened." << endl;
}
}
int main()
{
const int n1 = 5, n2 = 7, n3 = 6;
int a[n1], x, y, z;
float b[n2] = {};
char c[n3] = "";
//Begin writing files to text files which I name herein.
cout << "Writing data to text 3 text files." << endl;
savetext(a, "integer.txt");
savetext(b, "float.txt");
savetext(c, "string.txt");
cout << endl;
//Retrieve the text in the files and display them on console to prove that they work.
cout << "Now reading files, bitch!" << endl;
gettext(a, "integer.txt");
gettext(b, "float.txt");
gettext(c, "string.txt");
cout << endl;
return 0;
system("PAUSE");
}
大家好,晚上好。我有一个 C++ 程序,目前正在将数据(整数、浮点数和字符)写入 3 个单独的文本文件。但是,当它将数据写入文本文件时,会发生两件事:数据在文本文件中,但一堆难以理解的 wingdings 文本和大数字和大负数也是如此——我从未输入过的数据。
因此,我无法从文本文件中检索信息,也无法显示文本文件的信息。如何我能解决那个问题吗?谢谢你。
您的第一个问题是您正在将未初始化的数据写入文件。