添加文件io操作

This commit is contained in:
zhuyijun 2021-08-07 23:58:11 +08:00
parent dc548c0892
commit f11b43f9ff
18 changed files with 450 additions and 4 deletions

View File

@ -17,4 +17,6 @@ foreach( appsourcefile ${APP_SOURCES} )
get_filename_component( demo_name ${appsourcefile} NAME_WE )
add_executable( ${demo_name} ${appsourcefile} )
endforeach( appsourcefile ${APP_SOURCES} )
#add_executable(formore formore.cpp)
#add_executable(formore formore.cpp)
add_executable(outfile file/outfile.cpp)
add_executable(sumafile file/sumafile.cpp)

41
base/cctypes.cpp Normal file
View File

@ -0,0 +1,41 @@
//
// Created by nicemoe on 2021/8/7.
//
#include <iostream>
#include <cctype>
using namespace std;
/**
* cctype
* @return
*/
int main()
{
cout << "Enter text for analysis, and type @"
"to terminate input. \t";
char ch;
int whitespace = 0;
int digists = 0;
int chars = 0;
int punct = 0;
int others = 0;
cin.get(ch);
while (ch != '@')
{
if (isalpha(ch)) chars++;
else if (isspace(ch)) whitespace++;
else if (isdigit(ch)) digists++;
else if (ispunct(ch)) punct++;
else others++;
cin.get(ch);
}
cout << chars << " letters, "
<< whitespace << " whitespace, "
<< digists << " digists, "
<< punct << " punctuations,"
<< others << " others. \n";
return 0;
}

53
base/enum.cpp Normal file
View File

@ -0,0 +1,53 @@
//
// Created by nicemoe on 2021/8/7.
//
/**
*
*/
#include <iostream>
using namespace std;
enum
{
red, orange, yellow, green, blue, violet, indigo
};
int main()
{
int code;
cin >> code;
while (code >= red && code <= indigo)
{
switch (code)
{
case red:
cout << "Her lips were red. \n";
break;
case orange:
cout << "Her hair were orange. \n";
break;
case yellow:
cout << "Her shoes were yellow. \n";
break;
case green:
cout << "Her nails were green. \n";
break;
case blue:
cout << "Her sweatsuit were blue. \n";
break;
case violet:
cout << "Her eyes were violet. \n";
break;
case indigo:
cout << "Her mood were indigo. \n";
break;
}
cout << "Enter color code (0-6)";
cin >> code;
}
cout << "Bye \n";
return 0;
}

47
base/file/outfile.cpp Normal file
View File

@ -0,0 +1,47 @@
//
// Created by nicemoe on 2021/8/7.
//
#include <iostream>
#include<fstream>
using namespace std;
int main()
{
char automobile[50];
int year;
double a_price;
double d_price;
ofstream outFile;
outFile.open("charinfo.txt");
cout << "Enter the make and model of automobile: ";
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;
//用一般的方式输出浮点型,例如C++程序在控制台显示的时候大一点的数,显示的时候使用了科学计数法,使用该命令即可像一般的方式显示
cout << fixed;
//设置精确度为2并返回上一次的设置。
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;
outFile<<fixed;
outFile.precision(2);
outFile.setf(ios_base::showpoint);
outFile << "Make and model: "<< automobile << endl;
outFile << "Year: "<< year << endl;
outFile << "Was asking $"<< a_price << endl;
outFile << "Now asking $"<< d_price << endl;
outFile.close();
return 0;
}

3
base/file/scores.txt Normal file
View File

@ -0,0 +1,3 @@
18 19 18.5 13.5 14
16 19.5 20 18 12 18.5
17.5

72
base/file/sumafile.cpp Normal file
View File

@ -0,0 +1,72 @@
//
// Created by nicemoe on 2021/8/7.
//
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
const int SIZE = 60;
/**
* scores.txt
*
* 18 19 18.5 13.5 14
* 16 19.5 20 18 12 18.5
* 17.5
*
*
*
*
* @return
*/
int main()
{
char filename[SIZE];
ifstream inFile;
cout << "Enter name of data file: ";
cin.getline(filename, SIZE);
inFile.open(filename);
if (!inFile.is_open())
{
cout << "Could not open the file " << filename << endl;
cout << "pROGRAM TERMINATING. \n";
exit(EXIT_FAILURE);
}
double value;
double sum = 0.0;
int count = 0;
// inFile >> value;
//inFile.good() 该方法在没有任务错误且没有遇到结束符号的时候返回为true
//inFile >> value 的返回值为inFile 在需要判断bool的时候 inFile的结果是inFile.good()
while (inFile >> value)
{
++count;
sum += value;
}
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();
return 0;
}
/*
Enter name of data file: scores.txt
End of file reached.
Items read: 12
Sum: 204.5
Average: 17.0417
*/

31
base/fun.cpp Normal file
View File

