【问题标题】:Program prints weird symbols to output file C++程序打印奇怪的符号以输出文件 C++
【发布时间】:2017-02-20 04:21:01
【问题描述】:

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

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аѓФ,ью

【问题讨论】:

  • 这通常是由未初始化的打印变量引起的。
  • 你是对的。现在我必须弄清楚为什么程序没有从输入文件中提取新信息。感谢您的帮助!

标签: c++ file output symbols


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2017-07-18
    • 1970-01-01
    • 1970-01-01
    • 2021-07-13
    • 2011-04-07
    • 2020-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多