博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c++文件操作
阅读量:4587 次
发布时间:2019-06-09

本文共 3270 字,大约阅读时间需要 10 分钟。

#include 
#include
#include
#include
#include
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
::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!"

 

转载于:https://www.cnblogs.com/CodeWorkerLiMing/p/11220227.html

你可能感兴趣的文章
打印图片,自动调整宽高
查看>>
对类使用dir()
查看>>
【13】淘宝sdk——入门实战之header.php制作(一)
查看>>
安装SoapUI Pro
查看>>
杜教BM模板
查看>>
Makefile经典教程(掌握这些足够)
查看>>
自己成功的编写的将数据从excel导入到access中
查看>>
【Leetcode】【Easy】Compare Version Numbers
查看>>
014 链表中倒数第k个结点
查看>>
Python的pip安装Django
查看>>
第一冲刺阶段——站立会议第二天4月19日
查看>>
hdu-----(2807)The Shortest Path(矩阵+Floyd)
查看>>
简洁的MysqlHelper
查看>>
Android面试收集录2 Broadcast Receiver详解
查看>>
基于HTML5实现的中国象棋游戏
查看>>
Luogu P2024 [NOI2001]食物链 | 并查集
查看>>
openLayers3 中实现多个Overlay
查看>>
SQlServer2008 之 定时执行sql语句作业的制定
查看>>
函数式编程
查看>>
由浅入深之Jquery笔记(1)
查看>>