fread()和fwrite()实际上如何在C编程中工作? [英] How fread() and fwrite() actually works in C programming ?

查看:71
本文介绍了fread()和fwrite()实际上如何在C编程中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用c语言开发数据库平面文件项目.i创建了一个成员很少的结构。我正在使用fwrite()将它们写入二进制文件并使用fread()来获取数据。我的两个主要问题



我们可以在文本文件中编写结构吗?我没有看到任何好的例子。以文本格式写它几乎是错误的吗?



2这些fread()& fwrite works()。它们对一个数据块进行操作如何获取下一个块的地址.i意味着我们有指针但是文件没有任何地址所以指针如何进入下一个块?



我尝试了什么:



i已经尝试过它但是为什么以及怎么做?

解决方案

这是被调用的序列化,参见序列化 - 维基百科,免费的百科全书 [ ^ ] 。



引用:

以文本格式编写它实际上是错误的吗?

不,没错。实际上,一些序列化机制使用文本文件。但是效率有点低。



看看这个页面: C - 序列化技术 - 堆栈溢出 [ ^ ]。


引用:

我们可以在文本文件中编写结构?



是的你可以。

引用:

我见过没有好的例子。



这是您的设计选择。您可以决定结构如何转换为文件以及如何将其转换回结构。

XML文件始终用于此目的。结构转换为XML,反之亦然。

引用:

以文本格式编写它实际上是错误的?



您的设计选择。请参阅XML文件。

Quote:

2nd这些fread()& fwrite works()。它们对一个数据块进行操作,它们如何获取下一个块的地址.i表示我们有指针,但文件没有任何地址,所以指针如何进入下一个块?



看起来你需要回到C / C ++基础知识

C编程语言 - 维基百科,免费的百科全书 [ ^ ]
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf [ ^ ]

http://www.ime.usp.br /~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf [ ^ ]


我们可以在文本文件中编写结构吗?我没有看到任何好的例子。用文本格式写它几乎是错误的吗?





如果,按结构,你的意思是二进制数据(例如,不是ASCII),不,你不应该使用文本模式。如果你想在没有C运行时应用一些规则(例如熟数据)来影响回车,换行和 - 对于某些平台 - 文件结束标记的情况下操作字节,则将文件打开为二进制文件。



第二,这些fread()& fwrite works()。它们对数据块进行操作如何获取下一个块的地址.i表示我们有指针,但文件没有任何地址,所以指针如何转到下一个块?





文件系统处理这个问题。存储数据的是隐式地址或文件偏移量。每次调用fwrite都会导致偏移量移动,因此默认情况下,您始终会将数据附加到文件中。您还可以使用fseek更改此偏移量,并使用ftell检查偏移量。您无需担心磁盘扇区,块或集群的组织方式。


i am working on a database flat file project using c language .i have created a structure with few members . i am using fwrite() to write them in binary file and fread() to fetch the data .My two major question

1st can we write structure in text file ? i have seen no good example .is it practically wrong to write it in text format ?

2nd how these fread() & fwrite works().they operate on a block of data how they get the address of next block .i mean we do have the pointer but file doesnt have any address so how the pointer go to next block ?

What I have tried:

i have tried it works but why and how ?

解决方案

That's callsed Serialization, see Serialization - Wikipedia, the free encyclopedia[^].

Quote:

is it practically wrong to write it in text format ?

No, it is not wrong. In fact, some serialization mechanisms use text files. It is a bit inefficient, though.

Have a look at this page: C - serialization techniques - Stack Overflow[^].


Quote:

1st can we write structure in text file ?


Yes you can.

Quote:

i have seen no good example .


This your design choice. You decide how the structure translate to file and how you get it back to structure.
XML files are used for this all the time. Structures are translates to XML and vice-versa.

Quote:

is it practically wrong to write it in text format ?


You design choice. See XML files.

Quote:

2nd how these fread() & fwrite works().they operate on a block of data how they get the address of next block .i mean we do have the pointer but file doesnt have any address so how the pointer go to next block ?


Looks like you need to go back to C/C++ basics
The C Programming Language - Wikipedia, the free encyclopedia[^]
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf[^]
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf[^]


1st can we write structure in text file ? i have seen no good example .is it practically wrong to write it in text format ?



If, by structure, you mean binary data (eg. not ASCII), no you should not use text mode. Open the file as binary if you want to operate on bytes without the C runtime applying some rules (eg. "cooked" data) that effect carriage return, line feed, and - for some platforms - end of file marker.

2nd how these fread() & fwrite works().they operate on a block of data how they get the address of next block .i mean we do have the pointer but file doesnt have any address so how the pointer go to next block ?



The file system takes care of this. There's an implicit address or file offset where the data is stored. Each call to fwrite causes the offset to move so, by default, you are always appending data to the file. You can also change this offset by using fseek and examine the offset by using ftell. You do not need to worry about how disk sectors, blocks, or clusters are organized.


这篇关于fread()和fwrite()实际上如何在C编程中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