1

我正在尝试使用学生结构在文件处理中编写 ac 程序以添加信息,但是如果我尝试将记录输入第一个位置时在 switch 中的情况 4,它不会写入给定的详细信息。但是输出对于休息是正确的的职位

#include<stdio.h>

struct stu
{
  char fn[10];
  char ln[10];
  int id;
} student[10];

int main()
{
  FILE *p1, *p2;
  int n, idno, i, pos, choice;
  char ch;

  printf("\t\tSTUDENT DATA\n\n");
  p1 = fopen("STUDENT.txt", "w");
  printf("Student file opened\n");
  printf("How many students? ");
  scanf("%d", &n);
  fflush(stdin);
  printf("\nEnter data\n");
  printf("ID    First Name    Last Name\n\n");
  for (i = 0; i < n; i++)
  {

    fscanf(stdin, "%d    %s    %s", &student[i].id, student[i].fn,
        student[i].ln);
    fflush(stdin);
    fprintf(p1, "%d    %s    %s", student[i].id, student[i].fn, student[i].ln);

  }
  fclose(p1);
  printf("\nData written successfully!!\n");
  printf("File closed!!\n");

  do
  {

    printf(
        "\nPlease enter one of the folowing choices for the required operation:\n");
    printf("1.Reading information\n");
    printf("2.Searching information\n");
    printf("3.Copying information into new file\n");
    printf("4.Adding information\n");
    printf("5.Deleting information\n");
    scanf("%d", &choice);
    fflush(stdin);

    switch (choice)
    {

    case 1:
      printf("\nFile opening for reading....\n");
      p1 = fopen("STUDENT.txt", "r");

      for (i = 0; i < n; i++)
      {
        fscanf(p1, "%d    %s    %s", &student[i].id, student[i].fn,
            student[i].ln);
        fflush(stdin);
        fprintf(stdout, "%d %s %s", student[i].id, student[i].fn,
            student[i].ln);
        printf("\n");
      }
      fclose(p1);
      printf("File closed!!\n");

      break;

    case 2:
      printf("\nEnter id no. to be searched for.... ");
      scanf("%d", &idno);
      fflush(stdin);
      p1 = fopen("STUDENT.txt", "r");
      i = 0;
      while (i < n)
      {
        fscanf(p1, "%d    %s    %s", &student[i].id, student[i].fn,
            student[i].ln);
        fflush(stdin);
        if (student[i].id == idno)
        {
          fprintf(stdout, "%d  %s %s", student[i].id, student[i].fn,
              student[i].ln);
          break;
        }
        else
          i++;
      }

      if (student[i].id != idno)
        printf("ID not found!!\n");
      fclose(p1);

      break;

    case 3:
      printf("\n\nOpening file STUDENTC for the contents to be copied.....\n");
      p1 = fopen("STUDENT.txt", "r");
      p2 = fopen("STUDENTC.txt", "w");
      printf("Copying contents.....\n");
      for (i = 0; i < n; i++)
      {
        fscanf(p1, "%d    %s    %s", &student[i].id, student[i].fn,
            student[i].ln);
        fflush(stdin);
        fprintf(p2, "%d    %s    %s", student[i].id, student[i].fn,
            student[i].ln);
      }
      fclose(p1);
      fclose(p2);
      printf("Contents copied successfully!!\n");
      printf("\nReading from newly created file.....\n");
      p2 = fopen("STUDENT.txt", "r");
      for (i = 0; i < n; i++)
      {
        fscanf(p2, "%d %s %s", &student[i].id, student[i].fn, student[i].ln);
        fflush(stdin);
        fprintf(stdout, "%d %s %s", student[i].id, student[i].fn,
            student[i].ln);
        printf("\n");
      }
      fclose(p2);
      printf("\nAll operations successfully completed....:)");
      printf("\nClosing and saving all opened files.....\n");

      break;

    case 4:
      printf("At which position do you waht to insert the record?:\n");
      for (i = 0; i < n; i++)
      {
        fscanf(p1, "%d    %s    %s", &student[i].id, student[i].fn,
            student[i].ln);
        fflush(stdin);
        fprintf(stdout, "%d  %s  %s", student[i].id, student[i].fn,
            student[i].ln);
        printf("\n");
      }
      scanf("%d", &pos);
      fflush(stdin);

      p1 = fopen("STUDENT.txt", "a+");

      for (i = n - 1; i >= pos - 1; i--)
      {
        student[i + 1] = student[i];
      }
      i++;
      printf("\nEnter additional details:\n");
      printf("ID    First Name    Last Name\n\n");

      fscanf(stdin, "%d % s%s", &student[i].id, student[i].fn, student[i].ln);
      fflush(stdin);
      fprintf(p1, "%d    %s    %s", student[i].id, student[i].fn,
          student[i].ln);

      n++;
      fclose(p1);

      break;

    case 5:
      printf("\nEnter id no. to be deleted: ");
      scanf("%d", &idno);
      fflush(stdin);

      for (i = 0; student[i].id != idno; i++)
      {
        if (i == n)
        {
          printf("Record not found!! :(\n");
          break;
        }
      }

      if (student[i].id == idno)
      {

        while (i != n)
        {
          student[i] = student[i + 1];
          i++;
        }
        n--;
        printf("Deleted successfully!! :)\n");
      }

      break;

    default:
      printf("Wrong choice entered!!!\n");
      break;
    }

    printf("\nWant to run more operations?: ");
    scanf("%c", &ch);
    fflush(stdin);

  } while (ch == 'y' || ch == 'Y');

  return 0;
}
4

2 回答 2

1

您在第 145 行有一个错字:

fscanf(stdin,"%d % s%s",&student[i].id,student[i].fn,student[i].ln);

应该:

fscanf(stdin,"%d %s %s",&student[i].id,student[i].fn,student[i].ln);

我通过用额外的警告编译它来发现它:

$ gcc -Wall -Wextra so.c 
so.c: In function ‘main’:
so.c:145:4: warning: unknown conversion type character 0x20 in format [-Wformat]
so.c:145:4: warning: too many arguments for format [-Wformat-extra-args]
于 2013-08-31T09:23:59.340 回答
0

首先,我认为student[i+1]=student[i];行不通,你需要

memcpy(&student[i+1], &student[i], sizeof(student[i]);

其次,您不能将insert信息写入文本文件,您需要将整个信息写入临时文件(例如STUDENT.tmp),删除旧文件STUDENT.txt并重命名STUDENT.tmpSTUDENT.txt

于 2013-08-31T09:26:29.943 回答