无法读取 JPG C++的纯文本值
Can't Read Plain Text Value of JPG C++
我在读取jpg文件时遇到问题。我想通过套接字发送jpg图像的纯文本值,所以我以二进制模式打开了该文件,认为这可以工作,但事实并非如此。这是我的代码:
system("./imagesnap image.jpg");
ifstream image("image.jpg", ios::in | ios::binary);
char imageChar[1024];
string imageString;
while (getline(image, imageString))
{
for (int h; imageString[h] != ' '; h++) {
imageChar[h] = imageString[h];
}
send(sock, imageChar, strlen(imageChar), 0);
for (int k = 0; imageChar[k] != ' '; k++) {
imageChar[k] = ' ';
}
}
这是我的输出:
????
正如您所看到的,该文件不是以二进制模式打开的,或者它是但不工作。
有人能帮忙吗?
使用read()
而不是getline()
。请确保使用其返回值。
#include <sys/types.h>
#include <sys/socket.h>
#include <iostream>
#include <fstream>
void SendFile(int sock) {
std::ifstream image("image.jpg", std::ifstream::binary);
char buffer[1024];
int flags = 0;
while(!image.eof() ) {
image.read(buffer, sizeof(buffer));
if (send(sock, buffer, image.gcount(), flags)) {
; // handle error
}
}
}
相关文章:
- 文本文件中的单词链表
- 从命令行c++发送文本文件名
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 2D数组来自文本输入,中间有空格
- 如何将内容数组写入文本文件?
- 无法通过空白将文本文件行分隔为矢量
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- C++将文本文件中的数据读取到结构数组中
- 在指针的帮助下,文本文件中单词的频率
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 将值从二维数组输出到文本文件
- 如何在c++中从文本文件中逐行读取整数
- 如何创建一个QTableWidgetItem,用长文本右对齐,左边有省略号
- 从文本文件中读取时钟时间和事件时间并进行处理
- 如何从文本文件中读取值和数组
- 如何在C++中确定文本文件中的元素是字符还是数字
- 如何根据单词在文本中出现的概率输出单词
- 如何在C++中用std::cout正确显示带十六进制的字符串文本
- C++试图读取一个文件并输出到另一个文本文件
- 如何通过套接字将文本文件的内容从服务器发送到客户端