文件和流
/*1.文件操作这需要用到 C++ 中另一个标准库 fstream2.对于标准库 fstream ,它定义了三个新的数据类型:数据类型 描述ofstream 表示输出文件流,用于创建文件并向文件写入信息ifstream 表示输入文件流,用于从文件读取信息fstream 表示文件流,且同时具有 ofstream 和 ifstream 两种功能,这意味着它可以创建文件,向文件写入信息,从文件读取信息记忆:OutFileStream --> ofstreamInFileStream --> ifstream3.打开文件open() 函数是 fstream、ifstream 和 ofstream 对象的一个成员e.g.void open(const char *filename, ios::openmode mode);参数说明:(以下模式可以结合使用)ios::app 追加模式。所有写入都追加到文件末尾ios::ate 文件打开后定位到文件末尾ios::in 打开文件用于读取ios::out 打开文件用于写入ios::trunc 如果该文件已经存在,其内容将在打开文件之前被截断,即把文件长度设为 0例如:1.以写入模式打开文件,并希望截断文件,以防文件已存在ofstream outfile; // 实例化一个文件对象 outfileoutfile.open("file.dat", ios::out | ios::trunc ); // 调用文件对象,并使用 open 函数成员2.打开一个文件用于读写ifstream afile;afile.open("file.dat", ios::out | ios::in );4.读取文件使用流提取运算符 >> 从文件读取信息5.写入文件使用流插入运算符 << 向文件写入信息6.关闭文件( close()函数是 fstream、ifstream 和 ofstream 对象的一个成员)void close();7.文件位置指针1.istream 和 ostream 都提供了用于重新定位"文件位置指针"的成员函数istream 的 seekg() // 含义"seek get"ostream 的 seekp() // 含义"seek put"参数:(不写参数,默认ios::beg)ios::beg // begin 从开头 开始定位ios::cur // current 从当前位置 开始定位ios::end // end 从末尾 开始定位文件位置指针,是一个整数值指定了从文件的起始位置到指针所在位置的字节数
*/
#include <fstream>
#include <iostream>
using namespace std;int main ()
{char data[100];// 以写模式打开文件ofstream outfile;outfile.open("afile.dat");cout << "Writing to the file" << endl;cout << "Enter your name: "; cin.getline(data, 100);// 向文件写入用户输入的数据outfile << data << endl;cout << "Enter your age: "; cin >> data;cin.ignore();// 再次向文件写入用户输入的数据outfile << data << endl;// 关闭打开的文件outfile.close();// 以读模式打开文件ifstream infile; infile.open("afile.dat"); cout << "Reading from the file" << endl; infile >> data; // 在屏幕上写入数据cout << data << endl;// 再次从文件读取数据,并显示它infile >> data; cout << data << endl; // 关闭打开的文件infile.close();return 0;
}
// 定位到 fileObject 的第 n 个字节(假设是 ios::beg)
fileObject.seekg( n );// 把文件的读指针从 fileObject 当前位置向后移 n 个字节
fileObject.seekg( n, ios::cur );// 把文件的读指针从 fileObject 末尾往回移 n 个字节
fileObject.seekg( n, ios::end );// 定位到 fileObject 的末尾
fileObject.seekg( 0, ios::end );
异常处理
try
{// 尝试执行代码...
}
catch (ExceptionName e1) // 捕获异常,解决异常
{// catch 块
}
catch (ExceptionName e2)
{// catch 块
}
catch (ExceptionName eN)
{// catch 块
}
double division(int a, int b)
{if( b == 0 ){throw "Division by zero condition!"; // 抛出异常}return (a/b);
}
/*定义新的异常可以通过继承和重载 exception 类来定义新的异常。下面的实例演示了如何使用 std::exception 类来实现自己的异常:
*/#include <iostream>
#include <exception>
using namespace std;struct MyException : public exception // 通过让 MyException 继承 exception,实现自定义异常
{const char * what() const throw (){return "C++ Exception";}
};int main()
{try{throw MyException();}catch (MyException& e){std::cout << "MyException caught" << std::endl;std::cout << e.what() << std::endl;}catch (std::exception& e){//其他的错误}
}
多线程
/*<线程>1.什么是多线程?多线程是"多任务处理"的一种特殊形式多线程程序包含可以同时运行的两个或多个部分这样的程序中的每个部分称为一个线程每个线程定义了一个单独的执行路径2.创建线程e.g.#include <pthread.h>pthread_create (thread, attr, start_routine, arg)参数说明:参数 描述thread 指向线程标识符指针。attr 一个不透明的属性对象,可以被用来设置线程属性。您可以指定线程属性对象,也可以使用默认值 NULL。start_routine 线程运行函数起始地址,一旦线程被创建就会执行。arg 运行函数的参数。它必须通过把引用作为指针强制转换为 void 类型进行传递。如果没有传递参数,则使用 NULL。创建线程成功时,函数返回 0,若返回值不为 0 则说明创建线程失败
*/#include <iostream>
// 必须的头文件
#include <pthread.h>using namespace std;#define NUM_THREADS 5// 线程的运行函数
void* say_hello(void* args)
{cout << "Hello Runoob!" << endl;return 0;
}int main()
{// 定义线程的 id 变量,多个变量使用数组pthread_t tids[NUM_THREADS];for (int i = 0; i < NUM_THREADS; ++i){//参数依次是:创建的线程id,线程参数,调用的函数,传入的函数参数int ret = pthread_create(&tids[i], NULL, say_hello, NULL);if (ret != 0){cout << "pthread_create error: error_code=" << ret << endl;}}//等各个线程退出后,进程才结束,否则进程强制结束了,线程可能还没反应过来;pthread_exit(NULL);
}
Web 编程
/*1.什么是 CGI?1.公共网关接口(CGI),是一套标准,定义了信息是如何在 Web 服务器和客户端脚本之间进行交换的2.CGI 规范目前是由 NCSA 维护的,NCSA 定义 CGI 如下公共网关接口(CGI),是一种用于外部网关程序与信息服务器(如 HTTP 服务器)对接的接口标准2.Web 服务器配置CGI 编程之前,请确保您的 Web 服务器支持 CGI,并已配置成可以处理 CGI 程序所有由 HTTP 服务器执行的 CGI 程序,都必须在预配置的目录中该目录称为 CGI 目录,按照惯例命名为 /var/www/cgi-bin虽然 CGI 文件是 C++ 可执行文件,但是按照惯例它的扩展名是 .cgi————————————————————————————————————————默认情况下,Apache Web 服务器会配置在 /var/www/cgi-bin 中运行 CGI 程序如果您想指定其他目录来运行 CGI 脚本,您可以在 httpd.conf 文件中修改以下部分:<Directory "/var/www/cgi-bin">AllowOverride NoneOptions ExecCGIOrder allow,denyAllow from all</Directory><Directory "/var/www/cgi-bin">Options All</Directory>————————————————————————————————————————在这里,我们假设已经配置好 Web 服务器并能成功运行,你可以运行任意的 CGI 程序,比如 Perl 或 Shell 等编译下面的代码,把可执行文件命名为 cplusplus.cgi,并把这个文件保存在 /var/www/cgi-bin 目录中在运行 CGI 程序之前,请使用 chmod 755 cplusplus.cgi UNIX 命令来修改文件模式,确保文件可执行
*/#include <iostream>
using namespace std;int main()
{cout << "Content-type:text/html\r\n\r\n";cout << "<html>\n";cout << "<head>\n";cout << "<title>Hello World - 第一个 CGI 程序</title>\n";cout << "</head>\n";cout << "<body>\n";cout << "<h2>Hello World! 这是我的第一个 CGI 程序</h2>\n";cout << "</body>\n";cout << "</html>\n";return 0;
}