2012-04-05 147 views

回答

5

首先,這不是「一個常數」。它是一個指向常量字符數據的指針,即指向只讀字符串的指針。您可以更改指針,但不能更改指向的數據。例如:

FILE *out; 
const char *http_range = "Accept: text/*;q=0.3, text/html;q=0.7"; 

if ((out = fopen("textfile.txt", "w")) != NULL) 
{ 
    fprintf(out, "the range is '%s'\n", http_range); 
    fclose(out); 
} 

注意的是,以上在C,你的問題是古怪的雙標記的,所以我選C.

+0

這是C寫的價值,非常感謝。 – 2012-04-05 08:02:35

+2

@the_naive如果你的答案對你有幫助,你可以通過接受他的答案來感謝他。 – 2012-04-05 08:04:35

+0

你的意思是指向「只讀」字符串的指針,對吧? – hochl 2012-04-05 08:10:06

4

在C++下面的代碼將在test.txt的

// basic file operations 
#include <iostream> 
#include <fstream> 
using namespace std; 

int main() { 
    const char *http_range = "TEST"; 
    ofstream myfile; 
    myfile.open ("test.txt"); 
    myfile << http_range; 
    myfile.close(); 
    return 0; 
} 
相關問題