在C++中,可以使用多种方法对文件进行操作,如基于C的文件操作、基于C++的文件操作等等;
◆基于C的文件操作
在ANSI C中,对文件的操作分为两种方式,即流式文件操作和I/O文件操作,下面就分别介绍之。
一.流式文件操作
这种方式的文件操作有一个重要的结构FILE,FILE在stdio.h中定义如下:
typedef struct {
int level; /* fill/empty level of buffer */
unsigned flags; /* File status flags */
char fd; /* File descriptor */
unsigned char hold; /* Ungetc char if no buffer */
int bsize; /* Buffer size */
unsigned char _FAR *buffer; /* Data transfer buffer */
unsigned char _FAR *curp; /* Current active pointer */
unsigned istemp; /* Temporary file indicator */
short token; /* Used for validity checking */
} FILE; /* This is the FILE object */
FILE这个结构包含了文件操作的基本属性,对文件的操作都要通过这个结构的指针来进行,此种文件操作常用的函数见下表函数 功能
fopen() 打开流
fclose() 关闭流
fputc() 写一个字符到流中
fgetc() 从流中读一个字符
fseek() 在流中定位到指定的字符
fput ......
在C++中,可以使用多种方法对文件进行操作,如基于C的文件操作、基于C++的文件操作等等;
◆基于C的文件操作
在ANSI C中,对文件的操作分为两种方式,即流式文件操作和I/O文件操作,下面就分别介绍之。
一.流式文件操作
这种方式的文件操作有一个重要的结构FILE,FILE在stdio.h中定义如下:
typedef struct {
int level; /* fill/empty level of buffer */
unsigned flags; /* File status flags */
char fd; /* File descriptor */
unsigned char hold; /* Ungetc char if no buffer */
int bsize; /* Buffer size */
unsigned char _FAR *buffer; /* Data transfer buffer */
unsigned char _FAR *curp; /* Current active pointer */
unsigned istemp; /* Temporary file indicator */
short token; /* Used for validity checking */
} FILE; /* This is the FILE object */
FILE这个结构包含了文件操作的基本属性,对文件的操作都要通过这个结构的指针来进行,此种文件操作常用的函数见下表函数 功能
fopen() 打开流
fclose() 关闭流
fputc() 写一个字符到流中
fgetc() 从流中读一个字符
fseek() 在流中定位到指定的字符
fput ......
C/C++: 十六进制转10进制源码
收藏
view plain
copy to clipboard
print
?
int
hex_char_value(
char
c)
{
if
(c >=
'0'
&& c <=
'9'
)
return
c -
'0'
;
else
if
(c >=
'a'
&& c <=
'f'
)
return
(c -
'a'
+ 10);
else
if
(c >=
'A'
&& c <=
'F'
)
return
(c -
'A'
+ 10);
assert(0);
......
C/C++: 十六进制转10进制源码
收藏
view plain
copy to clipboard
print
?
int
hex_char_value(
char
c)
{
if
(c >=
'0'
&& c <=
'9'
)
return
c -
'0'
;
else
if
(c >=
'a'
&& c <=
'f'
)
return
(c -
'a'
+ 10);
else
if
(c >=
'A'
&& c <=
'F'
)
return
(c -
'A'
+ 10);
assert(0);
......
--------如果你用的是或打算用Eclipse For C++,那么从这里看,
1. 首先下载Eclipse for C++, 最新版是基于Eclipse 3.5.2的,eclipse-cpp-galileo-SR2-win32.zip
2. 解压,直接运行。注意,至少JDK你已经安装了(我用的是JDK1.6)。运行后welcome页面出现,进入Tutorials。先读Tutorial是个好习惯。
3. 首先 什么是CDT... 然后说,没包含C/C++的编译器、调试器,你得自己弄。
--------如果用的是或打算用的是Eclipse 不For C++的,比如偶用的Eclipse Classic. 那么从这里看
1. 首先下载Eclipse, 本文基于的是eclipse-SDK-3.5.2-win32.zip
2. 解压,直接运行。注意,至少JDK你已经安装了(我用的是JDK1.6)。运行后welcome页面出现,进入Tutorials。先读Tutorial是个好习惯。
3. 然后,至Help|Install new Software,输入http://download.eclipse.org/tools/cdt/releases/galileo下载CDT main feature;
CDT说它没包含C/C++的编译器、调试器,你得自己弄。
(当然也可以先把CTD下载回来,自已搞到eclipse下,但我弄了几次都没成功)
-----------------------------------------------------------------------------------------------
4. 继续。Windows下,MinGW ......
--------如果你用的是或打算用Eclipse For C++,那么从这里看,
1. 首先下载Eclipse for C++, 最新版是基于Eclipse 3.5.2的,eclipse-cpp-galileo-SR2-win32.zip
2. 解压,直接运行。注意,至少JDK你已经安装了(我用的是JDK1.6)。运行后welcome页面出现,进入Tutorials。先读Tutorial是个好习惯。
3. 首先 什么是CDT... 然后说,没包含C/C++的编译器、调试器,你得自己弄。
--------如果用的是或打算用的是Eclipse 不For C++的,比如偶用的Eclipse Classic. 那么从这里看
1. 首先下载Eclipse, 本文基于的是eclipse-SDK-3.5.2-win32.zip
2. 解压,直接运行。注意,至少JDK你已经安装了(我用的是JDK1.6)。运行后welcome页面出现,进入Tutorials。先读Tutorial是个好习惯。
3. 然后,至Help|Install new Software,输入http://download.eclipse.org/tools/cdt/releases/galileo下载CDT main feature;
CDT说它没包含C/C++的编译器、调试器,你得自己弄。
(当然也可以先把CTD下载回来,自已搞到eclipse下,但我弄了几次都没成功)
-----------------------------------------------------------------------------------------------
4. 继续。Windows下,MinGW ......
@echo off
@echo ╔┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉╗
@echo ┋ 心 如 止 水 ┋
@echo ┋ ┋
@echo ┋ DIY 个 性 BAT C 盘 防 毒 批 处 理 ┋
@echo ┋ ┋
@echo ┋ 预防LOGO病毒和网络执法官的批处理! 请相信我们! ┋
@echo ┋ ┋
@echo ┋ ┋
@echo ┋ QQ:24500012 ┋
@echo ┋ ┋
@echo ┋ QQ群:3334143 ┋
@echo ╚┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉╝
@pause
md %systemroot%\0sy.exe
attrib %systemroot%\0sy.exe +S +R +H
md %systemroot%\1sy.exe
attrib %systemroot%\1sy.exe +S +R +H
md %systemroot%\2sy.exe
attrib %systemroot%\2sy.exe +S +R +H
md ......
什么时候需要创建线程池呢?简单的说,如果一个应用需要频繁的创建和销毁线程,而任务执行的时间又非常短,这样线程创建和销毁的带来的开销就不容忽视,这时也是线程池该出场的机会了。如果线程创建和销毁时间相比任务执行时间可以忽略不计,则没有必要使用线程池了。
下面是Linux系统下用C语言创建的一个线程池。线程池会维护一个任务链表(每个CThread_worker结构就是一个任务)。
pool_init()函数预先创建好max_thread_num个线程,每个线程执thread_routine ()函数。该函数中
1. while (pool->cur_queue_size == 0)
2. {
3. pthread_cond_wait (&(pool->queue_ready),&(pool->queue_lock));
4. }
表示如果任务链表中没有任务,则该线程出于阻塞等待状态。否则从队列中取出任务并执行。
pool_add_worker()函数向线程池的任务链表中加入一个任务,加入后通过调用pthread_cond_signal (&(pool->queue_ready))唤醒一个出于阻塞状态的线程(如果有的话)。
pool_destroy ()函数用于销毁线程池,线程池任务链表中的任务不会再被执行,但是正在运行的线程会一直把任务运行完后再退出。
下面贴出完整代码
1. #include
2. #include
3 ......
什么时候需要创建线程池呢?简单的说,如果一个应用需要频繁的创建和销毁线程,而任务执行的时间又非常短,这样线程创建和销毁的带来的开销就不容忽视,这时也是线程池该出场的机会了。如果线程创建和销毁时间相比任务执行时间可以忽略不计,则没有必要使用线程池了。
下面是Linux系统下用C语言创建的一个线程池。线程池会维护一个任务链表(每个CThread_worker结构就是一个任务)。
pool_init()函数预先创建好max_thread_num个线程,每个线程执thread_routine ()函数。该函数中
1. while (pool->cur_queue_size == 0)
2. {
3. pthread_cond_wait (&(pool->queue_ready),&(pool->queue_lock));
4. }
表示如果任务链表中没有任务,则该线程出于阻塞等待状态。否则从队列中取出任务并执行。
pool_add_worker()函数向线程池的任务链表中加入一个任务,加入后通过调用pthread_cond_signal (&(pool->queue_ready))唤醒一个出于阻塞状态的线程(如果有的话)。
pool_destroy ()函数用于销毁线程池,线程池任务链表中的任务不会再被执行,但是正在运行的线程会一直把任务运行完后再退出。
下面贴出完整代码
1. #include
2. #include
3 ......
C/C++ optimizing compilers are great--but there *are* a few techniques for hand-tuning your code to run as efficiently as possible on the AMD Athlon64 and Opteron processors, especially when developing DLLs, device drivers, or other performance-bound pieces of code.
Alan Zeichick
Share |
6/21/2004
Sidebar: Know the Code
A code profiler can help determine which parts of your application should be optimized. You should always use a profiler both before and after optimization to see if your change actually made an improvement—with C/C++, you can never be sure if your hand-tuning methods are better than those of your optimizing compiler!
The smart engineers at AMD have spent a lot of time identifying places where C/C++ developers can optimize code for best performance. Some of these optimizations might look familiar—they're generally great coding practice for any 32-bit or 64-bit platform or operating system. In fact, most of these suggestions ......
C/C++ optimizing compilers are great--but there *are* a few techniques for hand-tuning your code to run as efficiently as possible on the AMD Athlon64 and Opteron processors, especially when developing DLLs, device drivers, or other performance-bound pieces of code.
Alan Zeichick
Share |
6/21/2004
Sidebar: Know the Code
A code profiler can help determine which parts of your application should be optimized. You should always use a profiler both before and after optimization to see if your change actually made an improvement—with C/C++, you can never be sure if your hand-tuning methods are better than those of your optimizing compiler!
The smart engineers at AMD have spent a lot of time identifying places where C/C++ developers can optimize code for best performance. Some of these optimizations might look familiar—they're generally great coding practice for any 32-bit or 64-bit platform or operating system. In fact, most of these suggestions ......