c:打印char*

void printCharPoint(char *c){
    while (*c != '\0'){
        printf("%d", *(c++));
    printf("\n");

c:读取文本

#include <stdio.h>
#include <stdlib.h>
// c/c++中文会乱码
int main(void)
    FILE* fp = fopen("C:\\Users\\Administrator\\Documents\\GitHub\\smart\\c_hik_ren\\read_me.txt", "r");
    if(!fp) {
        perror("File opening failed");
        return EXIT_FAILURE;
    int c; // 注意:int,非char,要求处理EOF
    while ((c = fgetc(fp)) != EOF) { // 标准C I/O读取文件循环
        putchar(c);
    if (ferror(fp))
        puts("I/O error when reading");
    else if (feof(fp))
        puts("End of file reached successfully");
    fclose(fp);

C++:读取文本内容

#include <fstream>
#include <iostream>
std::string  getFileContent(const std::string& path)
    std::ifstream file(path);
    std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
    return content;
int main() {
    std::cout << getFileContent("/home/oceanstar/CLionProjects/test_cpp/main.cpp");
    return 0;

C++: boost写文件

#include <ostream>
#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/stream.hpp>
int main() {
    boost::iostreams::stream_buffer<boost::iostreams::file_sink> buf("log.txt");
    std::ostream   out(&buf);
    out << "xxxxxx" ;
    return 0;

发现生成了一个文件,并且将xxxx写入到了文件中

c:图片转为二进制流并写入txt

#include <stdio.h>
#include <stdlib.h>
int main(void)
    FILE *fp;
    // //以二进制方式打开图像
    fopen_s(&fp,"C:\\Users\\Administrator\\Pictures\\test.jpg", "rb");
    if(fp == NULL) {
        perror("File opening failed");
        return EXIT_FAILURE;
    fseek(fp, 0, SEEK_END);
    long int size = ftell(fp);
    rewind(fp);
    printf("get the size is: %ld\n", size);
    //根据图像数据长度分配内存buffer
    char* ImgBuffer=(char*)malloc( size* sizeof(char) );
    //将图像数据读入buffer
    fread(ImgBuffer, size, 1, fp);
    fclose(fp);
    //以二进制写入方式
    if ( (fp=fopen("C:\\Users\\Administrator\\Pictures\\a.txt", "wb"))==NULL)
        perror("txtxFile opening failed");
        exit(0);
    //从buffer中写数据到fp指向的文件中
    fwrite(ImgBuffer, size, 1, fp);
    printf("ok");
    fclose(fp);
    free(ImgBuffer);
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define IMAGE_PATH "../"
#define SAVE_IMG  ".jpeg"
#define SAVE_TXT  ".txt"
char* genRandomString(int length)
    int flag, i;
    char* string;
    srand((unsigned) time(NULL ));
    if ((string = (char*) malloc(length)) == NULL )
        printf("Malloc failed!flag:14\n");
        return NULL ;
    for (i = 0; i < length - 1; i++)
        flag = rand() % 3;
        switch (flag)
            case 0:
                string[i] = 'A' + rand() % 26;
                break;
            case 1:
                string[i] = 'a' + rand() % 26;
                break;
            case 2:
                string[i] = '0' + rand() % 10;
                break;
            default:
                string[i] = 'x';
                break;
    string[length - 1] = '\0';
    return string;
void saveImg(char * pre, char* ImgBuffer, int size, char *save_format){
    // 生成名字
    char buff[1024]=IMAGE_PATH; //第一个字符串
    char *s=genRandomString(10);  //第二个字符串
    strcat(buff, pre);
    strcat(buff,s);    //拼接专两个字符串,结果保存在第属一个字符串当中
    strcat(buff,save_format);
    //以二进制写入方式
    FILE *fp;
    if ( (fp=fopen(buff, "wb+"))==NULL)
        perror("image fopen fall");
        exit(0);
    //从buffer中写数据到fp指向的文件中
    fwrite(ImgBuffer, size, 1, fp);
    fclose(fp);
int main(void)
    FILE *fp;
    // //以二进制方式打开图像
    fopen_s(&fp,"C:\\Users\\Administrator\\Pictures\\qtblog1.png", "rb");
    if(fp == NULL) {
        perror("File opening failed");
        return EXIT_FAILURE;
    fseek(fp, 0, SEEK_END);
    long int size = ftell(fp);
    rewind(fp);
    printf("get the size is: %ld\n", size);
    //根据图像数据长度分配内存buffer
    char* ImgBuffer=(char*)malloc( size* sizeof(char) );
    //将图像数据读入buffer
    fread(ImgBuffer, size, 1, fp);
    fclose(fp);
    saveImg("ren", ImgBuffer, size, SAVE_IMG);
    free(ImgBuffer);
int main() {
    FILE *fp;
    // //以二进制方式打开图像
    fopen_s(&fp,"C:\\Users\\Administrator\\Pictures\\test.jpg", "rb");
    if(fp == NULL) {
        perror("File opening failed");
        return EXIT_FAILURE;
    char pBuffer[8];
    FILE * wp = fopen("C:\\Users\\Administrator\\Pictures\\2.jpg", "wb");
    while (!feof(fp)){
        fread(pBuffer, 1, 3, fp); //f2 = fopen("2.jpg", "wb");
        fwrite(pBuffer, 1, 3, wp); // 每次读n个字节属
    fclose(fp);
    fclose(wp);
    printf("Hello, World!\n");
    return 0;

c++:图片与char* / char[]的转换

/*******************************************************************************
  Description: 打开系统设备文件函数
        Input: IN char *pcPath                      打开本地文件路径
               INOUT unsigned int *pulBuflen        获取本地待写入文件大小
               INOUT char **ppcBuffer               读取本地文件buffer
       Output: 无
       Return: 获取成功返回TRUE,失败返回FALSE
      Caution:
--------------------------------------------------------------------------------
  Modification History
  DATE        NAME             DESCRIPTION
--------------------------------------------------------------------------------
  YYYY-MM-DD
*******************************************************************************/
bool CLI_GetPicInfo(IN char *pcPath, INOUT unsigned int *pulBuflen, INOUT char **ppcBuffer)
    struct stat statbuf;
    unsigned int ulBuflen = 0;
    char *pcBuffer = NULL;
    int lReadlen = 0;
    int fd = 0;
    if (stat(pcPath, &statbuf) < 0)
        return false;
    ulBuflen = statbuf.st_size;
    if (0 == ulBuflen)
        return false;
    pcBuffer = (char *)malloc(ulBuflen);
    if (NULL == pcBuffer)
        return false;
    memset(pcBuffer, 0, ulBuflen);
    /* 打开本地文件 */
    fd = open(pcPath, O_RDONLY);
    if (fd < 0)
        return false;
    lReadlen = read(fd, pcBuffer, ulBuflen);
    if ((lReadlen < 0) || ((unsigned int) lReadlen != ulBuflen))
        free(pcBuffer);
        pcBuffer = NULL;
        close(fd);
        return false;
    close(fd);
    *pulBuflen = ulBuflen;
    *ppcBuffer = pcBuffer;
    return true;
int main(){
	 unsigned int ulBuflen = 0;       /* 单张图片长度*/
    char *pcImgBuf = NULL;           /* 单张图片buffer */
	if (!CLI_GetPicInfo("/home/a.jpg", &ulBuflen, &pcImgBuf))
        printf("Failed to get pic info %s.\n", stRtDirInfo.szName);
        return -1;
	free(pcImgBuf);
    pcImgBuf = NULL;
#include <fstream> // ifstream, ifstream::in
#include <iostream>
#define MAX 1024*1024
using namespace std;
// 将内存中的二进制文件写成一张图片
bool write_image(const string& filename, char  *pBuffer, int length){
    ofstream fout(filename, ios::binary);
    if(!fout){
        printf("can't open  = %s\n", filename.c_str());
        return false;
    fout.write(pBuffer,length);
    fout.close();
    return true;
bool write_image2(const string& filename, char  *image_buf, int image_len){
    int fd = open(filename.c_str(), O_CREAT | O_WRONLY | O_TRUNC);
    if (fd > 0) {
        int n = 0, m = 0;
        do {
            m = write(fd, image_buf + n, image_len - n);
            n += m;
        } while (n < image_len);
        close(fd);
// 将磁盘中的二进制文件读到内存中
bool read_image(const string& filename, char * pBuffer, int *out_length){
    ifstream fin(filename, ifstream::in | ios::binary);
    if(!fin){
        printf("can't open  = %s\n", filename.c_str());
        return false;
    // 求图片长度
    fin.seekg(0, std::ifstream::end);  //将文件流指针定位到流的末尾
    int length = fin.tellg();
    fin.seekg(0, std::ifstream::beg);  //将文件流指针重新定位到流的开始
    if(length <= 0 || length > MAX){
        printf("length = %d\n", length);
        return false;
    // 读取
    *out_length = length;
    fin.read(pBuffer, length);
    fin.close();
    return true;
int main(){
    // 读图片
    char * pBuffer = new char[MAX];
    int length = 0;
    bool succ = read_image( R"(D:\workspace\binarytree\test.jpg)", pBuffer, &length);
    if(succ){
        write_image(R"(D:\workspace\binarytree\save.jpg)", pBuffer, length);
        write_image(R"(D:\workspace\binarytree\save.png)", pBuffer, length);
        write_image(R"(D:\workspace\binarytree\save.txt)", pBuffer, length);
        write_image(R"(D:\workspace\binarytree\save.bmp)", pBuffer, length);
        write_image(R"(D:\workspace\binarytree\save.raw)", pBuffer, length);
    delete [] pBuffer;
    return 0;
int main11(){
    // 读图片
    char pBuffer[MAX] = {0};
    int length = 0;
    bool succ = read_image( R"(D:\workspace\binarytree\test.jpg)", pBuffer, &length);
    if(succ){
        write_image(R"(D:\workspace\binarytree\save.jpg)", pBuffer, length);
        write_image(R"(D:\workspace\binarytree\save.png)", pBuffer, length);
        write_image(R"(D:\workspace\binarytree\save.txt)", pBuffer, length);
        write_image(R"(D:\workspace\binarytree\save.bmp)", pBuffer, length);
        write_image(R"(D:\workspace\binarytree\save.raw)", pBuffer, length);
    return 0;
 

在创建文件流时,可以显示指定它的打开方式为ios::binary,也就是以二进制方式打开。但是,无论是否指定二进制方式打开文件,读写的最小单位都是字节。那么,它到底起到什么作用呢?

首先,介绍一下二进制方式打开与普通打开方式的区别,两者大的区别在于对换行符的处理方式不同。由于历史原因,Windows操作系统是用两个字符(\r\n)来表示换行符的;而Unix操作系统却是用单个字符(\n)来表示换行符的。因此,在创建文件流时,如果指定了以ios::binary方式打开,那么换行符就是单字符的;否则,就采用Windows操作系统的双字符。

总结来说,以ios::binary方式打开,换行符被解释成\n;反之,换行符被解释成\r\n。

所以为了兼容性,通常在Windous系统中通常使用ios::binary方式打开图像文件;在Unix(或类Unix)操作系统中,指定和不指定ios::binary方式没有区别。

C++: 图片与智能指针

#include <fstream> // ifstream, ifstream::in #include <iostream> #include <fcntl.h> #include <zconf.h> #include <memory> #define MAX 1024*1024*3 using namespace std; // 将内存中的二进制文件写成一张图片 bool write_image(const string& filename, const std::unique_ptr<char[]> &pBuffer, int length){ ofstream fout(filename, ios::binary); if(!fout){ printf("can't open = %s\n", filename.c_str()); return false; fout.write(pBuffer.get(),length); fout.close(); return true; bool read_image(const string& filename, const std::unique_ptr<char[]> & pBuffer, int *out_length){ ifstream fin(filename, ifstream::in | ios::binary); if(!fin){ printf("can't open = %s\n", filename.c_str()); return false; // 求图片长度 fin.seekg(0, std::ifstream::end); //将文件流指针定位到流的末尾 int length = fin.tellg(); fin.seekg(0, std::ifstream::beg); //将文件流指针重新定位到流的开始 if(length <= 0 || length > MAX){ printf("length = %d\n", length); return false; // 读取 *out_length = length; fin.read(pBuffer.get(), length); fin.close(); return true; int main(){ // 读图片 std::unique_ptr<char[]> pBuffer(new char[MAX]); int length = 0; bool succ = read_image( R"(/home/oceanstar/图片/1.jpg)", pBuffer, &length); if(succ){ write_image(R"(/home/oceanstar/CLionProjects/myffmget/save.jpg)", pBuffer, length); write_image(R"(/home/oceanstar/CLionProjects/myffmget/save.png)", pBuffer, length); write_image(R"(/home/oceanstar/CLionProjects/myffmget/save.txt)", pBuffer, length); write_image(R"(/home/oceanstar/CLionProjects/myffmget/save.bmp)", pBuffer, length); write_image(R"(/home/oceanstar/CLionProjects/myffmget/save.raw)", pBuffer, length); return 0; 读取文本#include &lt;stdio.h&gt;c#include &lt;stdlib.h&gt;// c/c++中文会乱码int main(void){ FILE* fp = fopen("C:\\Users\\Administrator\\Documents\\GitHub\\smart\\c_hik_ren\\read_me.txt", "r"); if(!fp) { perror("File opening failed"); r Mat img_target = Mat::zeros(cv::Size(target_w, target_h), CV_8UC3); Mat img_resize; cv::resize(input_img_color, img_resize, Size(padded_img_w, padded_img_h)); img_resize.copyTo... 最近编程用这一问题,网上查了一下,感觉还是自己写吧。 16进制数据一般是:text=0x340xb5…,就是0x开头,之后是两个数字十六进制数。 如果直接使用sscanf(text,”0x”,&num),会把连续的数字读进去直到遇到’x’. 如使用sscanf读取text第一个读到的十六进制数是0x340,误判啦。最后,因为是4个一组,就先把4个存到数组,再读取吧。看后面的代码好了。 3.代码实例: #include #include < int hexNum; sscanf(hexStr.c_str(), "%x", &hexNum); // 将十六进制字符串转为整型数 bitset<8> binaryNum(hexNum); // 将整型数转为二进制字符串 cout << binaryNum << endl; // 输出二进制字符串 return 0; 输出结果为:`00011010` 其中,`bitset<8>` 表示创建一个长度为8的二进制数。如果需要转换更长的十六进制数,需要相应地调整 `bitset` 的长度。 98段通天边路: 发送的INVITE数据是这个,为什么回复401啊? INVITE sip:34020000001320000002@192.168.8.200:5060 SIP/2.0\r\nVia: SIP/2.0/UDP 192.168.8.104:5061;rport=5061;branch=z9hG4bK790862409\r\nFrom: <sip:34020000001320000001@192.168.8.104>;tag=226563056\r\nTo: <sip:34020000001320000002@192.168.8.200:5060>\r\nCall-ID: b2fdb00b8c8148c887c9e2eb07c82038@192.168.8.104\r\nCSeq: 3 INVITE\r\nMax-Forwards: 70\r\nUser-Agent: eXosip/5.3.0\r\nContent-Length: 164\r\nContent-Type: APPLICATION/SDP\r\nContact: <sip:34020000001320000001@192.168.8.104:5060>\r\nSubject: 34020000001320000002:0;34020000001320000001:0\r\nExpires: 600\r\n\r\nv=0\r\no=34020000001320000001 0 0 IN IP4 192.168.8.104\r\ns=Play\r\nc=IN IP4 192.168.8.104\r\nt=0 0\r\nm=video 8000 RTP/AVP 96\r\na=recvonly\r\na=rtpmap:96 PS/90000\r\ny=0100000001