简体   繁体   English

Cout 有效但 ofstream 无效

[英]Cout works but ofstream doesnt

Okay so I am trying to return pairs of coordinates whos values are being read from an input file and displayed on an output file outFile.好的,所以我试图返回从输入文件读取值并显示在 output 文件 outFile 上的坐标对。 However, nothing is placed on to the outFile stream.但是,outFile stream 上没有任何内容。

ignore the area and distance values for now I'm not done fixing those现在忽略面积和距离值我还没有完成修复这些

The distance between  and  is 6.9
The distance between  and  is 3.0
The distance between  and  is 6.9
The area of this triangle is: 10.1

When using cout the values are displayed in console.使用 cout 时,值显示在控制台中。

void getPoint(ifstream &inFile, double x1, double y1){
    ofstream outFile;
    inFile >> x1 >> y1;
    cout << fixed << setprecision(1) << "(" << round_off(x1,1) << "," << round_off(y1,1) <<")" << endl;
}   
//Prints coordinates appropriately in console(1.0,1.2)
                                             (6.0,6.0)
                                             (6.0,6.0)
                                             (3.0,6.5)
                                             (1.0,1.2)
                                             (3.0,6.5)

When using outFile, the coordinates are not placed on to the output file and nothing returns.使用 outFile 时,坐标不会放在 output 文件中,并且没有任何返回。 (see in first pic) (见第一张图)

void getPoint(ifstream &inFile, double x1, double y1){
    ofstream outFile;
    inFile >> x1 >> y1;
    outFile << fixed << setprecision(1) << "(" << round_off(x1,1) << "," << round_off(y1,1) <<")" << endl;
} //No values displayed in outFile

Without using the output file as a reference parameter in getPoint (if that even solves it idk), how can I display/return a coordinate (x,y) from getPoint() onto the output file?如果不使用 output 文件作为 getPoint 中的参考参数(如果它甚至可以解决它,idk),我如何显示/返回坐标(x,y)从 getPoint() 到 output 文件? Note:笔记:

outFile << fixed << setprecision(1) << "(" << round_off(x1,1) << "," << round_off(y1,1) <<")" << endl;

works in function main but I want it to work in respect to getPoint's arguments.在 function main 中工作,但我希望它在 getPoint 的 arguments 中工作。

Full code for context https://ctxt.io/2/AAAgYbZ5Ew上下文的完整代码https://ctxt.io/2/AAAgYbZ5Ew

The declarations声明

    ofstream outFile;

in the functions main and getPoint are shadowing (hiding) the global在函数maingetPoint中隐藏(隐藏)全局

ofstream outFile;

and preventing it from printing things to output file.并防止它打印东西到 output 文件。 You should remove them to have the functions use the global outFile if you hate using reference parameter for some reason.如果您出于某种原因不喜欢使用引用参数,则应该删除它们以使函数使用全局outFile

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM