c++ - Printf 和 fstream 运算符 << 显示不同的值。为什么?
问题描述
我正在尝试获取两只眼睛的值x
和y
坐标。我使用 opencv XML 文件检测到它,并在控制台中x
出现 2 个不同的值,printf()
而我保存的文本文件operator<<
显示 1 个值。为什么会这样?
printf("X = %o,Y = %o\n", eyes[j].x, eyes[j].y);
ofstream coordinates;
coordinates.open("C:/Users/dougl/Desktop/Coordinates.txt");
coordinates << "X = " << eyes[j].x << "\n" << "Y = " << eyes[j].y;
解决方案
#include <iostream>
using std::cout;
using std::endl;
using std::oct;
using std::hex;
int main()
{
long int pos_value = 12345678;
cout << "The decimal value 12345678 is printed out as" << endl;
cout << "octal: " << oct << pos_value << endl;
cout << "hexadecimal: " << hex << pos_value << endl << endl;
return 0;
}
Printf 显示整数的无符号八进制数,使用:%o 格式。 https://www.geeksforgeeks.org/format-specifiers-in-c/amp/
流运算符<< 重载根据传递的值的数据类型工作。因此,要打印八进制值,您需要格式化 (std::oct) :
cout << "octal: " << oct << pos_value << endl;
参考:http ://faculty.cs.niu.edu/~mcmahon/CS241/c241man/node83.html
推荐阅读
- c++ - 如何添加最终覆盖
- google-chrome - XMLHttpRequest 打开导致 mac 上的 chrome 更改页面 url
- prolog - 在prolog中截断浮点数
- c# - C# regex selecting value from html
- c++ - Using Linked List as an object in a class in C++
- reactjs - 使用 Redux-form 验证字段
- php - 为什么 Laravel 5.6 / Eloquent 不同意它自己的数据库连接?
- tensorflow - 如何强制张量流使用所有可用的 GPU?
- postgresql - 环回 - 授权 GET 用户?
- css - Ionic 3 grind with item sliding