用 C 将结构写入文件

发布于 2024-09-11 06:26:16 字数 895 浏览 1 评论 0原文

我正在读取结构并将其写入不可读的文本文件中。我必须将可读数据从结构对象写入文件中。

这是我的代码的更多细节:

我的代码读取项目名称和代码列表并将其写入文件(file.txt)。该代码使用链表概念来读取和写入数据。 数据存储到结构体对象中,然后使用 fwrite 写入文件。

代码运行良好。但我需要将可读数据写入文本文件。

现在 file.txt 如下所示,

㵅㡸䍏䥔䥆㘸䘠㵅㩃䠀\䵏㵈䑜㵅㡸䍏䥔䥆㘸䘠\㵅㩃䠀䵏㵈䑜㵅㡸䍏䥔䥆㘸䘠㵅㩃䠀䵏㵈\䑜㵅㡸䍏䥔䥆㘸䘠㵅㩃䠀䵏㵈䑜㵅㡸䍏䥔\䥆㘸䘠㵅㩃䠀䵏㵈

我期待着文件应该是这样的,

铅笔aaaa
表 bbbb
笔cccc
笔记本 nnnn

这是代码片段:

struct Item
{
char itemname[255];
char dspidc[255];
struct Item *ptrnext;
};

  // Writing into the file
printf("\nEnter Itemname: ");
gets(ptrthis->itemname);
printf("\nEnter Code: ");
gets(ptrthis->dspidc);
fwrite(ptrthis, sizeof(*ptrthis), 1, fp);

  // Reading from the file
while(fread(ptrthis, sizeof(*ptrthis), 1, fp) ==1)
{
  printf("\n%s %s", ptrthis->itemname,ptrthis->dspidc);
  ptrthis = ptrthis->ptrnext;
}

I am reading and writting a structure into a text file which is not readable. I have to write readable data into the file from the structure object.

Here is little more detail of my code:

I am having the code which reads and writes a list of itemname and code into a file (file.txt). The code uses linked list concept to read and write data.
The data are stored into a structure object and then writen into a file using fwrite.

The code works fine. But I need to write a readable data into the text file.

Now the file.txt looks like bellow,

㵅㡸䍏䥔䥆㘸䘠㵅㩃䠀\䵏㵈䑜㵅㡸䍏䥔䥆㘸䘠\㵅㩃䠀䵏㵈䑜㵅㡸䍏䥔䥆㘸䘠㵅㩃䠀䵏㵈\䑜㵅㡸䍏䥔䥆㘸䘠㵅㩃䠀䵏㵈䑜㵅㡸䍏䥔\䥆㘸䘠㵅㩃䠀䵏㵈

I am expecting the file should be like this,

pencil aaaa
Table bbbb
pen cccc
notebook nnnn

Here is the snippet:

struct Item
{
char itemname[255];
char dspidc[255];
struct Item *ptrnext;
};

  // Writing into the file
printf("\nEnter Itemname: ");
gets(ptrthis->itemname);
printf("\nEnter Code: ");
gets(ptrthis->dspidc);
fwrite(ptrthis, sizeof(*ptrthis), 1, fp);

  // Reading from the file
while(fread(ptrthis, sizeof(*ptrthis), 1, fp) ==1)
{
  printf("\n%s %s", ptrthis->itemname,ptrthis->dspidc);
  ptrthis = ptrthis->ptrnext;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

疏忽 2024-09-18 06:26:16

写入大小为 255 字节的数组将向文件写入 255 字节(无论您在该数组中填充了什么)。如果您只需要该数组的“文本”部分,则需要使用处理空终止符的工具(即 printffprintf ...)。

读取会更加复杂,因为您需要设置代表字符串结尾的哨兵值的概念。

这并没有说明您正在写入的指针的值(无论是否已初始化)在下次读取时将没有上下文或有效性。指针(即内存位置)仅在当前执行的进程内有应用。尝试在另一个进程中使用一个进程的内存地址绝对是一个坏主意。

Writing the size of an array that is 255 bytes will write 255 bytes to file (regardless of what you have stuffed into that array). If you want only the 'textual' portion of that array you need to use a facility that handles null terminators (i.e. printf, fprintf, ...).

Reading is then more complicated as you need to set up the idea of a sentinel value that represents the end of a string.

This speaks nothing of the fact that you are writing the value of a pointer (initialized or not) that will have no context or validity on the next read. Pointers (i.e. memory locations) have application only within the currently executing process. Trying to use one process' memory address in another is definitely a bad idea.

↘紸啶 2024-09-18 06:26:16

代码工作正常

实际上并非如此:

a)您将结构的原始内容转储到文件中,包括指向另一个实例(如果“Item”)的指针。您不能指望从光盘读回指针并像使用 ptrthis = ptrthis->ptrnext 那样使用它(我的意思是,这就像您在给定代码片段中“使用”它一样工作,但只是因为该片段根本没有任何意义)。

b) 您正在向文件写入 2 * 255 字节的潜在垃圾。您在文件中看到这种奇怪的“块”的原因是,您将所有 255 字节的 itemname 和 255 字节的 dspidc 写入光盘.. 包括终止\0 (这是块,具体取决于您的编辑器)。真正的“字符串”是在 itemnamedspidc 开头的有意义的内容,后跟 \0,然后是之前的记忆。

您需要查找和阅读的术语称为序列化,已经有一些库可以解决将数据结构转储到光盘(或网络或其他任何东西)并将其读回的任务,例如 tpl

The code works fine

not really:

a) you are dumping the raw contents of the struct to a file, including the pointer to another instance if "Item". you can not expect to read back in a pointer from disc and use it as you do with ptrthis = ptrthis->ptrnext (i mean, this works as you "use" it in the given snippet, but just because that snippet does nothing meaningful at all).

b) you are writing 2 * 255 bytes of potential crap to the file. the reason why you see this strange looking "blocks" in your file is, that you write all 255 bytes of itemname and 255 bytes of dspidc to the disc .. including terminating \0 (which are the blocks, depending on your editor). the real "string" is something meaningful at the beginning of either itemname or dspidc, followed by a \0, followed by whatever is was in memory before.

the term you need to lookup and read about is called serialization, there are some libraries out there already which solve the task of dumping data structures to disc (or network or anything else) and reading it back in, eg tpl.

↙温凉少女 2024-09-18 06:26:16

首先,我只会序列化数据,而不序列化指针。

然后,在我看来,你有两个选择:

  1. 为你的语法编写一个解析器(例如使用 yacc)
  2. 使用数据转储格式,例如 rmi 序列化机制。

抱歉,我找不到在线文档,但我知道我有纸上的语法。
这两种解决方案都将独立于平台,无论是大端还是小端。

First of all, I would only serialize the data, not the pointers.

Then, in my opinion, you have 2 choices:

  1. write a parser for your syntax (with yacc for instance)
  2. use a data dumping format such as rmi serialization mechanism.

Sorry I can't find online docs, but I know I have the grammar on paper.
Both of those solution will be platform independent, be them big endian or little endian.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文