【发布时间】:2018-10-22 10:56:53
【问题描述】:
我目前正在开发一个模拟堆栈(使用链表)的 c 库。
堆栈必须能够管理任何数据类型。
它还必须能够将其内容写入文件然后检索它们。这是我遇到问题的地方。当我将原始堆栈与我存储在文件中的堆栈进行比较时,数据并不相同。
代码如下:
writeRead.c
#include <stdio.h>
#include "stack.h"
#define NODES 3
struct my_data {
int val;
char name[60];
};
int main() {
struct my_stack *s1, *fs1;
struct my_data *data, *data1, *data2;
// Initialize Stack
s1 = my_stack_init(sizeof(struct my_data));
// Initialize data and push to stack
for (int i = 0; i < NODES; i++) {
data = malloc(sizeof(struct my_data)); // We must allocate static memory
data->val = i;
sprintf(data->name, "Value %d", i);
if (my_stack_push(s1, data)) {
puts("Error in my_stack_push()");
exit(1);
}
printf("New node in s1: (%d, %s)\n", data->val, data->name);
}
// Write Stack to file
if (my_stack_write(s1, "/tmp/my_stack.data") == -1) {
puts("Error in my_stack_write (s1)");
exit(1);
}
// Read Stack from file
fs1 = my_stack_read("/tmp/my_stack.data");
if (!fs1) {
puts("Error in my_stack_read (fs1)");
exit(1);
}
// Compare data of stack s1 (memory) and fs1 (file)
while ((data1 = my_stack_pop(s1))) {
printf("FS1 len. %d\n",my_stack_len(fs1));
data2 = my_stack_pop(fs1);
printf("Node of s1: (%d, %s)\t", data1->val, data1->name);
printf("Node of fs1: (%d, %s)\n", data2->val, data2->name);
if (!data2 || data1->val != data2->val || my_strcmp(data1->name, data2->name)) {
printf("Data in s1 and fs1 are not the same.\n (data1->val: %d <> data2->val: %d) o (data1->name: %s <> data2->name: "
"%s)\n",
data1->val, data2->val, data1->name, data2->name);
exit(1);
}
}
return 0;
}
stack.h
#include <fcntl.h> /* Modos de apertura de función open()*/
#include <stdlib.h> /* Funciones malloc(), free(), y valor NULL */
#include <sys/stat.h> /* Permisos función open() */
#include <sys/types.h> /* Definiciones de tipos de datos como size_t*/
#include <unistd.h> /* Funciones read(), write(), close()*/
struct my_stack_node {
void *data;
struct my_stack_node *next;
};
struct my_stack {
int size;
struct my_stack_node *first;
};
int my_strcmp(const char *str1, const char *str2);
struct my_stack *my_stack_init(int size);
int my_stack_push(struct my_stack *stack, void *data);
void *my_stack_pop(struct my_stack *stack);
int my_stack_len(struct my_stack *stack);
struct my_stack *my_stack_read(char *filename);
int my_stack_write(struct my_stack *stack, char *filename);
int my_stack_purge(struct my_stack *stack);
stack.c
#include <stdio.h>
#include "stack.h"
int my_strcmp(const char *str1, const char *str2) {
int restultatCmp = *str1++ - *str2++;
//printf("ResultatCmp : %d \n", restultatCmp);
while(*str1++ && *str2++ && restultatCmp == 0) {
restultatCmp = *str1 - *str2;
}
return restultatCmp;
}
struct my_stack *my_stack_init(int dataSize) {
struct my_stack *stack = malloc(sizeof(struct my_stack));
stack -> first = NULL;
stack -> size = dataSize;
return stack;
}
int my_stack_push(struct my_stack *stack, void *dataIn) {
struct my_stack_node *nodeToPush;
nodeToPush = malloc(sizeof(struct my_stack_node));
if(stack == NULL && sizeof(dataIn)> 0){
printf("Null Stack or data size error.\n");
//la pila debe existir
return -1;
}
else {
nodeToPush -> data = dataIn;
if(stack -> first == NULL) {
nodeToPush -> next = NULL;
stack -> first = nodeToPush;
}
else {
nodeToPush -> next = stack -> first;
stack -> first = nodeToPush;
}
}
return 0;
}
void *my_stack_pop(struct my_stack *stack) {
if(stack -> first == NULL) {
return NULL;
}
struct my_stack_node *nodeToDelete = stack -> first;
void *data = nodeToDelete -> data;
stack -> first = nodeToDelete -> next;
free(nodeToDelete);
return data;
}
int my_stack_len(struct my_stack *stack) {
int numNodes = 0;
struct my_stack_node *currentElement = stack -> first;
while(currentElement != NULL) {
numNodes++;
currentElement = currentElement ->next;
}
return numNodes;
}
void recursiveWrite(struct my_stack_node *nodo, int fileDesc, int sizeData) {
if(nodo ->next != NULL) recursiveWrite(nodo -> next,fileDesc,sizeData);
if(write(fileDesc, nodo -> data, sizeData)== -1){
printf("Error de escritura\n");
return;// error escritura.
}
}
int my_stack_write(struct my_stack *stack, char *filename) {
struct my_stack_node *currentNode = stack -> first;
int fileDesc = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
if(fileDesc == -1) {
return -1; // Error open();
}
if(write(fileDesc, &stack -> size, sizeof(stack -> size)) == -1){
return -1; // Error write();
}
int sizeData = stack -> size;
recursiveWrite(currentNode,fileDesc,sizeData);
close(fileDesc);
return my_stack_len(stack);
}
struct my_stack *my_stack_read(char *filename) {
int fileDesc = open(filename, O_RDONLY, S_IRUSR);
if(fileDesc == -1) {
return NULL; // Error open();
}
char *buffer = malloc(sizeof(int));
int readBytes;
if((readBytes = read(fileDesc, buffer, sizeof(int))) == -1){
printf("Error reading data size.\n");
return NULL;
}
int dataSize = 0;
dataSize = (int) *buffer; // parse data Size from buffer.
struct my_stack *stack;
stack = malloc(sizeof(struct my_stack));
stack = my_stack_init(dataSize); // initialize Stack
buffer = realloc(buffer, stack -> size);
if(buffer == NULL){
return NULL;
}
else{
while(read(fileDesc, buffer, stack -> size) > 0) {
if((my_stack_push(stack,buffer))== -1){
printf("Error en my_stack_read: Push error.\n");
return NULL;
}
}
close(fileDesc);
return stack;
}
}
对不起,我试图尽可能简化示例。
输出:
New node in s1: (0, Value 0)
New node in s1: (1, Value 1)
New node in s1: (2, Value 2)
FS1 len. 3
Node of s1: (2, Value 2) Node of fs1: (2, Value 2)
FS1 len. 2
Node of s1: (1, Value 1) Node of fs1: (2, Value 2)
Data in s1 and fs1 are not the same.
(data1->val: 1 <> data2->val: 2) o (data1->name: Value 1 <> data2->name: Value 2)
预期输出:
New node in s1: (0, Value 0)
New node in s1: (1, Value 1)
New node in s1: (2, Value 2)
FS1 len. 3
Node of s1: (2, Value 2) Node of fs1: (2, Value 2)
FS1 len. 2
Node of s1: (1, Value 1) Node of fs1: (1, Value 1)
FS1 len. 1
Node of s1: (0, Value 0) Node of fs1: (0, Value 0)
我几乎可以确定文件写入是正确的。用十六进制编辑器检查,应该是正确的。
所以我的猜测是我的错在于读取文件。
忘了提到项目的一个限制是系统调用 I/O 是强制性的。
非常感谢任何帮助。
提前致谢。
编辑1:
按照@AndrewHenle 的建议更改了 *my_stack_read(),但输出仍然不是预期的。
struct my_stack *my_stack_read(char *filename) {
int fileDesc = open(filename, O_RDONLY, S_IRUSR);
if(fileDesc == -1) {
return NULL; // Error open();
}
char *buffer = malloc(sizeof(int));
ssize_t readBytes;
readBytes = read(fileDesc, buffer, sizeof(int));
if(readBytes == -1) {
return NULL;
}
int dataSize = 0;
dataSize = (int) *buffer; // parse data Size from buffer.
struct my_stack *stack;
stack = malloc(sizeof(struct my_stack));
stack = my_stack_init(dataSize); // initialize Stack
buffer = realloc(buffer, stack -> size);
if(buffer == NULL){
return NULL;
}
else{
while(read(fileDesc, buffer, stack -> size) > 0) {
int push = my_stack_push(stack,buffer);
if(push == -1){
printf("Error en my_stack_read: Push error.\n");
return NULL;
}
}
close(fileDesc);
return stack;
}
【问题讨论】:
-
首先,
read()和write()返回ssize_t而不是int。其次,IMOif((readBytes = read(fileDesc, buffer, sizeof(int))) == -1)...是一种绝对可怕的函数调用方式,尤其是对于像read()和write()这样可以返回部分成功的函数。不要试图在无法正确处理函数调用结果的代码行中填充太多内容。使用较少的行数不会获得任何奖励积分 - 事实上,当您过度使用此类填充时,您会使您的代码更难理解。 -
感谢您的快速答复!我将尝试根据您的建议更改代码@AndrewHenle。请注意,这是出于学习目的,我对某些概念还很陌生。非常感谢!
-
@melpomene 感谢您的帖子。我想我会尝试做它所说的几乎所有事情。我在这个项目中做了很多工作,我尝试使用 gdb 和 valgrind 进行调试(尽管最后一个仍然很困难)。我也尝试了不同的测试。我的帖子是一个更大的帖子的简化。我还使用 Ctutor 等在线工具来可视化代码执行。我不想通过指出所有这些来扩大帖子。我知道我的问题可能看起来微不足道,但我没有在没有进行研究或尝试过我手中的所有东西的情况下发布所有这些内容。不过,我很欣赏你的意图。谢谢。
标签: c io linked-list system-calls