Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
hi , i have a file which contain 16 elements....now i want to make a different file which will have a nice looking 4 x 4 matrix made of from those 16 elements....i am writing in C . how can i do that???
blue_gene
thanks

Read the data into a 2D array. Use a local variable to keep track of how many digits have been read, and then increment your indexes appropriately, and write the stuff out with a couple of loops.
myarray[4][4];
or
write the numbers as you read them in.
count = 0;
while(some_in_file)
{
read in val;if(count == 3)
{
fprintf(outf, "\n");
count = 0;
}
fprintf(outf, "%3d ", val);
count++;
}
Something like that. I'm a bit distracted at the moment, so check your data before writing or adding elements, and watch for overflow errors. :P

hi i was writing this way....but getting some errors at runtime(acesses violation in VC++)
#include<stdio.h>
main()
{
FILE *fp,*gp;
fp=fopen("c:\\x.dat","r");
gp = fopen("c:\\y.dat","w");
int a;
printf("hello");
int *buff;buff = NULL;
printf("hi");
do
{
fread(buff,sizeof(int),3,fp);
fprintf(gp,"%d\t%d\t%d",buff[0],buff[1],buff[2]);fprintf(gp,"\n");
}while(!feof(fp)) ;
fclose(fp);
fclose(gp);}
why acesses violation??????????

In other words dont use
int *buf;
but
int buf[5];or whatever number of elements you would like to have

![]() |
Email Validation in vb6
|
parking heads of HDD
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |