4 Replies - 1742 Views - Last Post: 14 July 2007 - 09:36 AM Rate Topic: -----

#1 ynnhoj67  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 13-July 07

using struct to write info into a file

Post icon  Posted 13 July 2007 - 10:49 PM

Im using struct to get the name in string and grades in int, then write those info into a file. I can't read the int number in that file, instead i got garbage stuff showed up in that file. Im just a beginner, so please help me. the code is below.thank you!
struct data
{
	   char name[20];
	   int grade;
	   };

int main ()
{
   int i,n;
   FILE*data;
   printf("\nHow many students will be input: ");
   scanf("%d",&n);
   struct data info[n];
   for(i=0;i<n;i++)
								 {
									 printf("\n%d student name: ",i+1);
									 scanf("%s",info[i].name);
									 printf("\n%d student grade: ",i+1); 
									 scanf("%d",&info[i].grade);
									 data=fopen("d:\\record.txt","aw");
									 fwrite(&info[i].name,strlen(info[i].name),n,data);
									 fclose(data);
									 data=fopen("d:\\test.txt","aw");
									 fwrite(&info[i].grade,sizeof(info[i].grade),n,data);
									 fclose(data);
									 }
  getch();
}



Is This A Good Question/Topic? 0
  • +

Replies To: using struct to write info into a file

#2 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5363
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: using struct to write info into a file

Posted 13 July 2007 - 11:34 PM

I know this is written in C, but hopefully it will help.

#include <stdio.h>

#define MAX 99

int main(void) {
	 struct {
		char name[MAX];
		int grade;
	} x[MAX];
	int i=0,c=0,n=0,j=0;
	char buffer[MAX],ch;
	FILE *data;

	ch='\0';

	printf("\nHow many students: ");
	while(ch!='\n') {
	  ch=getc(stdin);
	  buffer[c]=ch;
	  c++;
	}

	ch='\0';
	c=0;
	n=atoi(buffer);
	while (i<n) {
		j=i+1;
		printf("\n%d: Enter name:",j);

		while(ch!='\n') {
			 if(c>=98){
				  printf("\nOverflow");
				  exit(1);
			 }
			 ch=getc(stdin);
			 buffer[c]=ch;
			 c++;
		}
		for(c=0;c<MAX;c++) {
			 if(buffer[c]!='\n')x[i].name[c]=buffer[c];
		}

		c=0;
		ch='\0';
		printf("\n%d Enter grade:",j);

		while(ch!='\n') {
			 if(c>=98){
				 printf("\nOverflow");
				exit(1);
			}
			ch=getc(stdin);
			buffer[c]=ch;
			c++;
	   }
	   x[i].grade=atoi(buffer);
	   c=0;
	   ch='\0';
	   i++;
  }

  data=fopen("record.txt","w+");
  if(data) {
	for(i=0;i<n;i++) fprintf(data,"%s\n",x[i].name);
	fclose(data);
  }
  else printf("Error opening data file.\n");
  data=fopen("test.txt","w+");
  if(data) {
	for(i=0;i<n;i++) fprintf(data,"%d\n",x[i].grade);
	fclose(data);
  }
  else printf("Error opening data file.\n");
  return 0;
}



This will produce the following output :
[#2pencil:~/progs/c/code]$ ./junk												

How many students: 2

1: Enter name:Tim  

1 Enter grade:4

2: Enter name:John

2 Enter grade:3
[#2pencil:~/progs/c/code]$ cat *.txt											 
Tim
John
4
3


Was This Post Helpful? 0
  • +
  • -

#3 ynnhoj67  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 13-July 07

Re: using struct to write info into a file

Posted 14 July 2007 - 12:07 AM

anyways thank you, i don't know C that much, but still can see you are good programmer as long as you are use error checking.
i was kinda wondering what's the difference between "fprintf" and "fwrite"
if im using "fprintf" can i use "fread" afterward inorder to modify the info already existed in that file?
btw, could you please explain the logic little bit about ur coding.
thank you so much..
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5363
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: using struct to write info into a file

Posted 14 July 2007 - 01:08 AM

#include <stdio.h>

// Set the maximum to 99
#define MAX 99

int main(void) {
	 // 99 items in the struct
	 struct {
		char name[MAX];
		int grade;
	} x[MAX];
	int i=0,c=0,n=0,j=0;
	// our word buffer & character buffer
	char buffer[MAX],ch;
	FILE *data;

	// set the character buffer to be blank (no value)
	ch='\0';

	printf("\nHow many students: ");
	// while our character buffer is not equal to the enter key
	while(ch!='\n') {
	  // get a character from standard input
	  ch=getc(stdin);
	  // put it to the c value of our array (word buffer)
	  buffer[c]=ch;
	  // incrament the counter.
	  c++;
	}
	// Because enter was pressed, we've got our answer, blank out the buffers.
	ch='\0';
	c=0;
	// set the value of n to the int converted from char value of our word buffer
	n=atoi(buffer);
	while (i<n) {
		// set J equal to i for our output (so that it doesn't loop 0 through whatever)
		j=i+1;
		printf("\n%d: Enter name:",j);

		while(ch!='\n') {
			 if(c>=98){  // if we are at the max (defined above) amount of data 
								   // can hold, then don't try to hold anymore!
				  printf("\nOverflow");
				  exit(1);  // show the user their error & stop the program before
								// it crashes.
			 }
			 ch=getc(stdin);
			 // as long as we are not going our limit, might as well collect the data
			 // into our buffers, just like we did above.
			 buffer[c]=ch;
			 c++;
		}
		for(c=0;c<MAX;c++) {
			 // loop through all of the characters in our buffer array, 
			 // clearing out the enter key, & then dump them into the x[*loop*].name
			 // struct.
			 if(buffer[c]!='\n')x[i].name[c]=buffer[c];
		}

		c=0;
		ch='\0';
		printf("\n%d Enter grade:",j);

		// all of this is the same as above.
		while(ch!='\n') {
			 if(c>=98){
				 printf("\nOverflow");
				exit(1);
			}
			ch=getc(stdin);
			buffer[c]=ch;
			c++;
	   }
	   x[i].grade=atoi(buffer);
	   // clear the buffers again.
	   c=0;
	   ch='\0';
	   i++;
  }

  // open the file with read/write ability, create it if it doesn't exist.
  data=fopen("record.txt","w+");
  if(data) {  // check to see if the open was successful
	// since it exists, lets dump our data into it.
	for(i=0;i<n;i++) fprintf(data,"%s\n",x[i].name);
	fclose(data);
  }
  // if it was not a success, then let them know & back out.
  else printf("Error opening data file.\n");
  // same as right above, just a different file.
  data=fopen("test.txt","w+");
  if(data) {
	for(i=0;i<n;i++) fprintf(data,"%d\n",x[i].grade);
	fclose(data);
  }
  else printf("Error opening data file.\n");

  // let the OS know that we've successfully reached the end of our program 
  // & it's safe to close any open memory space.
  return 0;
}


Was This Post Helpful? 0
  • +
  • -

#5 ynnhoj67  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 13-July 07

Re: using struct to write info into a file

Posted 14 July 2007 - 09:36 AM

thank you so much...i gotcha it :^:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1