2 Replies - 206 Views - Last Post: 11 November 2012 - 02:18 AM Rate Topic: -----

#1 panelopy123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 26-October 12

file readin &writin

Posted 10 November 2012 - 06:49 AM

]I wanna write a program which includes student database in file.so for testin the application of fread and fwrite function with structure i wrote a small program but in file its givin so many zeroes with name.
rectify errors


#include<stdio.h>
struct student
 {
   int roll;
	float marks;
	char name[11];
 };
 struct student s;[b][/b]

 main()
  {
   	FILE *fp;
	fp=fopen("student.dat","w");
	
	
	printf("name\n");
	scanf("%s",s.name);
	printf("marks\n");
	scanf("%f",&s.marks);
	printf("age");
	scanf("%d",&s.roll);
	fwrite(&s,sizeof(struct student),1,fp);
	fclose(fp);
	}

This post has been edited by jimblumberg: 10 November 2012 - 07:01 AM
Reason for edit:: Added missing Code Tags, Please learn to use them.


Is This A Good Question/Topic? 0
  • +

Replies To: file readin &writin

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 3988
  • View blog
  • Posts: 12,302
  • Joined: 25-December 09

Re: file readin &writin

Posted 10 November 2012 - 07:06 AM

If you are going to try to write the file using the fwrite() function I suggest you open the file in binary mode "wb". Also how are you trying to read the file. When you use the fwrite() function you can't view the file with a normal editor.

Also please show what you entered for the values.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 panelopy123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 26-October 12

Re: file readin &writin

Posted 11 November 2012 - 02:18 AM

i have not used fread function but i'll use it.bcz fwrite only is not workin.in file with name and other details so many zeroes are coming.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1