结构数组中的垃圾输出

时间:2014-01-25 18:49:59

标签: c

这是我在sortstudents()函数中的项目我尝试从文件中读取数据在我尝试打印数组时有争议的一行或一行我得到了垃圾数据

这是关于数据在文件中的外观(2条记录)的示例:
1 mohamed talaat€@ A
2艾哈迈德·穆罕默德@A

#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
#include <windows.h>
///////////////////////////////////////////////////////////////////////////
struct student
 {
  char  id[5];
  char  name[30];
  int   term;
  float gpa;
  char  grade;
 };

struct student stu;

typedef struct student stud;


//////////////////////////////////////////////////////////////////////////
//set the cordinate to 0, 0 (top-left corner of window)
//<windows.h> is needed
COORD coord = {0,0};
//////////////////////////////////////////////////////////////////////////
//need cordinate struct to use it
//gotoxy to set coordinate x,y
void gotoxy(int x, int y)
{
//X and Y coordinates 
  coord.X = x; coord.Y = y;
// ew3a tensa Microsoft 
  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
//////////////////////////////////////////////////////////////////////////
//print regtangle shape Ascii table www.asciitable.com
// window width = 80 character window hight = 25 character
void drawRectangle()
{
 int i, j;
//print the corner rear top left 
 gotoxy(0,0);    
 printf("%c",201);
// print 78 line shape = starts from rear top left ended at the top rear right     
 for(i = 1; i < 78; i++)
  {
   gotoxy(i, 0);
   printf("%c",205);
  }
//print the corner rear top right
 gotoxy(78,0);    
 printf("%c",187);
//print the corner rear right side with width = 25
 for(i = 1; i < 25; i++)
  {
   gotoxy(78, i);
//print T-shape at width 6 and after 6 proceed until 25 printing right side 
   if(i == 6)
    {
     printf("%c",185);
    }
   else
    {
     printf("%c",186);
    }
  }
//print the corner rear bottom right
 gotoxy(78, 25);
 printf("%c",188);
// -i- already = 78
// print bottom side pf the regtangle
for(i = 77; i > 0; i--)
 {
  gotoxy(i,25);
// print T-shape at width 35 and after that proceed until 78 printing rgt base side 
 if(i == 35)
  {
   printf("%c",202);
  }
 else
  {
   printf("%c",205);
  }
 }
//print the corner rear bottom left
  gotoxy(0,25);
  printf("%c",200);
// print T-shape at width 6 and after 6 proceed until 25 printing left side     
 for(i = 24; i > 0; i--)
  {
   gotoxy(0,i);
   if(i == 6)
    {
     printf("%c",204);
    }
   else
    {
     printf("%c",186);
    }
   }
// print T-shape at width 36 and connect left side to right side
 for(i = 1; i < 78; i++)
  {
   gotoxy(i,6);
   if(i == 35)
    {
     printf("%c",203);
    }
    else
    {
     printf("%c",205);
    }
  }
// connect middle T-shape at the middle of the regtangle to the base
 for(i = 7; i < 25; i++)
  {
   gotoxy(35,i);
   printf("%c",186);
  }
}
//////////////////////////////////////////////////////////////////////////
// Build Program window interface using functions --drawRectangle 
// with color 1 = Blue & Font color 7 = White
void swindow()
{
 int i;
 drawRectangle();
 gotoxy(28,1);
 system("color 17");
 printf("STUDENT GRADE SYSTEM");
 gotoxy(28,2);
 for(i=1;i<21;i++)
 {
  printf("%c",205);
 }
 gotoxy(15,3);
 printf("College of Computing and Information Technology");
 gotoxy(10,4);
 printf(" ");
 gotoxy(10,5);
 printf("Arab Academy for Science, Technology & Maritime Transport");
 gotoxy(25,24);
}
//////////////////////////////////////////////////////////////////////////
void print_heading(const char st[])
{
 gotoxy(50,8);
 printf("%s",st);
}
//////////////////////////////////////////////////////////////////////////
void clearWindow()
{
    int i,j;
    for(i = 37; i < 78; i++)
     {
        for(j = 7; j < 25; j++)
        {
           gotoxy(i,j);
           printf(" ");
        }
    }
}
//////////////////////////////////////////////////////////////////////////



//////////////////////////////////////////////////////////////////////////
void add()
{
    clearWindow();
    print_heading("Add Record");
    int print = 37;
    char ans;
    int i;
    FILE *fp;
    fp = fopen("record.txt","ab+");
    if(fp == NULL)
    {
     MessageBox(0,"Error in Opening file\nMake sure your file is not write protected","Warning",0);
    }
    else
    {
     fflush(stdin);
//here i can Add  Records ... 
//////////////////////////////////////////////////////////////////////////////// 
     gotoxy(print,10);printf("ID: ");gets(stu.id);
     gotoxy(print,12);printf("Name: ");gets(stu.name);
     gotoxy(print,14);printf("Term: ");scanf("%d",&stu.term);
     gotoxy(print,16);printf("Score % : ");scanf("%f",&stu.gpa);

     if (stu.gpa>=3.40)
      {
       stu.grade='A';
      }
     else {
           if (stu.gpa>=2.80)
            {stu.grade='B';}
           else
           {
            if (stu.gpa>=2.20)
             {stu.grade='C';}
            else
            {
             if (stu.gpa>=2.00)
              {stu.grade='D';}
             else
             {stu.grade='F';}
             }
           }
          }
     gotoxy(print,18);printf("GPA: %c",stu.grade);printf("\n");

     gotoxy(print,20);printf("Press(Y) to Save (N) for Cancel... ");//scanf("%c",&ans);
     ans = getche();   

     if (ans=='y' || ans=='Y')
     {
      fwrite(&stu, sizeof(stu), 1, fp);
      gotoxy(40,22); printf("The record is sucessfully added");
     }
     else 
     {
      gotoxy(40,22); printf("Entry process cancelled");
     }
    }
    fclose(fp);
}
//////////////////////////////////////////////////////////////////////////
void search(){
    clearWindow();
    print_heading("Search Record");

    char s_id[5];

    int isFound = 0;

    gotoxy(37,10);

    printf("Enter ID to Search: ");

    fflush(stdin);

    gets(s_id);
 //Read the record file from File
    FILE *fp;
    fp = fopen("record.txt","ab+");
    while(fread(&stu,sizeof(stu),1,fp) == 1)
    {
        if(strcmp(s_id,stu.id) == 0)
        {
         isFound = 1;
         break;
        }
    }
    if(isFound == 1){
        gotoxy(37,12);printf("The record is Found");
        gotoxy(37,13);printf("--------------------");
        gotoxy(37,14);printf("ID: %s",stu.id);
        gotoxy(37,16);printf("Name: %s",stu.name);
        gotoxy(37,18);printf("Term: %d",stu.term);
        gotoxy(37,20);printf("Score %: %0.1f",stu.gpa);
        gotoxy(37,22);printf("GPA: %c",stu.grade);
    }else
    {
     gotoxy(37,12);printf("Sory, No record found in the database");
    }
    fclose(fp);
}
//////////////////////////////////////////////////////////////////////////
void sortstudents()
{
 clearWindow(); 
 FILE *fp;
    fp = fopen("record.txt","r");

//////// detect number of characters ////////
char nextChar = getc(fp);
int numCharacters = 0;

while (nextChar != EOF)
{
    //Do something else, like collect statistics
    numCharacters++;
    nextChar = getc(fp);
}
//////// detect number of characters ////////

int chunck = numCharacters/sizeof(stu);

//stud *arr = (stud *)malloc(chunck);
stud starray[25];

int d;

int numStudents = 0;

     while( fscanf(fp,"%s%s%d%f%c",stu.id,stu.name,&stu.term,&stu.gpa,&stu.grade) > 0)
    {
          starray[numStudents++] = stu;
    }

  gotoxy(37,12);printf("The record is Found");
  gotoxy(37,13);printf("--------------------");
  gotoxy(37,14);printf("ID: %s",starray[0].id);
  gotoxy(37,16);printf("Name: %s",starray[0].name);
  gotoxy(37,18);printf("Term: %d",starray[0].term);
  gotoxy(37,20);printf("Score %: %0.1f",starray[0].gpa);
  gotoxy(37,22);printf("GPA: %c",starray[0].grade);


     /*"ID: %s",stu.id);
     "Name: %s",stu.name);
     "Term: %d",stu.term);
     Score %: %0.1f",stu.gpa);
     "GPA: %c",stu.grade);*/            

fclose(fp);

}
//////////////////////////////////////////////////////////////////////////
void menu(){
    int choice;
    int x = 2;
    while(1)
    {
     gotoxy(x,10);
     printf("1. Add Student");
     gotoxy(x,12);
     printf("2. Search Student");
     gotoxy(x,14);
     printf("3. Statistics");
     gotoxy(x,16);
     printf("4. Close");
     gotoxy(x,20);
     printf("Please enter your choice :");
     scanf("%d",&choice);

    switch(choice)
     {
      case 1:
       add();
      break;

      case 2:
       search();
      break;

      case 3:
       sortstudents();
      break;

      case 4:
       exit(0);
      break;

      default:
       break;
        }
    }
}
//////////////////////////////////////////////////////////////////////////
int main()
{
// draw entry window
//drawRectangle();
swindow();
menu();
//clearWindow();
 system("PAUSE");   
 return 0; 
}

1 个答案:

答案 0 :(得分:0)

sortStudents中,您在开头有一个循环,它将读取文件中的所有字符。然后,您尝试将文件作为一种记录读取。当您尝试阅读超出文件末尾时,fscanf的这种用法将返回EOF

即使您没有读取starray数组中的任何记录,您仍然会打印来自starray[0]的值,这些值将全部未初始化(因此包含不确定值(例如看似随意的)值。)