gpt4 book ai didi

c++ - 程序将奇怪的符号打印到输出文件 C++

转载 作者:太空宇宙 更新时间:2023-11-04 13:06:02 25 4
gpt4 key购买 nike

好的,这是一个不完整程序的一小部分。该程序读取一个输入文件,进行一些计算并打印到一个输出文件,但输出文件中却出现了奇怪的符号。

void printRecord (char name[20], char Id[20], ostream& outfile)
{
outfile << name << Id << endl;
}

int main()
{
ofstream outfile;
ifstream infile;


char file_nameI[21], file_nameO[21], name[20], Id[8];

float hworkgrade, grade1;
int deductions;

cout << "Please enter name of input file: ";
cin >> file_nameI;
infile.open(file_nameI);
if ( !infile)
{
cout << "Could not open input file \n";
return 0;
}

cout << "Please enter name of output file: ";
cin >> file_nameO;
outfile.open(file_nameO);
if ( !outfile)
{
cout << "Could not open output file \n";
return 0;
}

do
{
infile >> name >> Id;
cout<< name << Id;
hworkgrade = CalHworkGrade(grade1, infile);
printRecord(name, Id, outfile);
}
while(!infile.eof());

return 0;
}

这是输出文件中的内容

Ф,ью\Xаѓ Ф,ью

最佳答案

程序没有从输入文件中提取信息。如果我将变量设置为 hello,它们不会被新信息覆盖,程序会打印 hello

char file_nameI[21], file_nameO[21], name[20] = hello , Id[8]  = hello;

关于c++ - 程序将奇怪的符号打印到输出文件 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42336431/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com