[cpp]: <fstream> - read_from_file
一、介绍
1、介绍:从文件【big.cpp】读取内容,然后将【读取内容】输出到【屏幕】。
2、主程序源文件:iotest.cpp
3、被读取的文件:big.cpp
二、源代码
1、主程序源代码:iotest.cpp
1 /* file_name = iotest.cpp
2 *
3 * date = "2024-01-11"
4 *
5 * */
6
7
8 #include <iostream>
9 #include <fstream>
10 #include <string>
11 #include <locale>
12
13
14 using namespace std;
15
16
17 void read_from_keybord()
18 {
19 string ptin = "[ Input a string ]: ";
20 string ptout = "[ Output ]: ";
21 string s ;
22 cout << ptin << flush ;
23 //cin >> s;
24 getline(cin, s);
25 cout << ptout << s << endl;
26 }
27
28
29 void read_from_file()
30 {
31 // set buffer size
32 char s[1020][1020];
33
34 fstream to_stream;
35 to_stream.open("big.cpp", ios::in);
36
37 int end_file = 0;
38 for(int i=0; to_stream.getline(s[i], 1000); i++){ end_file = i+1; }
39 // set end of s[]
40 s[end_file][0]='\n';
41
42 for(int i=0; s[i][0]!='\n'; i++){ cout << i << "\t" << s[i] << endl; }
43
44 to_stream.close();
45 }
46
47
48 int main(int argc, char *argv[], char *envp[])
49 {
50 read_from_file();
51
52 return 0;
53 }
2、被读取的文件:big.cpp
1 /* file_name = big.cpp
2 *
3 * date = "2024-01-11"
4 *
5 * */
6
7
8 #include <iostream>
9 #include <string>
10
11
12 using namespace std;
13
14
15 class name{};
16
17
18 class big :name
19 {
20 public:
21 big() { cout << "big() "<< endl; }
22
23 big(string n, string i, string f):name{n},id{i},info{f}
24 {
25 cout << " [constructor]: big(string, string, string) "<< endl;
26 }
27
28 ~big(){ cout << " [destructor]: ~big()" << endl;}
29
30 void msg()
31 {
32 cout <<"\tid := " << id << endl;
33 cout <<"\tname := " << name << endl;
34 cout <<"\tinfo := " << info << endl;
35 }
36
37 private:
38 string name;
39 string id;
40 string info;
41 };
42
43
44 int main(int argc, char *argv[], char *envp[])
45 {
46 big b("file", "0xF0B1", "memory");
47 b.msg();
48
49 return 0;
50 }
三、运行结果
1 [wit@fedora io]$ alias | grep "gpp"
2 alias gpp='g++ -g -Wall -std=c++20 -o'
3 [wit@fedora io]$
4 [wit@fedora io]$
5 [wit@fedora io]$ gpp iotest iotest.cpp && ./iotest
6 0 /* file_name = big.cpp
7 1 *
8 2 * date = "2024-01-11"
9 3 *
10 4 * */
11 5
12 6
13 7 #include <iostream>
14 8 #include <string>
15 9
16 10
17 11 using namespace std;
18 12
19 13
20 14 class name{};
21 15
22 16
23 17 class big :name
24 18 {
25 19 public:
26 20 big() { cout << "big() "<< endl; }
27 21
28 22 big(string n, string i, string f):name{n},id{i},info{f}
29 23 {
30 24 cout << " [constructor]: big(string, string, string) "<< endl;
31 25 }
32 26
33 27 ~big(){ cout << " [destructor]: ~big()" << endl;}
34 28
35 29 void msg()
36 30 {
37 31 cout <<"\tid := " << id << endl;
38 32 cout <<"\tname := " << name << endl;
39 33 cout <<"\tinfo := " << info << endl;
40 34 }
41 35
42 36 private:
43 37 string name;
44 38 string id;
45 39 string info;
46 40 };
47 41
48 42
49 43 int main(int argc, char *argv[], char *envp[])
50 44 {
51 45 big b("file", "0xF0B1", "memory");
52 46 b.msg();
53 47
54 48 return 0;
55 49 }
56 [wit@fedora io]$
57 [wit@fedora io]$
58 [wit@fedora io]$ gpp big big.cpp && ./big
59 [constructor]: big(string, string, string)
60 id := 0xF0B1
61 name := file
62 info := memory
63 [destructor]: ~big()
64 [wit@fedora io]$
65 [wit@fedora io]$
66 [wit@fedora io]$
四、参考资料
1、 getline() -- https://cplusplus.com/reference/istream/istream/getline/
本文由 lnlidawei 原创、整理、转载,本文来自于【博客园】; 整理和转载的文章的版权归属于【原创作者】; 转载或引用时请【保留文章的来源信息】:https://www.cnblogs.com/lnlidawei/p/17957625