PDA

View Full Version : Yet another round of "C for dummies"


KrazyHorse
11-03-2004, 00:14:22
I've got a file containing 3 columns of data (total of 100 rows)

I want to assign the first column to energy[i], the second to flux[i] and the third to error[i] (i running from 0 to 99 for ease)

So far I know that I need to write down fscanf(something)

HEEEELLLLLPPPPPPPP

KrazyHorse
11-03-2004, 00:16:28
The columns appear to be separated by tabs, by the way.

Each row is simply on a new line (no blank lines between.

Venom
11-03-2004, 02:12:34
Banished to the geek forum!

Venom
11-03-2004, 02:13:07
This is C? Fuck that shit then.

Sir Penguin
11-03-2004, 02:30:04
I wouldn't use fscanf. I've read that it's unreliable. Instead, I use fgets to get lines of data, and sscanf to break up the lines. I think it would go something like this (I don't know how to open filehandles in C):

#include <stdio.h>

char line[81]; /* input line buffer */

int main()
{
float energy,flux,error;
/* open the filehandle as 'in_file' */
fgets(line,sizeof(line),in_file); //get line from file
sscanf(line,"%f\t%f\t%f",&energy,&flux,&error); //break string into variables
printf("Energy: %f\nFlux: %f\nError: %f\n",energy,flux,error); //output
}
SP

KrazyHorse
11-03-2004, 02:39:05
#include <stdio.h>
#include <math.h>
#include "nr.h"
#include "nrutil.h"

main()

{

int i, j;
float numbers[300];
float energy[100];
float flux[100];
float error[100];

FILE *file;

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

i = 0;

while(!feof(file))

{

fscanf(file, "%f", &numbers[i]);
i++;

}

j = 0;
i = 0;

while (j < 100)

{

energy[j] = numbers[i];
j = j + 1;
i = i + 3;

}

j = 0;
i = 1;

while (j < 100)

{

flux[j] = numbers[i];
j = j + 1;
i = i + 3;

}

j = 0;
i = 2;

while (j < 100)

{

error[j] = numbers[i];
j = j + 1;
i = i + 3;

}


for (i = 0; i < 100; i++)
printf("%f\n", error[i]);


}

KrazyHorse
11-03-2004, 02:41:21
I just managed to find something that did the first bit for me (taking from dat and putting into array) on the internet after 90 minutes of searching.

Thank you very much SP; if I'd been slightly slower I'd be copy-pasting your code right now.

:beer:

Yours is also about a hundred times shorter than mine.

Venom
11-03-2004, 02:54:39
Yours has excellent spacing though. Well done.

Sir Penguin
11-03-2004, 03:23:47
Dammit, and I just spent 45 minutes thrashing through a complete working program that demonstrates what I was talking about! This is mine:

#include <stdio.h>

int main()
{
int i = 0; // iteration counter
float energy[100]; // values from first column
float flux[100]; // values from second column
float error[100]; // values from third column
char line[81]; // line input buffer

FILE *file; // input filehandle
file = fopen("xrayspect.dat","r");
for (i=0; i<100; i++) { // assuming there are exactly 100 lines
fgets(line,sizeof(line),file);
sscanf(line,"%f\t%f\t%f",&energy[i],&flux[i],&error[i]);
}
/* Now, the three float arrays have their values.
* You can do what you want with them. This does what you have:
*/

for (i=0; i<100; i++) {
printf("%f\n",error[i]);
}
fgets(line,sizeof(line),stdin); // "Press any key to exit" bit
}

SP

Sir Penguin
11-03-2004, 03:26:01
It took 45 minutes because I got the line array and the format string mixed up in the sscanf call.

SP

KrazyHorse
11-03-2004, 06:45:50
Thank you very much, SP. I'll try your way next time. :beer:

You're a really good guy. :bounce:

Sir Penguin
11-03-2004, 07:06:03
I'd be better if you'd taked a course in Python. :)

SP

QtFLW@Work
11-03-2004, 18:36:09
Or you'd taken a course in English, SP :)

Sir Penguin
11-03-2004, 20:53:18
What are you talking about? :mad:

SP

Nav
12-03-2004, 01:30:12
I think he means 'Qaj level' English ;)

Sir Penguin
12-03-2004, 01:39:18
Oh God dammit, I just saw what he's talking about.

SP

Deacon
12-03-2004, 07:59:43
:)

QtFLW@Work
12-03-2004, 17:10:16
:lol:

Darkstar
23-03-2004, 18:52:35
Humm... I'd have been tempted to pre-parsed the input to replace the tabs with "," character, then you'd been able to use standard MFC C/C++ code to read in the input directly into your variable/arrays. About 10 minutes of work that way.

BUT... you might not have been on a MS platform. ;)