讀取文字檔有很多方式,在此歸納出最精簡的程式寫法。
若要一行一行的讀取文字檔,可使用以下寫法。
1

/**/
/*
2
(C) OOMusou 2006 http://oomusou.cnblogs.com
3
4
Filename : ReadTextFilePerLine.cpp
5
Compiler : Visual C++ 8.0 / ISO C++
6
Description : Demo how to read text file per line
7
Release : 10/15/2006
8
*/
9
10
#include
<
iostream
>
11
#include
<
fstream
>
12
#include
<
string
>
13
14
using
namespace
std;
15
16
int
main()
{
17
ifstream inFile("books.txt");
18
string line;
19
20
while(getline(inFile,line))
{
21
cout << line << endl;
22
}
23
24
inFile.close();
25
26
return 0;
27
}


2

3

4

5

6

7

8

9

10

11

12

13

14

15

16



17

18

19

20



21

22

23

24

25

26

27

執行結果




若在一行一行讀取文字檔的同時,還想同時讀出每一個字串,可用以下寫法。
1

/**/
/*
2
(C) OOMusou 2006 http://oomusou.cnblogs.com
3
4
Filename : ReadTextFilePerLineWord.cpp
5
Compiler : Visual C++ 8.0 / ISO C++
6
Description : Demo how to read text file per line
7
Release : 10/15/2006
8
*/
9
#include
<
iostream
>
10
#include
<
fstream
>
11
#include
<
string
>
12
#include
<
sstream
>
13
14
using
namespace
std;
15
16
int
main()
{
17
ifstream inFile("books.txt");
18
string line;
19
20
while(getline(inFile,line))
{
21
cout << line << endl;
22
istringstream ss(line);
23
string word;
24
while(ss >> word)
{
25
cout << word << endl;
26
}
27
cout << endl;
28
}
29
30
inFile.close();
31
32
return 0;
33
}


2

3

4

5

6

7

8

9

10

11

12

13

14

15

16



17

18

19

20



21

22

23

24



25

26

27

28

29

30

31

32

33

執行結果
















若只要讀取文字檔中的每個字,使用while()的方式,可直接處理字串。
1

/**/
/*
2
(C) OOMusou 2006 http://oomusou.cnblogs.com
3
4
Filename : ReadTextFilePerWord.cpp
5
Compiler : Visual C++ 8.0 / ISO C++
6
Description : Demo how to read text file per word
7
Release : 12/07/2006
8
*/
9
10
#include
<
iostream
>
11
#include
<
fstream
>
12
#include
<
string
>
13
14
using
namespace
std;
15
16
int
main()
{
17
ifstream inFile("books.txt");
18
string str;
19
20
while(infile >> str)
21
cout << str << endl;
22
23
inFile.close();
24
25
return 0;
26
}


2

3

4

5

6

7

8

9

10

11

12

13

14

15

16



17

18

19

20

21

22

23

24

25

26

另外一種方式,使用copy() algorithm將文字都讀到vector中,再做後續的加工處理,優點是程式超短,缺點是要多浪費一個vector。
1

/**/
/*
2
(C) OOMusou 2006 http://oomusou.cnblogs.com
3
4
Filename : ReadTextByCopy.cpp
5
Compiler : Visual C++ 8.0 / ISO C++
6
Description : Demo how to read file per string by copy() algorithm
7
Release : 12/17/2006 1.0
8
*/
9
#include
<
iostream
>
10
#include
<
fstream
>
11
#include
<
vector
>
12
#include
<
string
>
13
#include
<
algorithm
>
14
15
using
namespace
std;
16
17
int
main()
{
18
ifstream inFile("books.txt");
19
vector<string> svec;
20
copy(istream_iterator<string>(inFile), istream_iterator<string>(), back_inserter(svec));
21
copy(svec.begin(), svec.end(), ostream_iterator<string>(cout,"\n"));
22
23
inFile.close();
24
25
return 0;
26
}


2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17



18

19

20

21

22

23

24

25

26

執行結果










(02/20/2007 更新) 有網友問我怎麼將文字檔讀到二維陣列處理,以下是處理的方式
文字檔


1

/**/
/*
2
(C) OOMusou 2007 http://oomusou.cnblogs.com
3
4
Filename : ReadTextFilePerLineWordToArray.cpp
5
Compiler : Visual C++ 8.0 / gcc 3.4.2 / ISO C++
6
Description : Demo how to read text file to 2 dim array
7
Release : 02/20/2007 1.0
8
*/
9
#include
<
iostream
>
10
#include
<
fstream
>
11
#include
<
string
>
12
#include
<
sstream
>
13
14
using
namespace
std;
15
16
int
main()
{
17
ifstream inFile("source.txt");
18
const int xsize = 5;
19
const int ysize = 2;
20
string (*arr)[xsize] = new string[ysize][xsize];
21
22
string line;
23
int y = 0;
24
while(getline(inFile,line))
{
25
istringstream ss(line);
26
string word;
27
int x = 0;
28
while(ss >> word)
{
29
arr[y][x] = word;
30
++x;
31
}
32
++y;
33
}
34
35
inFile.close();
36
37
for(int y = 0; y != ysize; ++y)
{
38
for(int x = 0; x != xsize; ++x)
{
39
cout << arr[y][x] << " ";
40
}
41
cout << endl;
42
}
43
44
delete []arr;
45
46
return 0;
47
}


2

3

4

5

6

7

8

9

10

11

12

13

14

15

16



17

18

19

20

21

22

23

24



25

26

27

28



29

30

31

32

33

34

35

36

37



38



39

40

41

42

43

44

45

46

47

執行結果

