binaryFiles.c

binF.c

fread/ fwrite in binary files. In my 1st semester in DIT, in order to save/load game for the game reversi(othello), we used binary files.

Do not forget to select the appropriate mode in fopen. Also close the file stream with fclose.
“In order to open a file as a binary file, a “b” character has to be included in the mode string. This additional “b” character can either be appended at the end of the string (thus making the following compound modes: “rb”, “wb”, “ab”, “r+b”, “w+b”, “a+b”) or be inserted between the letter and the “+” sign for the mixed modes (“rb+”, “wb+”, “ab+”).”

int for_load(FILE *fp,int play,int n,int **table)
{
	int i,j,ok,a,size_for_malloc;
    char *s,buf[20];

	fprintf(stdout,"Name : ");
	fgets(buf,20,stdin);
	size_for_malloc=strlen(buf)+1;
	s=malloc(size_for_malloc*sizeof(char) );
	strcpy(s,buf); /* Get the name of the file */
	fp = fopen(s, "rb");
	if (fp==NULL)
	{
		printf("Didn't find the file");
		return 1;
	}
	ok=fread(&n, sizeof(int), 1, fp);
	if(ok!=1)
	{
        printf("Sorry,something went wrong\n");
	}
	for(i=0 ; i<n ; i++)
	{
		for(j=0 ; j<n ; j++)
		{
			fread(&a, sizeof(int), 1, fp);
			if(a=='b')
			{printf("0  ");
				table[i][j]=0;
			}
			else if(a=='w')
			{printf("1  ");
				table[i][j]=1;
			}
			else if(a=='n')
			{printf("2  ");
				table[i][j]=2;
			}
		}
	}
	ok=fread(&play, sizeof(int), 1, fp);
	if(ok!=1)
	{
    	fclose(fp);
    	return 1;
	}
}
int for_save(FILE **fp,int play,int n,int **table)
{
	int i,j,a,ok,size_for_malloc;
	char *s,buf[20];
	fprintf(stdout,"Name : ");
	fgets(buf,20,stdin);
	size_for_malloc=strlen(buf)+1;
	s=malloc(size_for_malloc*sizeof(char) );
	strcpy(s,buf);
       *fp = fopen(s, "wb");
	fwrite(&n, sizeof(int), 1, *fp);/* Save size of the game*/
	for(i=0 ; i<n ; i++)/* Save all the postitions*/
	{
		for(j=0 ; j<n ; j++)
		{
			if(table[i][j]==0)
			{
				a='b';
				ok=fwrite(&a, sizeof(int), 1, *fp);
				if(ok!=1)
				{
                	printf("Sorry,something went wrong\n");
				}
			}
			else if(table[i][j]==1)
			{
				a='w';
               	ok=fwrite(&a, sizeof(int), 1, *fp);
            	if(ok!=1)
				{
              		printf("Sorry,something went wrong\n");
				}
			}
			else if( (table[i][j]==2)||(table[i][j]==3) )
			{
				a='n';
                ok=fwrite(&a, sizeof(int), 1, *fp);
                if(ok!=1)
				{
               		printf("Sorry,something went wrong\n");
				}
			}
		}
	}
	ok=fwrite(&play, sizeof(int), 1, *fp);/* Save the round that we are in*/
    	if(ok!=1)
	{
        printf("Sorry,something went wrong\n");
	}
	fwrite(&s,sizeof(char),20, *fp);/* Save name given by user*/
	if (*fp==NULL)
	{
		printf("Didn't find the file");
		return 1;
	}
	fclose(*fp);
}

This code is developed by me, G. Samaras.

Have questions about this code? Comments? Did you find a bug? Let me know!😀
Page created by G. (George) Samaras (DIT)

One thought on “binaryFiles.c

  1. Pingback: reading from binary files

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s