Computing.Net > Forums > Programming > file pointer problem [ C program]

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

file pointer problem [ C program]

Reply to Message Icon

Name: blue_gene
Date: December 20, 2003 at 09:58:43 Pacific
OS: xp
CPU/Ram: 128mb
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: Ronin1
Date: December 20, 2003 at 12:45:40 Pacific
Reply:

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


0

Response Number 2
Name: blue_gene
Date: December 21, 2003 at 02:58:42 Pacific
Reply:

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??????????


0

Response Number 3
Name: dayi
Date: December 21, 2003 at 07:12:56 Pacific
Reply:

because you haven't allocated any memory for the buff pointer


0

Response Number 4
Name: ChrisKansai
Date: December 21, 2003 at 18:28:32 Pacific
Reply:

In other words dont use
int *buf;
but
int buf[5];

or whatever number of elements you would like to have


0

Response Number 5
Name: blue_gene
Date: December 22, 2003 at 06:34:46 Pacific
Reply:

thanks... i got it


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon

Email Validation in vb6 parking heads of HDD



Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: file pointer problem [ C program]

I'm having problem c++ programming www.computing.net/answers/programming/im-having-problem-c-programming/17450.html

read text file in c to char pointer www.computing.net/answers/programming/read-text-file-in-c-to-char-pointer/7074.html

problem in compiling c program www.computing.net/answers/programming/problem-in-compiling-c-program/7435.html