The File Functions in C

本文详细介绍了C语言中文件操作的基本方法,包括文件的打开、读取、写入及关闭等核心过程。通过实例演示了如何从一个文件读取整数,并将其写入另一个文件中。

在平时写程序时,程序员经常要用到IO操作,用的最多的一般是与控制台进行交互(标准的输入输出),但往往也需要对已命名的文件进行读写操作。本文主要分析C语言对文件操作的支持,在以后的文章中再另外分析C++中标准的IO库。(2010-3-26 wcdj)



[磁盘文件数据存储方式]
实际上,文件是在计算机内存中以二进制表示的数据在外部存储介质上的另一种存放形式。文件通常分为二进制文件和文本文件。二进制文件是包含在ASCII及扩展ASCII字符中编写的数据或程序指令的文件。一般是可执行文件(exe)、图形、图像、声音等文件。而文本文件(通常也成为ASCII文件),它的每一字节存放的是可表示为一个字符的ASCII代码的文件。这里把文件区分为二进制文件和文本文件,但实际上它们都是以二进制数据的方式来存储的。文本文件里所存储的每一个字节都可以转化为一个可读的字符。譬如'a','b',但在内存中并不会存储'a','b',而是存储它们的ASCII码:61h和62h。

[请勿混淆]
在计算机中所有文件都是二进制的。所谓文本,Unicode都是编码方式,按某种编码方式读出来的时候才产生的不同。
数据就是数据,关键看你怎么使用了,如果说区别,就是文本文件载入的时候,会把回车/r换行/n,2个符号转换成回车/n符号,而吃掉换行符/r,存盘的时候把这个吃掉的换行符再吐回去。至于也可以把文本文件当初数据文件来读,纯粹是看应用了。就好比有人问10进制,16进制的区别,纯粹就是一回事。
二进制文件和文本文件都是数据,只不过看问题的角度不同而已,他们都是一样的。好比'1',你用文本文件看是'1',你用16进制工具看是0x31,实际上都是0x31,或者是0011 0001(十进制49)。
从文件系统的角度,所有的文件都是一个顺序的比特集合,区别在于程序如何解读这些数据,一段数据可以看成是文本,图像或者声音。

[C语言对文件操作的支持]
在C语言中,对于文件的操作都是利用FILE结构体来进行的。包括文件的打开、文件的读写、文件的关闭、文件指针的定位等。

[一道基本的对文件操作的例子]

[Reference]

 


 

Write a program to simulate a process of translation from a logical address to physical address. Assumptions 1. Assume the file la.txt includes the sequence of generated addresses from CPU. 2. Use a part of memory as backing store that store data for a process. 3. The backing store size is 128 bytes 4. The size of process p is 128 bytes. 5. The contents of p is included in a file pdata.bin which is a binary file. 6. Use a part of memory as RAM. The size of physical memory is 256 bytes, from 0 to 255. All the physical memory is available, allocating starting from beginning in sequence. That is, allocate frame 0 first, then frame 1, then frame 2…. 7. The size of a frame is 32 bytes, i.e., 5 bits for the offset in a frame, total number of frames is 8. 8. At beginning, no page table is available for process p. Requirements Write a program to 1. Setup a simulating backing store in memory. Read the data from pdata.bin to this backing store. 2. Initialize a page table for process p, set the frame number to be -1 for each page, indicating that the page is not loaded into memory yet. 3. Read logical addresses one by one from la.txt. 4. For each logical address, a) if its page has been loaded into physical memory, simply find the frame number in the page table, then generate physical address, find and print out the physical address and data inside this address. b) if the page is used for the first time, i.e., in page table, its frame number is -1,then the page that contains this address should be loaded into a free frame in physical memory (RAM). Then update the page table by adding the frame number to the right index in the page table. Then repeat 4a). Refer to Figure 1 for the relationships and how physical memory, backing store, and CPU are simulated. Figure 1 How physical memory, backing store and CPU are simulated in this program assignment Hints: a) use a memory block pointed by a pointer or use an array as a simulation of backing store b) use functions fread or mmap for the binary file read. Search through the Internet for the usage of these functions. c) Use an array to simulate the memory. d) Use bit operators &, |, <<, and >> to get the bits in a logic address or form a physical address e) Use char for the type of data in the process, use unsigned char (8 bits) for the type of address. Coding & Submission 1. Using pure C to finish this program. 2. Put all the codes in one .c file named PA3_#####.c, replace “#####” as the last 5 digits of your student ID. 3. Put pdata.txt and la.txt in the same folder as PA3_#####.c, which the need .txt file can be open directly by filename instead of absolute path. 4. Submit only the .c file mentioned above.使用C语言完成
05-24
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值