C++ 输出Cstring遇见的奇葩问题
先上代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 // webConteng.cpp : Defines the entry point for the console application. 2 // 3 4 #include "stdafx.h" 5 #include <stdlib.h> 6 #include <afxinet.h> 7 #include <iostream> 8 #include <fstream> 9 using namespace std; 10 11 int main(int argc, char* argv[]) 12 { 13 printf("Hello World!\n"); 14 ///////////////////////////////////////////// 15 CInternetSession session("HttpClient"); 16 char * url = "http://www.baidu.com"; 17 CHttpFile *pfile = (CHttpFile *)session.OpenURL(url); 18 19 DWORD dwStatusCode; 20 pfile->QueryInfoStatusCode(dwStatusCode); 21 if(dwStatusCode == HTTP_STATUS_OK) 22 { 23 CString content; 24 CString data; 25 ofstream o_file; 26 o_file.open("11.txt"); 27 while (pfile->ReadString(data)) 28 { 29 content += data + "\r\n"; 30 char* test=data.GetBuffer(data.GetLength()); 31 o_file << test <<endl; 32 } 33 o_file.close(); 34 content.TrimRight(); 35 printf(" %s\n ", content); 36 } 37 pfile->Close(); 38 delete pfile; 39 session.Close(); 40 //////////////////////////////////////////////////////// 41 system("pause"); 42 return 0; 43 }
如果不将data赋值给test,而是直接输出data就会出现很奇葩的问题,输出的全是八位的数字
只要将data转为 char*就OK了;
坑死我一个多小时的时间。。。