PDA

View Full Version : Another C question


KrazyHorse
01-04-2004, 00:05:29
Say I've got an array of data (which are numbers) datatu[i] with i going from 1 to rows*rows (in this case rows = 512)

I want to output this to a file (say, newimage.dat) which is to have 20 entries a line (separated by tabs) with 13107 lines in the file, plus the final 4 entries on the 13108th line.

Help me please. I'll give you another of these :beer: if you do.

Sir Penguin
01-04-2004, 00:08:32
How do you open a file and print to it?

SP

KrazyHorse
01-04-2004, 00:09:23
Damned if I know.

it's got something to do with fprintf

KrazyHorse
01-04-2004, 00:10:52
Oh, it'd also be nice if the output was in scientific notation with 3 sigfigs, but I won't get greedy.

Sir Penguin
01-04-2004, 00:11:14
That's what I thought, it was more the opening the file part I'm stuck on. Your last C thread needed a file to be opened for reading. This would be the same, but with an "r" instead of a "w" or something.

SP

KrazyHorse
01-04-2004, 00:13:05
Just for shits and giggles, here's my latest monstrosity



#include <stdio.h>
#include <math.h>
#include "nr.h"
#include "nrutil.h"

float magnitude(float, float);

main()

{

int ndim = 2;
int i, j, k;
int rows = 512; /* number of rows */
unsigned long int *nn;
float numbers[rows * rows];
float *data;
float *datat;
float *datau;
float *datatu;
int isignt = 1;
int isignu = -1;
float **mtrx, **mtrxu;
float maxmag;
int maxfreq;
int onetozero, twotoone, thrtotwo, frtothr;

nn = lvector(1, ndim);
data = vector(1, 2 * rows * rows);
datat = vector(1, 2 * rows * rows);
datau = vector(1, 2 * rows * rows);
datatu = vector(1, rows);

mtrx = matrix(1, rows, 1, rows);
mtrxu = matrix(1, rows, 1, rows);

FILE *file;

file = fopen("image.dat", "r");

k = 0;

while(!feof(file))

{

fscanf(file, "%f", &numbers[k]); /* taking file entries in order */
k++;

}

i = 1;
k = 0;

while(k < rows * rows)

{

data[i] = numbers[k];
datat[i] = numbers[k];
i = i + 2;
k = k + 1;

}

/* use below to build a matrix out of data */

/* j = 1; */
/* i = 1; */
/* k = 1; */

/* while(i <= rows) */

/* { */

/* while(j <= rows) */

/* { */

/* mtrx[i][j] = data[k]; */
/* k = k + 2; */
/* j = j + 1; */

/* } */

/* i = i + 1; */
/* j = 1; */

/* } */

nn[1] = rows;
nn[2] = rows;

/* use below to check values of matrix */

/* for(j = 1; j <= 11; j++) */
/* printf("%18.15f\n", mtrx[2][j]); */

fourn(datat, nn, ndim, isignt);

/* use below to check transformed data */

/* for(j = 1; j <= 11; j++) */
/* printf("%18.15f\n", datat[j]); */

maxmag = 0.0;

i = 1;
maxfreq = 0;

while(i <= 2 * rows * rows)

{

if(magnitude(datat[i], datat[i + 1]) <= maxmag)
i = i + 2;

else

{

maxmag = magnitude(datat[i], datat[i + 1]);
maxfreq = i;
i = i + 2;

}

}

/* use below to check magnitudes */

/* for(j = 0; j <=250; j++) */
/* printf("%18.15f\n", magnitude(datat[1000 * j + 1], datat[1000 * j + 2])); */

/* use below to check maximum magnitude */

/* printf("%18.15f\n", maxmag); */

/* printf("%6d\t %18.15f\t %18.15f\n", maxfreq, datat[maxfreq], datat[maxfreq + 1]); */

/* use below to bin transformed data */

/* i = 1; */
/* onetozero = 0; */
/* twotoone = 0; */
/* thrtotwo = 0; */
/* frtothr = 0; */

/* while(i <= 2 * rows * rows) */

/* { */

/* if(magnitude(datat[i], datat[i + 1]) >= 0.1 * maxmag) */

/* { */

/* onetozero = onetozero + 1; */
/* i = i + 2; */

/* } */

/* else */

/* { */

/* if(magnitude(datat[i], datat[i + 1]) >= 0.01 * maxmag) */

/* { */

/* twotoone = twotoone + 1; */
/* i = i + 2; */

/* } */

/* else */

/* { */

/* if(magnitude(datat[i], datat[i + 1]) >= 0.001 * maxmag) */

/* { */

/* thrtotwo = thrtotwo + 1; */
/* i = i + 2; */

/* } */

/* else */

/* { */

/* if(magnitude(datat[i], datat[i + 1]) >= 0.0001 * maxmag) */

/* { */

/* frtothr = frtothr + 1; */
/* i = i + 2; */

/* } */

/* else */

/* i = i + 2; */

/* } */

/* } */

/* } */

/* } */

/* use below to check bin numbers */

/* printf("0.1-1, 0.01-0.1, 0.001-0.01, 0.0001-0.001 %d\t %d\t %d\t %d\n", onetozero, twotoone, thrtotwo, frtothr); */

i = 1;

while(i <= 2 * rows * rows)

{

if(magnitude(datat[i], datat[i + 1]) >= 0.01 * maxmag)

{

datau[i] = datat[i];
datau[i + 1] = datat[i + 1];
i = i + 2;

}

else

{

datau[i] = 0.0;
datau[i + 1] = 0.0;
i= i + 2;

}

}

fourn(datau, nn, ndim, isignu);

for(i = 1; i <= 2 * rows * rows; i++)
datau[i] = datau[i] / (rows * rows);

i = 1;
j = 1;

while(j <= rows)

{

datatu[j] = datau[i];
i = i + 2;
j = j + 1;

}

for(i = 1; i <= 50; i = i + 1)
printf("%18.15f\n", datatu[i]);



}

float magnitude(float real, float imaginary)

{

float magsquared, mag;

magsquared = real * real + imaginary * imaginary;
mag = pow(magsquared, 0.5);

return(mag);

}

KrazyHorse
01-04-2004, 00:21:59
Well, according to the code i blatantly copy-pasted from elsewhere, the syntax for opening this file is

file = fopen("newimage.dat", "r");

or something like that

Sir Penguin
01-04-2004, 03:39:42
Here's what I've got:

#include <stdio.h>

#define ARRAY_SIZE 104 // size of the data array

int main()
{
float datatu[ARRAY_SIZE]; // given array of data
int i; // iteration counter
FILE *outFile; // output filehandle

// populate the data array with some values, just for example's sake
for (i=0; i<ARRAY_SIZE; i++) {
datatu[i] = 5.0 + i;
}

outFile = fopen("newimage.dat","w");
for (i=1; i<ARRAY_SIZE+1; i++) {
fprintf(outFile,"%.3e",datatu[i-1]);
// print a \n if it's the end of the line, otherwise a \t
fprintf(outFile,(i%20==0)?"\n":"\t");
}
}
SP

Sir Penguin
01-04-2004, 03:42:17
The second for loop starts at 1 because otherwise the i%20==0 expression would return true for the 0th element in the array (0%20==0) and it would be on a line all by itself.

SP

KrazyHorse@home
01-04-2004, 03:59:26
I'll try it tomorrow, SP. Thank you.
Here's one in advance

:beer:

Asher
01-04-2004, 08:22:47
Have I mentioned that I hate C?