c文件操作就这么简单
文件操作,稍微一总结.。
FILE是一个结构体类型,在TC和VS中定义不相同,他的作用就是在fopen()之后保存了打开文件的信息。也就是说操作系统帮我们完成了,我们不用管,大大降低了难度吧。
首先我们考虑下对文件的操作,文件类型分为文本和二进制。读写文件的函数一般前面有个f,也就表示file,open,fopen,read,fread,很容易记住。
打开文件后要读写,当前其中关于判断打开是否成功,读写是否成功。。。。我就省略了。打开时设置打开后的操作模式,这个模式直接保存在了FILE结构体的flag中。打开就一个fopen,然后我们先考虑读吧,读写的函数都比较多:
fgetc(),fgetchar(),getc();fgets(),gets(),fread(),fscanf();
前三个是读取单个字符,后三个是读取字符串,一般都是结合while判断是否到了EOF来处理。fscanf()和fprintf()较为复杂,也容易搞混淆,可以这么理解,
fscanf(fp,"%s",name); //从fp指定的文件中以字符串保存在name变量中
fprintf(fp,"%s",name); //将name以字符串的形式保存到fp指定的文件中
fread(a,b,c,d)函数功能强大,可以这么理解,从d中读取c个b长度的数据保存到a中。如:fread(buf, strlen(msg)+1, 1,fp);
fwri ......
<script>
a=62;
function encode() {
var code = document.getElementById('code').value;
code = code.replace(/[\r\n]+/g, '');
code = code.replace(/'/g, "\\'
");
var tmp = code.match(/\b(\w+)\b/g);
tmp.sort();
var dict = [];
var i, t = '';
for(var i=0; i<tmp.length; i++) {
if(tmp[i] != t) dict.push(t = tmp[i]);
}
var len = dict.length;
var ch;
for(i=0; i<len; i++) {
ch = num(i);
code = code.replace(new RegExp('\\b'+dict[i]+'\\b','g'), ch);
if(ch == dict[i]) dict[i] = '';
}
document.getElementById('code').value =
"eval(function(p,a,c,k,e,d){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)d[e(c)]=k[c]||e(c);k=[function(e){return
d[e]}];e=function(){return'\\\\w+'};c=1};while(c--)if(k[c])p=p.replace(new
RegExp('\\\\b'+e(c)+'\\\\b','g'),k[c]);return p}("
......
int main()
{
printf(&unix["\021%six\012\0"], (unix)["have"] + "fun" - 0x60);
}
gcc -S编译成汇编代码如下:
.file "test.c"
.section .rodata
.LC0:
.string "fun"
.LC1:
.string "\021%six\n"
.string ""
.text
.globl main
.type main,@function
main:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
andl $-16, %esp
movl $0, %ea ......
看了下官方文档的关于object c 的内存管理,总结下:
在iphone中object c 中的内存管理是通过计数器来管理的,每个对象内部都有一个计数器.新建一个对象,或者这个对象被其他对象引用,多会使计数器加1.Retain 增加计数器值 release 减少计数器值.当计数器为0时对象就dealloc自己.
在object c中你生成的一个对象那么你就有责任去释放它,内存管理的一般规则:
You own any object you create by allocating memory for it or copying it.
Related methods: alloc, allocWithZone:, copy, copyWithZone:, mutableCopy, mutableCopyWithZone:
If you are not the creator of an object, but want to ensure it stays in memory for you to use, you can express an ownership interest in it.
Related method: retain
If you own an object, either by creating it or expressing an ownership interest, you are responsible for releasing it when you no longer need it.
Related methods: release, autorelease
Conversely, if you are not ......
static char *file2memory(FILE *file, long *size)
{
char buffer[1024];
char *string=NULL;
char *newstring=NULL;
long len=0;
long stringlen=0;
if(file) {
while((len = fread(buffer, 1, sizeof(buffer), file))) {
if(string) {
newstring = (char*)realloc(string, len+stringlen+1);
if(newstring)
string = newstring;
else
break; /* no more strings attached! :-) */
}
else
string = (char*)malloc(len+1);
memcpy(&string[stringlen], buffer, len);
stringlen+=len;
}
if (string) {
& ......
(一)
对文件操作时有时获得文件的大小时必要的.下面是获得其大小小的较简单方法.
#include<io.h> //C语言头文件
#include<iostream> //for system();
using namespace std;
int main()
{
int handle;
handle = open("test.txt", 0x0100); //open file for read
long length = filelength(handle); //get length of file
cout<<"file length in bytes:"<<length<<endl;
close(handle);
system("pause");
return 0;
}
(二)
//用Windows API 中的 GetFileSize()获得文件长度
//假设文件file.txt 在当前目录下
//file.txt的内容为:123abc
//关于windows API函数情参考部分windows API函数或MSDN
#include <iostream>
#include <windows.h> //for windows api
using namespace std;
int main()
{
//用API函数CreateFile()创建文件句柄
HANDLE fhadle = CreateFile("file.txt", //文件名或路径
&nb ......
(一)
对文件操作时有时获得文件的大小时必要的.下面是获得其大小小的较简单方法.
#include<io.h> //C语言头文件
#include<iostream> //for system();
using namespace std;
int main()
{
int handle;
handle = open("test.txt", 0x0100); //open file for read
long length = filelength(handle); //get length of file
cout<<"file length in bytes:"<<length<<endl;
close(handle);
system("pause");
return 0;
}
(二)
//用Windows API 中的 GetFileSize()获得文件长度
//假设文件file.txt 在当前目录下
//file.txt的内容为:123abc
//关于windows API函数情参考部分windows API函数或MSDN
#include <iostream>
#include <windows.h> //for windows api
using namespace std;
int main()
{
//用API函数CreateFile()创建文件句柄
HANDLE fhadle = CreateFile("file.txt", //文件名或路径
&nb ......