C++ 输出Cstring遇见的奇葩问题

时间:2021-10-21 17:26:19

先上代码

 // webConteng.cpp : Defines the entry point for the console application.
// #include "stdafx.h"
#include <stdlib.h>
#include <afxinet.h>
#include <iostream>
#include <fstream>
using namespace std; int main(int argc, char* argv[])
{
printf("Hello World!\n");
/////////////////////////////////////////////
CInternetSession session("HttpClient");
char * url = "http://www.baidu.com";
CHttpFile *pfile = (CHttpFile *)session.OpenURL(url); DWORD dwStatusCode;
pfile->QueryInfoStatusCode(dwStatusCode);
if(dwStatusCode == HTTP_STATUS_OK)
{
CString content;
CString data;
ofstream o_file;
o_file.open("11.txt");
while (pfile->ReadString(data))
{
content += data + "\r\n";
char* test=data.GetBuffer(data.GetLength());
o_file << test <<endl;
}
o_file.close();
content.TrimRight();
printf(" %s\n ", content);
}
pfile->Close();
delete pfile;
session.Close();
////////////////////////////////////////////////////////
system("pause");
return ;
}

如果不将data赋值给test,而是直接输出data就会出现很奇葩的问题,输出的全是八位的数字

只要将data转为 char*就OK了;

坑死我一个多小时的时间。。。