c - trouble reading an array from a file into an array then printing it to another file -
hey guys i'm trying make piece of code work in c, i've got data file looks this:
123 456 789 101 121 131 415.... it's data image, numbers in grid of 256x128, i'm trying read in numbers using nested loops , printing them in same way there should same grid in output file actually:
-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460... i'm not sure needs changed in code fix i'm new programming , 1 of first attempts @ code sorry if not clear
#include <stdio.h>; #define height 128 #define width 256 #define max 255 char magic[] = "p3"; int main() { { int n, m; double r[128][256]; file *red = null; red = fopen ("picture.red", "r"); //read infile// (n=0; n<128; n++) { (m=0; m < 256; m++) fscanf(red, "%d", &r[n][m]); //scan in grid// } fclose (red); } { int n,m; double r[128][256]; file *pfile = null; pfile = fopen ("myfile.ppm", "w"); //create new file// fprintf(pfile, "%s\n%d %d\nnorman norm\n%d\n", magic, width, height, max); (n=0; n<128; n++) { //print header outpute file// (m=0; m<256; m++) fprintf (pfile, "%d", r[n][m]); //print grid output file// fprintf (pfile, "\n"); } fclose(pfile); //close output file// return 0; } } thanks :)
at least 3 changes needed:
you need change first
"%d"infscanf"%lf"because&r[n][m]pointerdouble.and change second
"%d"infprintf"%f".and move
double r[128][256];both sections use same array (and delete second definition).
or, declare r int rather double.
edit: compiler using? widely-used compilers should have given warning. example, gcc gives, standard switches, warnings:
line 1: warning: tokens @ end of #include directive
line 18: warning: format '%d' expects type 'int *', argument 3 has type 'double *'
line 32: warning: format '%d' expects type 'int', argument 3 has type 'double'
(plus complaint prototype of main()).
Comments
Post a Comment