- 论坛徽章:
- 0
|
程序结束后,结构中的数据并没有写入文件。
不知道为什么确会显示在屏幕上。
- #include <stdio.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <unistd.h>
- typedef struct stu {
- char name[20];
- char number[10];
- }* nstu;
- int err(char * text)
- {
- printf ("Error: %s\n", text);
- exit(1);
- }
- int main()
- {
- int df, databuf = sizeof(struct stu);
- printf ("databuf = %d \n", databuf);
- nstu data = (nstu) malloc (databuf);
- printf ("please name:\n");
- scanf ("%s", data -> name);
- printf ("please enter number:\n");
- scanf ("%s", data -> number);
- if (access ("tf.dat", W_OK) == -1)
- if (df = creat ("tf.dat", 0644) == -1)
- err("Can't create file!");
- if (df = open ("tf.dat", O_RDWR) == -1)
- err ("Can't open file!");
- if (write(df, data, databuf) != databuf)
- err ("Write error! ");
- if (close(df) != 0)
- err ("Can't close!");
- printf ("ex successful!\n");
- return 0;
- }
复制代码 |
|