@ -0,0 +1,31 @@
//
// Created by nicemoe on 2021/8/7.
//
#include <iostream>
using namespace std;
void temp(int &a, int &b)
{
int tmp = a;
a = b;
b = tmp;
}
void temp2(int *a, int *b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}
int main()
{
int m = 10, n = 23;
temp(m, n);
cout << m << "\t" << n << endl;
temp2(&m, &n);
cout << m << "\t" << n << endl;
return 0;
}

18
base/goto.cpp Normal file
View File

@ -0,0 +1,18 @@
//
// Created by nicemoe on 2021/8/7.
//
#include <iostream>
using namespace std;
int main()
{
paris: cout << "You've just arrived at Paris.\n";
char ch;
cin >> ch;
if (ch == 'p') goto paris;
return 0;
}

View File

@ -30,8 +30,20 @@ int main()
char food[] = "carrots";
strcpy(food, "flan");
cout << *(food + sizeof food -2);
cout << *(food + sizeof food - 2) <<endl;
int m = 10, n = 20;
//指针指向的地址的值不可以更改
const int *p = &m;
p = &n;
cout << *p <<endl;
//指针指向的地址不能更改
int * const p1 = &m;
*p1 = 30;
cout << *p1 <<" " << m<<endl;
//指针指向的地址和地址的值都不可以更改
const int * const p2 = &m;
return 0;
}

28
base/structTest.cpp Normal file
View File

@ -0,0 +1,28 @@
//
// Created by nicemoe on 2021/8/7.
//
#include <iostream>
using namespace std;
struct student
{
string id;
string name;
int age;
int score;
};
int main()
{
student stu[] = {
{"1", "1", 21, 100},
{"2", "2", 22, 100}
};
cout << (stu ) <<"\t" <<stu->name << endl;
cout << (stu + 1) <<"\t" <<(stu + 1)->name << endl;
return 0;
}

View File

@ -3,7 +3,7 @@
//
#include "include/test3.h"
#include "iostream"
#include <iostream>
void print() {
std::cout << "函数";

View File

@ -3,7 +3,6 @@
//
#include <iostream>
using namespace std;
int main() {
@ -11,5 +10,6 @@ int main() {
y = (4 + x++) + (6 + x++);// 相当于 (4 +0) + (6 +1)
cout << y << endl;
cout << x;
cout << y;
return 0;
}

24
base/testin1.cpp Normal file
View File

@ -0,0 +1,24 @@
//
// Created by nicemoe on 2021/8/7.
//
#include <iostream>
using namespace std;
int main()
{
char ch;
int count = 0;
cout << "Enter characters; enter # to quit: \n";
cin >> ch;
while (ch != '#')
{
cout << ch;
++count;
cin >> ch;
}
cout << endl << count << " characters read \n";
return 0;
}

25
base/testin2.cpp Normal file
View File

@ -0,0 +1,25 @@
//
// Created by nicemoe on 2021/8/7.
//
#include <iostream>
using namespace std;
int main()
{
char ch;
int count = 0;
cout << "Enter characters; enter # to quit: \n";
cin.get(ch);
while (ch != '#')
{
cout << ch;
++count;
cin.get(ch);
}
cout << endl << count << " characters read \n";
return 0;
}

26
base/testin3.cpp Normal file
View File

@ -0,0 +1,26 @@
//
// Created by nicemoe on 2021/8/7.
//
#include <iostream>
using namespace std;
int main()
{
char ch;
int count = 0;
cout << "Enter characters; enter # to quit: \n";
cin.get(ch);
// while (!cin.fail())
while (cin)
{
cout << ch;
++count;
cin.get(ch);
}
cout << endl << count << " characters read \n";
return 0;
}

24
base/testin4.cpp Normal file
View File

@ -0,0 +1,24 @@
//
// Created by nicemoe on 2021/8/7.
//
#include <iostream>
using namespace std;
int main()
{
char ch;
int count = 0;
cout << "Enter characters; enter # to quit: \n";
while ((ch = cin.get()) != EOF)
{
cout.put(char(ch));
++count;
}
cout << endl << count << " characters read \n";
return 0;
}

18
base/typedefTest.cpp Normal file
View File

@ -0,0 +1,18 @@
//
// Created by nicemoe on 2021/8/7.
//
#include <iostream>
using namespace std;
//typedef typeName aliasName
typedef char byte;
int main()
{
byte a = 'A';
int m =a;
cout <<m;
return 0;
}

22
base/waiting.cpp Normal file
View File

@ -0,0 +1,22 @@
//
// Created by nicemoe on 2021/8/7.
//
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
cout << "Enter the delay time , in seconds: ";
float secs;
cin >> secs;
clock_t delay = secs * CLOCKS_PER_SEC;
cout <<"starting\n";
clock_t start = clock();
while (clock() - start < delay)
;
cout<< "done \n";
return 0;
}