Counterglow Forums  

Go Back   Counterglow Forums > General Discussions > Technoglow

Reply
 
Thread Tools Display Modes
Old 11-03-2004, 00:14:22   #1
KrazyHorse
Habs Fan
 
KrazyHorse's Avatar
 
Join Date: Dec 2001
Yet another round of "C for dummies"

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 is offline   Reply With Quote
Old 11-03-2004, 00:16:28   #2
KrazyHorse
Habs Fan
 
KrazyHorse's Avatar
 
Join Date: Dec 2001
The columns appear to be separated by tabs, by the way.

Each row is simply on a new line (no blank lines between.
KrazyHorse is offline   Reply With Quote
Old 11-03-2004, 02:12:34   #3
Venom
Look out behind you!
 
Venom's Avatar
 
Join Date: Nov 2001
Location: Rudolph's stable
Banished to the geek forum!
__________________
TO MY BALLS!!!!!
or
TO LazyView!!!!

Venom is offline   Reply With Quote
Old 11-03-2004, 02:13:07   #4
Venom
Look out behind you!
 
Venom's Avatar
 
Join Date: Nov 2001
Location: Rudolph's stable
This is C? Fuck that shit then.
__________________
TO MY BALLS!!!!!
or
TO LazyView!!!!

Venom is offline   Reply With Quote
Old 11-03-2004, 02:30:04   #5
Sir Penguin
Wise sprite
 
Sir Penguin's Avatar
 
Join Date: Nov 2001
Location: command.com
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):

Code:
#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
__________________
Whether 'tis nobler in the plane to suffer
The asps and adders of outrageous fortune,
Or to take arms against a sea of bitch-ass motherfuckers
And by opposing fuck them up?

-- Samuel L. Jackson as Hamlet

Last edited by Sir Penguin; 11-03-2004 at 02:32:14.
Sir Penguin is offline   Reply With Quote
Old 11-03-2004, 02:39:05   #6
KrazyHorse
Habs Fan
 
KrazyHorse's Avatar
 
Join Date: Dec 2001
Code:
#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 is offline   Reply With Quote
Old 11-03-2004, 02:41:21   #7
KrazyHorse
Habs Fan
 
KrazyHorse's Avatar
 
Join Date: Dec 2001
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.



Yours is also about a hundred times shorter than mine.
KrazyHorse is offline   Reply With Quote
Old 11-03-2004, 02:54:39   #8
Venom
Look out behind you!
 
Venom's Avatar
 
Join Date: Nov 2001
Location: Rudolph's stable
Yours has excellent spacing though. Well done.
__________________
TO MY BALLS!!!!!
or
TO LazyView!!!!

Venom is offline   Reply With Quote
Old 11-03-2004, 03:23:47   #9
Sir Penguin
Wise sprite
 
Sir Penguin's Avatar
 
Join Date: Nov 2001
Location: command.com
Dammit, and I just spent 45 minutes thrashing through a complete working program that demonstrates what I was talking about! This is mine:

Code:
#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
__________________
Whether 'tis nobler in the plane to suffer
The asps and adders of outrageous fortune,
Or to take arms against a sea of bitch-ass motherfuckers
And by opposing fuck them up?

-- Samuel L. Jackson as Hamlet

Last edited by Sir Penguin; 11-03-2004 at 07:04:44.
Sir Penguin is offline   Reply With Quote
Old 11-03-2004, 03:26:01   #10
Sir Penguin
Wise sprite
 
Sir Penguin's Avatar
 
Join Date: Nov 2001
Location: command.com
It took 45 minutes because I got the line array and the format string mixed up in the sscanf call.

SP
__________________
Whether 'tis nobler in the plane to suffer
The asps and adders of outrageous fortune,
Or to take arms against a sea of bitch-ass motherfuckers
And by opposing fuck them up?

-- Samuel L. Jackson as Hamlet
Sir Penguin is offline   Reply With Quote
Old 11-03-2004, 06:45:50   #11
KrazyHorse
Habs Fan
 
KrazyHorse's Avatar
 
Join Date: Dec 2001
Thank you very much, SP. I'll try your way next time.

You're a really good guy.
KrazyHorse is offline   Reply With Quote
Old 11-03-2004, 07:06:03   #12
Sir Penguin
Wise sprite
 
Sir Penguin's Avatar
 
Join Date: Nov 2001
Location: command.com
I'd be better if you'd taked a course in Python.

SP
__________________
Whether 'tis nobler in the plane to suffer
The asps and adders of outrageous fortune,
Or to take arms against a sea of bitch-ass motherfuckers
And by opposing fuck them up?

-- Samuel L. Jackson as Hamlet
Sir Penguin is offline   Reply With Quote
Old 11-03-2004, 18:36:09   #13
QtFLW@Work
But you can just call me QtFLW
 
QtFLW@Work's Avatar
 
Join Date: Jan 2004
Location: In the Government Salt Mines
Or you'd taken a course in English, SP
__________________
Counterglow Pity Poster
QtFLW@Work is offline   Reply With Quote
Old 11-03-2004, 20:53:18   #14
Sir Penguin
Wise sprite
 
Sir Penguin's Avatar
 
Join Date: Nov 2001
Location: command.com
What are you talking about?

SP
__________________
Whether 'tis nobler in the plane to suffer
The asps and adders of outrageous fortune,
Or to take arms against a sea of bitch-ass motherfuckers
And by opposing fuck them up?

-- Samuel L. Jackson as Hamlet
Sir Penguin is offline   Reply With Quote
Old 12-03-2004, 01:30:12   #15
Nav
Never gonna be as ugly as Lurker
 
Nav's Avatar
 
Join Date: Nov 2001
Location: Everywhere...
I think he means 'Qaj level' English
Nav is offline   Reply With Quote
Old 12-03-2004, 01:39:18   #16
Sir Penguin
Wise sprite
 
Sir Penguin's Avatar
 
Join Date: Nov 2001
Location: command.com
Oh God dammit, I just saw what he's talking about.

SP
__________________
Whether 'tis nobler in the plane to suffer
The asps and adders of outrageous fortune,
Or to take arms against a sea of bitch-ass motherfuckers
And by opposing fuck them up?

-- Samuel L. Jackson as Hamlet
Sir Penguin is offline   Reply With Quote
Old 12-03-2004, 07:59:43   #17
Deacon
Washes his hands.
 
Deacon's Avatar
 
Join Date: Jan 2002
Location: Illinois.
Deacon is offline   Reply With Quote
Old 12-03-2004, 17:10:16   #18
QtFLW@Work
But you can just call me QtFLW
 
QtFLW@Work's Avatar
 
Join Date: Jan 2004
Location: In the Government Salt Mines
__________________
Counterglow Pity Poster
QtFLW@Work is offline   Reply With Quote
Old 23-03-2004, 18:52:35   #19
Darkstar
will bitch for beer
 
Darkstar's Avatar
 
Join Date: Nov 2001
Location: Rocket City
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.
__________________
> clue++;
> display clue;
-878923403
Darkstar is offline   Reply With Quote
Reply
Forum Jump

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



All times are GMT. The time now is 13:23:31.


Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© Counterglow 2001-2012. All rights reserved.