C++博客
::
首頁
::
聯系
::
聚合
::
管理
117 Posts :: 2 Stories :: 61 Comments :: 0 Trackbacks
常用鏈接
我的隨筆
我的評論
我參與的隨筆
留言簿
(8)
給我留言
查看公開留言
查看私人留言
隨筆分類
《Visual C++代碼參考與技巧大全》學習筆記(60)
《Visual C++游戲編程基礎》學習筆記(22)
《編程之美》學習筆記
《編程珠璣》學習筆記
《算法導論》學習筆記
Android 開發筆記(1)
C++ primer plus學習筆記(22)
C++ Web開發(1)
Linux 學習筆記(3)
操作系統學習筆記(1)
高質量程序設計指南(林銳)學習筆記
計算機組成原理學習筆記
面試筆試題積累
其他(1)
數據結構系列學習筆記
數據庫學習筆記
數學系列學習筆記
英語學習積累(6)
隨筆檔案
2010年7月 (4)
2010年4月 (22)
2010年2月 (43)
2010年1月 (48)
搜索
最新評論
1.?re: 如何學習操作系統?——整理篇,非個人感悟
收藏,先把現代操作系統過一遍,有個基本概念后,再來好好學習一番
--星空不遠
2.?re: 畫筆與畫刷
我來瞅瞅
--張一一
3.?re: 關于Aptana studio工具
驗證碼不會變。需要刷新網頁。
--速度
4.?re: Ubuntu(Linux)使用Eclipse搭建C/C++編譯環境
是eclipse的版本和cdt不相符,如果用樓主的方法要改上面的網址,根據自己的版本改,一般都不是galileo
--Circle
5.?re: Ubuntu(Linux)使用Eclipse搭建C/C++編譯環境
@monkeyjun 你google下,我好久沒碰了
--煙皚
閱讀排行榜
1.?Ubuntu(Linux)使用Eclipse搭建C/C++編譯環境(59666)
2.?如何學習操作系統?——整理篇,非個人感悟(11519)
3.?GDI繪圖函數(8074)
4.?PreTranslateMessage(6508)
5.?引入lib庫到工程中(6049)
評論排行榜
1.?Ubuntu(Linux)使用Eclipse搭建C/C++編譯環境(17)
2.?這是你應該做的(14)
3.?《Visuanl C++游戲編程基礎》學習筆記——索引隨筆 (5)
4.?GDI繪圖函數(3)
5.?千萬別study English,應學會learn English——英語學習方法強烈推薦(3)
C++ primer plus 文件處理程序
第一個程序:將內容寫入txt文本
#include
"
stdafx.h
"
#include
<
iostream
>
#include
<
fstream
>
//
for file I/O
using
namespace
std;
int
main(
int
argc,
char
*
argv[])
{
char
automobile[
50
];
int
year;
double
a_price;
double
d_price;
ofstream outFile;
//
creat object for output
/**/
/*
associate with a file在這里,程序運行之前,文件carinfo.txt并不存在。
* 在這種情況下,方法open()將新建一個名為carinfo.txt的文件。運行該程序
* 之后,文件carinfo.txt將存在。默認情況下,open()將首先截短該文件,即
* 將其長度截短到零——丟棄原有內容,然后將新的輸出加入到該文件中。
*
* 打開已有的文件,以接受輸出時,默認將它其長度截短為零,因此原來的內容將丟失。
*/
outFile.open(
"
carinfo.txt
"
);
cout
<<
"
Enter the make and model of auto mobile:
"
;
cin.getline(automobile,
50
);
cout
<<
"
Enter the model year:
"
;
cin
>>
year;
cout
<<
"
Enter the original asking price:
"
;
cin
>>
a_price;
d_price
=
0.913
*
a_price;
//
display information on screen with cout
cout
<<
fixed
;
cout.precision(
2
);
cout.setf(ios_base::showpoint);
cout
<<
"
Make and model:
"
<<
automobile
<<
endl;
cout
<<
"
Year:
"
<<
year
<<
endl;
cout
<<
"
Was asking $
"
<<
a_price
<<
endl;
cout
<<
"
Now asking $
"
<<
d_price
<<
endl;
//
now do exact same things using outFile instead of cout
outFile
<<
fixed
;
cout.precision(
2
);
cout.setf(ios_base::showpoint);
outFile
<<
"
Make and mode:
"
<<
automobile
<<
endl;
outFile
<<
"
Year:
"
<<
year
<<
endl;
outFile
<<
"
Was asking $
"
<<
a_price
<<
endl;
outFile
<<
"
Now asing $
"
<<
d_price
<<
endl;
outFile.close();
//
done with file
return
0
;
}
第二個程序:讀取txt文本的內容
下面程序打開用戶指定的文件,讀取其中的數字,然后指出文件中包含多少個值以及它們的和與平均值。正確地設計輸入循環至關重要。
這個程序我運行的時候出了一個問題,就是未能找到最后結果計算,如下圖所示,
正確的運行結果應該是:
很有意思,可以研究哈,我在csdn發帖詢問了,可參考其帖子:
txt文件讀取問題
程序代碼如下:
#include
"
stdafx.h
"
#include
<
iostream
>
#include
<
fstream
>
//
file I/O support
#include
<
cstdlib
>
//
support for exit()
using
namespace
std;
const
int
SIZE
=
60
;
int
main(
int
argc,
char
*
argv[])
{
char
filename[SIZE];
ifstream inFile;
//
object for handling file input
cout
<<
"
Enter name of data file:
"
;
cin.getline(filename, SIZE);
inFile.open(filename);
//
associate inFile with a file
if
(
!
inFile.is_open())
//
failed to open file
{
cout
<<
"
Could not open the file
"
<<
filename
<<
endl;
cout
<<
"
Program terminating.\n
"
;
exit(EXIT_FAILURE);
}
double
value;
double
sum
=
0.0
;
int
count
=
0
;
//
number of items read
inFile
>>
value;
//
get first value
while
(inFile.good())
//
while input good and not at EOF
{
++
count;
//
one more item read
sum
+=
value;
//
calculate runing total
//
cout<<"第"<<count<<"個數="<<value<<endl;
inFile
>>
value;
//
get next value
}
//
cout<<value<<endl;
if
(inFile.eof())
cout
<<
"
End of file reached.\n
"
;
else
if
(inFile.fail())
cout
<<
"
Input terminated by data mismatch.\n
"
;
else
cout
<<
"
Input terminated for unknown reason.\n
"
;
if
(count
==
0
)
cout
<<
"
No data processed.\n
"
;
else
{
cout
<<
"
Items read:
"
<<
count
<<
endl;
cout
<<
"
Sum:
"
<<
sum
<<
endl;
cout
<<
"
Average:
"
<<
sum
/
count
<<
endl;
}
inFile.close();
//
finished with the file
return
0
;
}
posted on 2010-02-11 17:10
煙皚
閱讀(410)
評論(0)
編輯
收藏
引用
所屬分類:
C++ primer plus學習筆記
只有注冊用戶
登錄
后才能發表評論。
【推薦】100%開源!大型工業跨平臺軟件C++源碼提供,建模,組態!
相關文章:
數組和指針的區別與處理技巧
這是你應該做的
VC6.0中友元函數無法訪問類私有成員的解決辦法-------VC6.0的bug
C++ primer plus第十一章 使用類的程序
C++ primer plus 關于引用的一些程序
函數指針
函數和string對象
函數和結構
函數和C-style string
函數處理數組問題
網站導航:
博客園
IT新聞
BlogJava
博問
Chat2DB
管理
Copyright @ 煙皚
Powered by:
.Text
and
ASP.NET
Theme by:
.NET Monster
国产精品免费福利久久
|
一本色道久久综合狠狠躁
|
久久精品这里热有精品
|
88久久精品无码一区二区毛片
|
精品国产91久久久久久久a
|
精品久久人人爽天天玩人人妻
|
伊人久久国产免费观看视频
|
天天躁日日躁狠狠久久
|
激情五月综合综合久久69
|
伊人久久大香线蕉av不卡
|
久久97久久97精品免视看
|
久久久亚洲欧洲日产国码是AV
|
97久久精品无码一区二区天美
|
久久久久久毛片免费看
|
精品久久久噜噜噜久久久
|
久久精品无码专区免费
|
久久99精品国产99久久6男男
|
久久精品视频一
|
开心久久婷婷综合中文字幕
|
亚洲狠狠婷婷综合久久久久
|
人妻丰满?V无码久久不卡
|
国产精品青草久久久久福利99
|
久久夜色精品国产噜噜亚洲AV
|
久久久久国产精品三级网
|
欧美久久综合性欧美
|
国产亚洲欧美成人久久片
|
四虎国产精品成人免费久久
|
久久久精品日本一区二区三区
|
久久久青草久久久青草
|
2021久久国自产拍精品
|
久久精品夜夜夜夜夜久久
|
久久久久久久亚洲Av无码
|
97精品依人久久久大香线蕉97
|
久久亚洲精品成人无码网站
|
久久人人爽人人爽AV片
|
欧美精品丝袜久久久中文字幕
|
国产精品青草久久久久婷婷
|
久久99精品久久久大学生
|
国产精品99久久久久久宅男小说
|
久久精品无码专区免费
|
久久婷婷五月综合色99啪ak
|