2017-02-20 74 views
1

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

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

+2

这通常是由打印未初始化的变量引起的。 – drescherjm

+0

你是对的。现在我必须弄清楚为什么程序不会从输入文件中提取新信息。谢谢您的帮助! – Morgan

回答

0

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

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