Hello to you all ,

Major problem in C , Files included

Summary of problem :
Program will not copy the feild "name" from struct "univ->std.name" properly. the rest of data is presented fairly.
do not touch function "output_university" .

Thank you ,

yotam , Israel .

Attachments
Nir 32251 99.80 11001
Ely 12347 77.89 01111
Moshe 45321 50.34 11111
Avi 31456  49.78 00011
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include <math.h>
#define MAX 20
static void forcefloat(float *p)
{
float f=*p;
forcefloat(&f);
}

typedef struct{
char* name;
long id;
float avrg;
char works[5];
}student;

typedef struct{
student *std;
}university;

void input_university(university *univ, FILE *in);
void output_university(university *univ, FILE *in);
void output_factor(university *univ, FILE *in);

int num_cells=0;
char* temp;

int main()
{
FILE *in, *out;
university univ;
char op='a';
static int flag=0;
printf("please enter your options:\n");
printf(" a:for reading\n b:for output of the student information\n c:for showing marks after the factor\n z:exit");
while(op!='z')
{
	scanf("%c",&op);
	switch(op)
	{
		case 'a':if((in=fopen("input.txt","r"))==NULL)
			 {
			 fprintf(stderr, "input file not available");
			 break;
			 }
		       else {
		     input_university(&univ,in);
				fclose(in);
				flag=1;
				break;  }
case 'b':if(flag==1)
				{
				if((out=fopen("output.txt","w"))==NULL)
					{
					fprintf(stderr, "output file not available");
					break;
					}
				else {
				output_university(&univ,out);
				fclose(out);
				flag=2; }
				}
				else
				{
				if((out=fopen("output.txt","w"))==NULL)
				{
				fprintf(stderr, "output file not available");
				break;
				}
				fprintf(out, "'b' before 'a'-not available");
				fclose(out);
				break;
				}
				break;
case 'c':			if(flag==2)
				{
				if((out=fopen("output.txt","a"))==NULL)
				{
				fprintf(stderr, "output file not available");
				break;
				}
				else {
				output_factor(&univ,out);
				fclose(out); }
				}
				else{
				if((out=fopen("output.txt","a"))==NULL)
				{
				fprintf(stderr, "output file not available\n");
				break;
				}
				fprintf(out, "'c' brfore 'a' or 'b' -not available\n");
				fclose(out);
				break;
				}
				break;
case 'z': { printf("end of program"); }
}
}
getch(); flushall();
return 0;
}



void input_university(university *univ, FILE *in)
{
int i,j,str_s;
char buffer[MAX];


while(fscanf(in,"%s",buffer)!=EOF)
{

   str_s=strlen(buffer);

   univ->std=(student*)realloc(univ->std,(num_cells+1)*sizeof(student));
     if (univ->std==NULL) exit(1);
   univ->std[num_cells].name=(char*)malloc(str_s+1);
     if (univ->std[num_cells].name==NULL) exit(1);
   temp=(char*)malloc(5*sizeof(char));
     if (temp==NULL) exit(1);

 fscanf(in,"%ld",&univ->std[num_cells].id);
 fscanf(in,"%f",&univ->std[num_cells].avrg);
 strcpy(temp,univ->std[num_cells].works);
 fscanf(in,"%s",temp);

 printf("%s %ld %.2f %s \n",univ->std[num_cells].name,univ->std[num_cells].id,univ->std[num_cells].avrg,univ->std->works);
 ++num_cells;

}
}


void output_university(university *univ, FILE *out)
{
 int i,j,count;
 char *temp;
fprintf(out,"BEFORE:\n");
for(i=0;i<num_cells;i++)
 {
 fprintf(out,"student %d : ",i);
 //fputs(univ->std[i].name,out);
 fprintf(out,"%s",univ->std[i].name);

 fprintf(out," %ld",univ->std[i].id);
 fprintf(out," %.2f",univ->std[i].avrg);
 count=0;
 temp=univ->std[i].works;
 while(temp!='\0'){
  if(*temp==1)
  count++;
  temp++;
  }
 if(count==3 || count>3)
 fprintf(out,"%s",'1');
 else
 fprintf(out,"%s",'0');


fprintf(out,"\n");
}
}
void output_factor(university *univ, FILE *out)
{
int i,j,max=univ->std[0].avrg,factor,count;
char *temp;
fprintf(out,"AFTER:\n");
for(i=1;i<num_cells;i++)
{
if(univ->std[i].avrg>max)
max=univ->std[i].avrg;
}
for(i=0;i<num_cells;i++)
{
fprintf(out,"student %d\n",i);
fputs(univ->std->name,out);
fprintf(out,"%d",univ->std->id);
factor=((univ->std[i].avrg) *100 )/max;
fprintf(out,"%f",factor);
}
temp=univ->std[i].works ;
count=0;
while(temp!='\0'){
  if(*temp==1)
  count++;
  temp++;
  }
 if(count==3 || count>3)
 fprintf(out,"%s",'1');
 else
 fprintf(out,"%s",'0');
}

In function input_university() I see where scanf() is reading the string, and univ->std[num_cells].name is getting allocated, but I don't see where it is copying buffer to univ->std[num_cells].name after allocation. Add this and it will probably work ok.

strcpy(univ->std[num_cells].name,buffer);

Apparently, I wasted my time :mad:
http://www.daniweb.com/techtalkforums/thread43435.html

Not only have you dropped the initialisation of univ in main(), but you've gone back to using fscanf to read the file.
Not only that, you're now using global variables like "temp", which get allocated on every iteration of the loop and never freed.
Don't forget the not using a temporary variable when calling realloc, nor the unnecessary casting of malloc and realloc.

Y'know what, if you're just going to ignore people, just say so - ok?

Apparently, I wasted my time :mad:
http://www.daniweb.com/techtalkforums/thread43435.html

Not only have you dropped the initialisation of univ in main(), but you've gone back to using fscanf to read the file.
Not only that, you're now using global variables like "temp", which get allocated on every iteration of the loop and never freed.
Don't forget the not using a temporary variable when calling realloc, nor the unnecessary casting of malloc and realloc.

Y'know what, if you're just going to ignore people, just say so - ok?

I guess i didnt see it - sorry mate :confused:

In function input_university() I see where scanf() is reading the string, and univ->std[num_cells].name is getting allocated, but I don't see where it is copying buffer to univ->std[num_cells].name after allocation. Add this and it will probably work ok.

strcpy(univ->std[num_cells].name,buffer);

I did just that . now it messes up another string - i'll figure it out
somehow , the thing is , we were never tought the subject BUFSIZ and
"sscanf" - so no offence there ;)
i got slapped on using it .... sorry

This article has been dead for over six months. Start a new discussion instead.