C++博客
::
首頁(yè)
::
聯(lián)系
::
聚合
::
管理
117 Posts :: 2 Stories :: 61 Comments :: 0 Trackbacks
常用鏈接
我的隨筆
我的評(píng)論
我參與的隨筆
留言簿
(8)
給我留言
查看公開留言
查看私人留言
隨筆分類
《Visual C++代碼參考與技巧大全》學(xué)習(xí)筆記(60)
《Visual C++游戲編程基礎(chǔ)》學(xué)習(xí)筆記(22)
《編程之美》學(xué)習(xí)筆記
《編程珠璣》學(xué)習(xí)筆記
《算法導(dǎo)論》學(xué)習(xí)筆記
Android 開發(fā)筆記(1)
C++ primer plus學(xué)習(xí)筆記(22)
C++ Web開發(fā)(1)
Linux 學(xué)習(xí)筆記(3)
操作系統(tǒng)學(xué)習(xí)筆記(1)
高質(zhì)量程序設(shè)計(jì)指南(林銳)學(xué)習(xí)筆記
計(jì)算機(jī)組成原理學(xué)習(xí)筆記
面試筆試題積累
其他(1)
數(shù)據(jù)結(jié)構(gòu)系列學(xué)習(xí)筆記
數(shù)據(jù)庫(kù)學(xué)習(xí)筆記
數(shù)學(xué)系列學(xué)習(xí)筆記
英語(yǔ)學(xué)習(xí)積累(6)
隨筆檔案
2010年7月 (4)
2010年4月 (22)
2010年2月 (43)
2010年1月 (48)
搜索
最新評(píng)論
1.?re: 如何學(xué)習(xí)操作系統(tǒng)?——整理篇,非個(gè)人感悟
收藏,先把現(xiàn)代操作系統(tǒng)過一遍,有個(gè)基本概念后,再來(lái)好好學(xué)習(xí)一番
--星空不遠(yuǎn)
2.?re: 畫筆與畫刷
我來(lái)瞅瞅
--張一一
3.?re: 關(guān)于Aptana studio工具
驗(yàn)證碼不會(huì)變。需要刷新網(wǎng)頁(yè)。
--速度
4.?re: Ubuntu(Linux)使用Eclipse搭建C/C++編譯環(huán)境
是eclipse的版本和cdt不相符,如果用樓主的方法要改上面的網(wǎng)址,根據(jù)自己的版本改,一般都不是galileo
--Circle
5.?re: Ubuntu(Linux)使用Eclipse搭建C/C++編譯環(huán)境
@monkeyjun 你google下,我好久沒碰了
--煙皚
閱讀排行榜
1.?Ubuntu(Linux)使用Eclipse搭建C/C++編譯環(huán)境(59695)
2.?如何學(xué)習(xí)操作系統(tǒng)?——整理篇,非個(gè)人感悟(11530)
3.?GDI繪圖函數(shù)(8087)
4.?PreTranslateMessage(6527)
5.?引入lib庫(kù)到工程中(6062)
評(píng)論排行榜
1.?Ubuntu(Linux)使用Eclipse搭建C/C++編譯環(huán)境(17)
2.?這是你應(yīng)該做的(14)
3.?《Visuanl C++游戲編程基礎(chǔ)》學(xué)習(xí)筆記——索引隨筆 (5)
4.?千萬(wàn)別study English,應(yīng)學(xué)會(huì)learn English——英語(yǔ)學(xué)習(xí)方法強(qiáng)烈推薦(3)
5.?字符串輸入技巧(3)
C++ primer plus 文件處理程序
第一個(gè)程序:將內(nèi)容寫入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在這里,程序運(yùn)行之前,文件carinfo.txt并不存在。
* 在這種情況下,方法open()將新建一個(gè)名為carinfo.txt的文件。運(yùn)行該程序
* 之后,文件carinfo.txt將存在。默認(rèn)情況下,open()將首先截短該文件,即
* 將其長(zhǎng)度截短到零——丟棄原有內(nèi)容,然后將新的輸出加入到該文件中。
*
* 打開已有的文件,以接受輸出時(shí),默認(rèn)將它其長(zhǎng)度截短為零,因此原來(lái)的內(nèi)容將丟失。
*/
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
;
}
第二個(gè)程序:讀取txt文本的內(nèi)容
下面程序打開用戶指定的文件,讀取其中的數(shù)字,然后指出文件中包含多少個(gè)值以及它們的和與平均值。正確地設(shè)計(jì)輸入循環(huán)至關(guān)重要。
這個(gè)程序我運(yùn)行的時(shí)候出了一個(gè)問題,就是未能找到最后結(jié)果計(jì)算,如下圖所示,
正確的運(yùn)行結(jié)果應(yīng)該是:
很有意思,可以研究哈,我在csdn發(fā)帖詢問了,可參考其帖子:
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<<"個(gè)數(shù)="<<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
煙皚
閱讀(418)
評(píng)論(0)
編輯
收藏
引用
所屬分類:
C++ primer plus學(xué)習(xí)筆記
只有注冊(cè)用戶
登錄
后才能發(fā)表評(píng)論。
【推薦】100%開源!大型工業(yè)跨平臺(tái)軟件C++源碼提供,建模,組態(tài)!
相關(guān)文章:
數(shù)組和指針的區(qū)別與處理技巧
這是你應(yīng)該做的
VC6.0中友元函數(shù)無(wú)法訪問類私有成員的解決辦法-------VC6.0的bug
C++ primer plus第十一章 使用類的程序
C++ primer plus 關(guān)于引用的一些程序
函數(shù)指針
函數(shù)和string對(duì)象
函數(shù)和結(jié)構(gòu)
函數(shù)和C-style string
函數(shù)處理數(shù)組問題
網(wǎng)站導(dǎo)航:
博客園
IT新聞
BlogJava
博問
Chat2DB
管理
Copyright @ 煙皚
Powered by:
.Text
and
ASP.NET
Theme by:
.NET Monster
亚洲午夜久久久
|
99精品国产综合久久久久五月天
|
国产精品久久久久无码av
|
久久精品国产精品青草
|
久久久无码精品亚洲日韩软件
|
久久精品亚洲AV久久久无码
|
久久久久夜夜夜精品国产
|
日韩亚洲国产综合久久久
|
国内精品久久久久久久97牛牛
|
久久99精品久久久久久齐齐
|
久久狠狠爱亚洲综合影院
|
国产国产成人久久精品
|
99精品久久久久久久婷婷
|
日本高清无卡码一区二区久久
|
99久久国产综合精品麻豆
|
伊人久久一区二区三区无码
|
久久se精品一区二区
|
少妇精品久久久一区二区三区
|
亚洲成色WWW久久网站
|
激情综合色综合久久综合
|
国内精品久久久久影院一蜜桃
|
国产69精品久久久久APP下载
|
精品无码久久久久久久久久
|
国产婷婷成人久久Av免费高清
|
国产69精品久久久久观看软件
|
久久99精品九九九久久婷婷
|
亚洲综合久久综合激情久久
|
久久精品国产亚洲AV高清热
|
久久精品无码一区二区WWW
|
久久天天日天天操综合伊人av
|
国产91久久综合
|
一本久久久久久久
|
国产精品xxxx国产喷水亚洲国产精品无码久久一区
|
亚洲国产精品成人久久
|
成人久久免费网站
|
久久久久久久精品妇女99
|
久久天天躁狠狠躁夜夜avapp
|
亚洲欧美日韩精品久久亚洲区
|
欧美色综合久久久久久
|
性做久久久久久久久
|
久久无码国产专区精品
|