C#读写文件

C#读写文件的方法:

C#中,在含有转义字符的 字符串前加@可以避免使用转义字符

使用前要using System.IO;

StreamReader reader = new StreamReader(@"E:/main.c", Encoding.Default);            
string line;
while ((line = reader.ReadLine()) != null)
{
    Console.WriteLine(line);
}
reader.Close();
// 参数改为false可以直接重写文件
StreamWriter writer = new StreamWriter(@"E:/main.c", true, Encoding.Default);
writer.WriteLine("ok..");
writer.Close();

C语言拆分字符串strtok

C语言拆分字符串strtok函数

Example
/* STRTOK.C: In this program, a loop uses strtok
 * to print all the tokens (separated by commas
 * or blanks) in the string named "string".
 */
#include <string.h>
#include <stdio.h>
char string[] = "A string/tof ,,tokens/nand some  more tokens";
char seps[]   = " ,/t/n";
char *token;
void main( void )
{
   printf( "%s/n/nTokens:/n", string );
   /* Establish string and get the first token: */
   token = strtok( string, seps );
   while( token != NULL )
   {
      /* While there are tokens in "string" */
      printf( " %s/n", token );
      /* Get next token: */
      token = strtok( NULL, seps );
   }
}

用CHtmlView接口获取HTML源文件

用CHtmlView接口获取HTML源文件:

支持在UNICODE工程下获取HTML代码,无乱码。。。。发了两年多了才知道,害得我找了一晚上,没想到自己的博客里就有。。。

BOOL CWeb::GetSource(CString& strString) 
{ 
    IHTMLDocument2* pHtmlDoc2 = (IHTMLDocument2*)GetHtmlDocument(); 
    // check if HtmlDocument initialized 
    if( pHtmlDoc2 != NULL) 
    { 
        IHTMLDocument3* pHTMLDoc3 = NULL; 
        HRESULT hr = pHtmlDoc2->QueryInterface(IID_IHTMLDocument3, (LPVOID*)&pHTMLDoc3); 
        ASSERT(SUCCEEDED(hr)); 
        if(pHTMLDoc3) 
        { 
            IHTMLElement* pDocElem=NULL; 
            hr = pHTMLDoc3->get_documentElement(&pDocElem); 
            ASSERT(SUCCEEDED(hr)); 
            if(pDocElem) 
            { 
                BSTR bstrHTML; 
                pDocElem->get_outerHTML(&bstrHTML); 
                pDocElem->Release(); 
                USES_CONVERSION; 
                //MessageBox(OLE2T(bstrHTML), _T("源文件")); 
                strString = OLE2T(bstrHTML); 
                SysFreeString(bstrHTML); 
            } 
            pHTMLDoc3->Release(); 
        } 
        pHtmlDoc2->Release(); 
        pHtmlDoc2=NULL; 
    }
    return TRUE; 
}

关于gethostname函数失败的问题

调用gethostname函数失败。

解决办法:调用gethostname之前, 要先调用WSAStartup才可以, 否则gethostname会失败!

下面是正确的代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Winsock2.h>
#include <windows.h>
#pragma comment(lib, "Ws2_32")
int main()
{
	WSADATA wsData;
	::WSAStartup(MAKEWORD(2,2), &wsData);
	char szIP[32] = {0};
	char szHostName[32] = {0};
	int iResult = ::gethostname(szHostName, sizeof(szHostName));
	if (iResult != 0)
	{
		printf("error/n");
		return -1;
	}
	printf("%s/n", szHostName);
	hostent *pHost = ::gethostbyname(szHostName);
	::WSACleanup();
	return 0;
} 

C语言十进制转二进制(移位法)

C语言十进制转二进制(移位法)

#include <stdio.h>
int main()
{
	char strBinary[sizeof(int)*8+1];
	int a = 100;
	// 让p指向strBinary的最后一位
	char *p = strBinary + sizeof(int)*8;
	*p = '/0';
	while (a)
	{
		--p;
		*p = (a & 1) + 0x30;	// 加0x30转变成字符
		a >>= 1;	// 右移一位
	}
	printf("%s/n", p);
	return 0;
}

sqlplus启动问题