c++文件操作


#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;

void ex1_xtra2()
{
    //輸入一個文件名稱
    string file_name;
    cout << "Please enter a file to be opened: (try input.txt or text.txt) ";
    cin >> file_name;

    //判斷是否為空
    if ( ! cin || file_name.empty() )
    {
        cerr << "oops! unable to read file name\n"; return;
    }

    //建立一個讀取的ifile,由於讀取 input.txt中的內容
   ifstream ifile( file_name.c_str() ); if ( ! ifile ) { cerr << "oops! unable to open input file: " << file_name << endl; return; } else { cerr << "!! ok: opened " << file_name << " for input\n"; } //新建一個input.txt.sort文件,用於保存排好序的文字 //類型是輸出ofile file_name += ".sort"; ofstream ofile( file_name.c_str() ); if ( ! ofile ) { cerr << "oops! unable to open output file: " << file_name << endl; return; } else { cerr << "!! ok: opened " << file_name << " for output\n"; } string word; vector< string > text; //讀取內容到word至文件末尾 while ( ifile >> word ) { //插入到容器text中 text.push_back( word ); } if ( text.empty() ) { cerr << "bummer! input file is empty: bailing out\n"; return; } else { cerr << "!! ok: read " << text.size() << " strings from input\n"; } //排序操作 sort( text.begin(), text.end() ); int cnt = 0; for ( vector<string>::iterator iter = text.begin(); iter != text.end(); ++iter ) { //排列格式 cnt += iter->size() + 1; if ( cnt > 40 ) { ofile << '\n'; cnt = 0; } //輸出到ofile中,即input.txt.sort文件中 ofile << *iter << ' '; } cout << "ok: wrote sorted strings into " << file_name << endl; } int main(void) { ex1_xtra2(); return 0; }

  intput.txt

Alice Emma has long flowing red hair.  
Her Daddy says when the wind blows 
through her hair, it looks almost alive, 
like a fiery bird in flight.  A beautiful 
fiery bird, he tells her, magical but untamed.  
``Daddy, shush, there is no such thing,'' she 
tells him, at the same time wanting him to 
tell her more.  Shyly, she asks, ``I mean, 
Daddy, is there?''

  input.txt.sort

A Alice Daddy Daddy, Emma Her Shyly, 
``Daddy, ``I a alive, almost asks, at beautiful 
bird bird, blows but fiery fiery flight. 
flowing hair, hair. has he her her her, him 
him, in is is it like long looks magical 
mean, more. no red same says she she shush, 
such tell tells tells the the there there?'' 
thing,'' through time to untamed. wanting when 
wind 

  c_str()函數返回一個指向正規C字符串的指針常量, 內容與本string串相同. 

解決的方法有兩種:

1.用c_str()函數,下面詳細介紹。

2.包含頭文件"string"

 

下面我們進入正題,請出我們的今天的主角  c_str()    他是一個函數哦。。。不要忘記了括號。。

語法: 
const char *c_str();
c_str()函數返回一個指向正規C字符串的指針常量, 內容與本string串相同. 
這是為了與c語言兼容,在c語言中沒有string類型,故必須通過string類對象的成員函數c_str()把string 對象轉換成c中的字符串樣式。
注意:一定要使用strcpy()函數 等來操作方法c_str()返回的指針 
比如:最好不要這樣: 
char* c; 
string s="1234"; 
c = s.c_str(); //c最后指向的內容是垃圾,因為s對象被析構,其內容被處理,同時,編譯器也將報錯——將一個const char *賦與一個char *。

應該這樣用: 
char c[20]; 
string s="1234"; 
strcpy(c,s.c_str()); 
這樣才不會出錯,c_str()返回的是一個臨時指針,不能對其進行操作

再舉個例子
c_str() 以 char* 形式傳回 string 內含字符串
如果一個函數要求char*參數,可以使用c_str()方法: 
string s = "Hello World!";
printf("%s", s.c_str()); //輸出 "Hello World!"

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM