Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 逐行散列文本文件_C_Database_Hash - Fatal编程技术网

C 逐行散列文本文件

C 逐行散列文本文件,c,database,hash,C,Database,Hash,我正在尝试散列一个包含19行学生个人信息的文本文件。有一个基于案例的系统,我可以选择要执行的操作,如插入哈希、显示等。代码根本不会读取我提供的文本文件,当我按Display作为操作时,它不会显示任何内容。当我按下Insert选项时,同样的事情也会发生 代码根本无法读取我提供的文本文件 您的问题可能来自: 因为(line[len-1]!='\n')这可能永远不会是真的,因为fgets能够读取一个大的行,并且在这些行只包含很少的数据之后考虑fscanf 为什么要在/*不完整的行*/上工作 在测试的完

我正在尝试散列一个包含19行学生个人信息的文本文件。有一个基于案例的系统,我可以选择要执行的操作,如插入哈希、显示等。代码根本不会读取我提供的文本文件,当我按Display作为操作时,它不会显示任何内容。当我按下Insert选项时,同样的事情也会发生

代码根本无法读取我提供的文本文件

您的问题可能来自:

因为
(line[len-1]!='\n')
这可能永远不会是真的,因为fgets能够读取一个大的行,并且在这些行只包含很少的数据之后考虑fscanf

为什么要在/*不完整的行*/上工作

在测试的完整线路上工作

if (len && (line[len - 1] == '\n')) 
你也有一个意外的回报

因此,您不能阅读多个答案,请在
else{…}

请注意,您的代码假设用户只请求一次插入,如果他多次请求,您将多次添加相同的元素

在你读了一行之后,你又在文件中读了一遍

因此,您只保存了一半行中的数据,您希望这样做

sscanf(line,"%s %s %s %d",node.AM, node.first_name, node.last_name, &node.grade);

你还有其他问题

在散列中,假设标识符至少有7个字符,如果不是这种情况,则使用未定义的行为读取名称(在空字符之后)

做一些类似于:

int Hash(char *AM, int n)
{    
  int i;
  int hashIndex = 0;

  for (i=0; (i< 8) && (AM[i] != 0); i++)
  {
    hashIndex += AM[i];
  }

  return hashIndex % n;
}
您对int但
节点使用格式%d。grade
是一个浮点,用%f替换%d,并检查fscanf返回4

我还鼓励您限制读取字符串的大小,以免冒着从字段中写入的风险,因此(事实上,fscanf必须由sscanf替换)

您使用格式%d表示int,但给出了
char*

将d替换为s或更改AM的类型(当然还有如何在其他地方读取和使用)

insertToHash末尾的返回是无用的,其他地方还有一些无用的返回

您使用格式%d表示整数,但给出了双精度

用%lf或%lg等替换%d

AM未初始化,行为未定义

你想要

int hashIndex = Hash(node.AM, 19);

AM
名字
姓氏
等级
未初始化,行为未定义

你想要

struct node *newnode = createNode(node.AM, node.first_name, node.last_name, node.grade);
并删除无用的变量hashIndex、grade、last\u name、first\u name、AM

在deleteFromHash和searchInHash中执行测试

if (len && (line[len - 1] == '\n')) 
是错误的,因为你比较指针,你想要

if (!strcmp(myNode->AM, AM))

结构和全局变量使用相同的名称,这不是一个好主意

此外,全局变量节点仅在insertToHash中使用,因此您不需要它,请删除全局变量并将局部变量添加到insertToHash中


编辑后,添加主菜单

必须是(2次)

您的变量
first\u name、last\u name
grade
未使用


考虑到我所有的意见,守则是:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>

struct hash *hashTable = NULL;
int eleCount = 0;

struct Node 
{
  float grade;
  char AM[100];
  char first_name[100];
  char last_name[100];
  struct Node *next;
};

struct hash 
{
struct Node *head;
int count;
};

struct Node * createNode(char *AM, char *first_name, char *last_name, float grade) 
{
  struct Node *newNode;

  newNode = (struct Node *) malloc(sizeof(struct Node));
  strcpy(newNode->AM, AM);
  strcpy(newNode->last_name, last_name);
  strcpy(newNode->first_name, first_name);
  newNode->grade = grade;
  newNode->next = NULL;
  return newNode;
}

int Hash(char *AM, int n)
{    
  int i;
  int hashIndex = 0;

  for (i=0; (i< 8) && (AM[i] != 0); i++)
  {
    hashIndex += AM[i];
  }

  return hashIndex % n;
}

void insertToHash() 
{
  struct Node node;

  FILE *fp;
  fp = fopen ("Foitites-Vathmologio-DS.txt","rb");

  if (fp == NULL) 
  { 
    fprintf(stderr,"Could not open file");  
    return;
  } 

  char line[4096];

  while (fgets(line, sizeof line,fp)) {
    size_t len = strlen(line);
    if (len && (line[len - 1] == '\n')) {
      /* complete line */
      if (sscanf(line,"%99s %99s %99s %f",node.AM, node.first_name, node.last_name, &node.grade) != 4) {
    puts("invalid file");
    return;
      }

      int hashIndex = Hash(node.AM, 19);

      struct Node *newNode = createNode(node.AM, node.first_name, node.last_name, node.grade);
      /* head of list for the bucket with index "hashIndex" */

      if (!hashTable[hashIndex].head) 
      {
        hashTable[hashIndex].head = newNode;
        hashTable[hashIndex].count = 1;
      }
      else {
        /* adding new Node to the list */
        newNode->next = (hashTable[hashIndex].head);
        /*
           * update the head of the list and no of
           * Nodes in the current bucket
        */
        hashTable[hashIndex].head = newNode;
        hashTable[hashIndex].count++;
      }
    }
  }
  fclose(fp);
  printf("Done! \n");
}

void deleteFromHash(char *AM) 
{
  /* find the bucket using hash index */
  int hashIndex = Hash(AM, 19);
  int flag = 0;

  struct Node *temp, *myNode;
  /* get the list head from current bucket */

  myNode = hashTable[hashIndex].head;

  if (!myNode) {
    printf("Given data is not present in hash Table!!\n");

    return;
  }

  temp = myNode;
  while (myNode != NULL) {
    /* delete the Node with given AM */
    if (!strcmp(myNode->AM, AM)) {
      flag = 1;
      if (myNode == hashTable[hashIndex].head)
        hashTable[hashIndex].head = myNode->next;
      else
        temp->next = myNode->next;

      hashTable[hashIndex].count--;
      free(myNode);
      break;
    }

    temp = myNode;
    myNode = myNode->next;
  }
  if (flag)
    printf("Data deleted successfully from Hash Table\n");
  else
    printf("Given data is not present in hash Table!!!!\n");
}

void searchInHash(char *AM) {
  int hashIndex = Hash(AM, 19);
  int flag = 0;
  struct Node *myNode;        myNode = hashTable[hashIndex].head;
  if (!myNode) {
    printf("Search element unavailable in hash table\n");
    return;
  }
  while (myNode != NULL) {
    if (!strcmp(myNode->AM, AM)) {
      printf("Student ID  : %s\n", myNode->AM);
      printf("First Name     : %s\n", myNode->first_name);
      printf("Last Name     : %s\n", myNode->last_name);
      printf("grade      : %lg\n", myNode->grade);
      flag = 1;
      break;
    }
    myNode = myNode->next;
  }
  if (!flag)
    printf("Search element unavailable in hash table\n");
}

void display() {
  struct Node *myNode;
  int i;
  for (i = 0; i < eleCount; i++) {
    if (hashTable[i].count == 0)
      continue;
    myNode = hashTable[i].head;
    if (!myNode)
      continue;
    printf("\nData at index %d in Hash Table:\n", i);
    printf("Student ID    First Name    Last Name      Grade   \n");
    printf("--------------------------------\n");
    while (myNode != NULL) {
      printf("%-12s", myNode->AM);
      printf("%-15s", myNode->first_name);
      printf("%-15s", myNode->last_name);
      printf("%lg\n", myNode->grade);
      myNode = myNode->next;
    }
  }
}

int main() 
{
  int n=19, ch;
  char AM[100];
  int insertDone = 0;

  eleCount = n;
  /* create hash table with "n" no of buckets */
  hashTable = (struct hash *) calloc(n, sizeof(struct hash));
  while (1) {
    printf("\n1. Insertion\t2. Deletion\n");
    printf("3. Searching\t4. Display\n5. Exit\n");
    printf("Enter your choice:");
    scanf("%d", &ch);


    switch (ch) {
    case 1: 
      if (insertDone)
        puts("Inserton was already done");
      else {
    /*inserting new Node to hash table */
        insertToHash();
        insertDone = 1;
      }
      break;

    case 2: 
      printf("Enter the AM to perform deletion:");
      scanf("%99s", AM);
      /* delete Node with "AM" from hash table */
      deleteFromHash(AM);
      break;

    case 3: 
      printf("Enter the AM to search:");
      scanf("%99s", AM);
      searchInHash(AM);
      break;
    case 4: 
      display();
      break;
    case 5: 
      exit(0);
    default: 
      printf("U have entered wrong option!!\n");
      break;
    }
  }
  return 0;
}

您正在对未初始化的指针调用
inthashindex=Hash(AM,19)
您是否打算调用
inthashindex=Hash(node.AM,19)
?@kiran Biradar我根据这篇文章编写了我的代码,因此我认为这可以回答您的问题,请阅读。最好提供一个简短、完整、可编译的示例,说明您遇到的具体问题。在这种情况下,您提供了太多格式不好的代码(自那以后一直在编辑),但它不包含
intmain(void)
intmain(intargc,char*argv[])
函数,因此它不会编译。请编辑您的帖子,包括您是如何编写主函数的。对于我来说,
(第[len-1]行!='\n')
必须是
(第[len-1]='\n')
,您还有几个其他问题,请参阅我的回答。我想我们在这方面取得了一些进展!在我更改主功能之前,特别是在scaf(“%d”、&AM)中,当我选择显示框时,它将仅显示我文件的第一行,但在您建议的更改之后,我将返回ld1 exitstatus@vaskar我将继续更正您的代码,当前显示器将生成我在文件中输入的条目,我已在mu代码中编辑了这些条目,以及您提供给allong的内容和形式的更正我的.txt文件,现在,当我选择“显示”时,它会同时显示两个或三个学生,就像他们在同一个位置一样node@vaskar我将继续编辑我的答案以更正您的代码,请等待我完成,我将警告您,并将完整更正的代码加上一个,以供您的广泛努力。
printf("Student ID  : %d\n", myNode->AM);
printf("%-12d", myNode->AM);
printf("grade      : %d\n", myNode->grade);
printf("%d\n", myNode->grade);
int hashIndex = Hash(AM, 19);
int hashIndex = Hash(node.AM, 19);
struct node *newnode = createNode(AM, first_name, last_name, grade);
struct node *newnode = createNode(node.AM, node.first_name, node.last_name, node.grade);
 if (myNode->AM == AM)
if (!strcmp(myNode->AM, AM))
struct node 
{
  float grade;
  char AM[100];
  char first_name[100];
  char last_name[100];
  struct node *next;
}node;
scanf("%d", &AM);
scanf("%99s", AM);
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>

struct hash *hashTable = NULL;
int eleCount = 0;

struct Node 
{
  float grade;
  char AM[100];
  char first_name[100];
  char last_name[100];
  struct Node *next;
};

struct hash 
{
struct Node *head;
int count;
};

struct Node * createNode(char *AM, char *first_name, char *last_name, float grade) 
{
  struct Node *newNode;

  newNode = (struct Node *) malloc(sizeof(struct Node));
  strcpy(newNode->AM, AM);
  strcpy(newNode->last_name, last_name);
  strcpy(newNode->first_name, first_name);
  newNode->grade = grade;
  newNode->next = NULL;
  return newNode;
}

int Hash(char *AM, int n)
{    
  int i;
  int hashIndex = 0;

  for (i=0; (i< 8) && (AM[i] != 0); i++)
  {
    hashIndex += AM[i];
  }

  return hashIndex % n;
}

void insertToHash() 
{
  struct Node node;

  FILE *fp;
  fp = fopen ("Foitites-Vathmologio-DS.txt","rb");

  if (fp == NULL) 
  { 
    fprintf(stderr,"Could not open file");  
    return;
  } 

  char line[4096];

  while (fgets(line, sizeof line,fp)) {
    size_t len = strlen(line);
    if (len && (line[len - 1] == '\n')) {
      /* complete line */
      if (sscanf(line,"%99s %99s %99s %f",node.AM, node.first_name, node.last_name, &node.grade) != 4) {
    puts("invalid file");
    return;
      }

      int hashIndex = Hash(node.AM, 19);

      struct Node *newNode = createNode(node.AM, node.first_name, node.last_name, node.grade);
      /* head of list for the bucket with index "hashIndex" */

      if (!hashTable[hashIndex].head) 
      {
        hashTable[hashIndex].head = newNode;
        hashTable[hashIndex].count = 1;
      }
      else {
        /* adding new Node to the list */
        newNode->next = (hashTable[hashIndex].head);
        /*
           * update the head of the list and no of
           * Nodes in the current bucket
        */
        hashTable[hashIndex].head = newNode;
        hashTable[hashIndex].count++;
      }
    }
  }
  fclose(fp);
  printf("Done! \n");
}

void deleteFromHash(char *AM) 
{
  /* find the bucket using hash index */
  int hashIndex = Hash(AM, 19);
  int flag = 0;

  struct Node *temp, *myNode;
  /* get the list head from current bucket */

  myNode = hashTable[hashIndex].head;

  if (!myNode) {
    printf("Given data is not present in hash Table!!\n");

    return;
  }

  temp = myNode;
  while (myNode != NULL) {
    /* delete the Node with given AM */
    if (!strcmp(myNode->AM, AM)) {
      flag = 1;
      if (myNode == hashTable[hashIndex].head)
        hashTable[hashIndex].head = myNode->next;
      else
        temp->next = myNode->next;

      hashTable[hashIndex].count--;
      free(myNode);
      break;
    }

    temp = myNode;
    myNode = myNode->next;
  }
  if (flag)
    printf("Data deleted successfully from Hash Table\n");
  else
    printf("Given data is not present in hash Table!!!!\n");
}

void searchInHash(char *AM) {
  int hashIndex = Hash(AM, 19);
  int flag = 0;
  struct Node *myNode;        myNode = hashTable[hashIndex].head;
  if (!myNode) {
    printf("Search element unavailable in hash table\n");
    return;
  }
  while (myNode != NULL) {
    if (!strcmp(myNode->AM, AM)) {
      printf("Student ID  : %s\n", myNode->AM);
      printf("First Name     : %s\n", myNode->first_name);
      printf("Last Name     : %s\n", myNode->last_name);
      printf("grade      : %lg\n", myNode->grade);
      flag = 1;
      break;
    }
    myNode = myNode->next;
  }
  if (!flag)
    printf("Search element unavailable in hash table\n");
}

void display() {
  struct Node *myNode;
  int i;
  for (i = 0; i < eleCount; i++) {
    if (hashTable[i].count == 0)
      continue;
    myNode = hashTable[i].head;
    if (!myNode)
      continue;
    printf("\nData at index %d in Hash Table:\n", i);
    printf("Student ID    First Name    Last Name      Grade   \n");
    printf("--------------------------------\n");
    while (myNode != NULL) {
      printf("%-12s", myNode->AM);
      printf("%-15s", myNode->first_name);
      printf("%-15s", myNode->last_name);
      printf("%lg\n", myNode->grade);
      myNode = myNode->next;
    }
  }
}

int main() 
{
  int n=19, ch;
  char AM[100];
  int insertDone = 0;

  eleCount = n;
  /* create hash table with "n" no of buckets */
  hashTable = (struct hash *) calloc(n, sizeof(struct hash));
  while (1) {
    printf("\n1. Insertion\t2. Deletion\n");
    printf("3. Searching\t4. Display\n5. Exit\n");
    printf("Enter your choice:");
    scanf("%d", &ch);


    switch (ch) {
    case 1: 
      if (insertDone)
        puts("Inserton was already done");
      else {
    /*inserting new Node to hash table */
        insertToHash();
        insertDone = 1;
      }
      break;

    case 2: 
      printf("Enter the AM to perform deletion:");
      scanf("%99s", AM);
      /* delete Node with "AM" from hash table */
      deleteFromHash(AM);
      break;

    case 3: 
      printf("Enter the AM to search:");
      scanf("%99s", AM);
      searchInHash(AM);
      break;
    case 4: 
      display();
      break;
    case 5: 
      exit(0);
    default: 
      printf("U have entered wrong option!!\n");
      break;
    }
  }
  return 0;
}
pi@raspberrypi:/tmp $ gcc -g -pedantic -Wextra -Wall c.c
pi@raspberrypi:/tmp $ cat Foitites-Vathmologio-DS.txt
123 aze qsd 1.23
456 iop jkl 4.56
pi@raspberrypi:/tmp $ ./a.out

1. Insertion    2. Deletion
3. Searching    4. Display
5. Exit
Enter your choice:4

1. Insertion    2. Deletion
3. Searching    4. Display
5. Exit
Enter your choice:1
Done! 

1. Insertion    2. Deletion
3. Searching    4. Display
5. Exit
Enter your choice:4

Data at index 7 in Hash Table:
Student ID    First Name    Last Name      Grade   
--------------------------------
456         iop            jkl            4.56

Data at index 17 in Hash Table:
Student ID    First Name    Last Name      Grade   
--------------------------------
123         aze            qsd            1.23

1. Insertion    2. Deletion
3. Searching    4. Display
5. Exit
Enter your choice:1
Inserton was already done

1. Insertion    2. Deletion
3. Searching    4. Display
5. Exit
Enter your choice:3
Enter the AM to search:123
Student ID  : 123
First Name     : aze
Last Name     : qsd
grade      : 1.23

1. Insertion    2. Deletion
3. Searching    4. Display
5. Exit
Enter your choice:3
Enter the AM to search:1234
Search element unavailable in hash table

1. Insertion    2. Deletion
3. Searching    4. Display
5. Exit
Enter your choice:2
Enter the AM to perform deletion:1
Given data is not present in hash Table!!

1. Insertion    2. Deletion
3. Searching    4. Display
5. Exit
Enter your choice:2
Enter the AM to perform deletion:123
Data deleted successfully from Hash Table

1. Insertion    2. Deletion
3. Searching    4. Display
5. Exit
Enter your choice:4

Data at index 7 in Hash Table:
Student ID    First Name    Last Name      Grade   
--------------------------------
456         iop            jkl            4.56

1. Insertion    2. Deletion
3. Searching    4. Display
5. Exit
Enter your choice:2
Enter the AM to perform deletion:456
Data deleted successfully from Hash Table

1. Insertion    2. Deletion
3. Searching    4. Display
5. Exit
Enter your choice:4

1. Insertion    2. Deletion
3. Searching    4. Display
5. Exit
Enter your choice:5
pi@raspberrypi:/tmp $