2 Replies - 1201 Views - Last Post: 04 November 2011 - 03:51 AM Rate Topic: -----

#1 zilstershiner   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-November 11

How to read from a file into an array

Posted 03 November 2011 - 10:29 PM

I am doing a homework assignment where I am to read and write from external files. I have most of it correct, but one problem I'm having is reading values from a file, and getting those values into an array. The program determines the position of three objects using an array, and uses gets the x,y from a file. This is what I have, but I can only get it to create one object:
inputFile = fopen(inputFileName,"r");
while(fscanf(inputFile,"%d,%d,%f\n",&n1,&n2,&s1) != EOF){
	int xFly[3]={n1},yFly[3]={n2},vxFly[3]={0,0,0},vyFly[3]={0,0,0},axFly[3]={0,0,0},ayFly[3]={0,0,0};
	float sFly[3]={s1};
	graphics_init(500,500);//creates graphics window
	while(kiss_get_key_bit('Q')==0){ //Ends function when Q is pressed.
		draw_background();//draws background
		kiss_get_mouse_position(&x,&y);
		g_printString("Click a fly",100,30,200,200,200,3);//Adds text to graphics
		for(i=0;i<3;i++){ //draws out objects using arrays
			draw_fly(xFly[i],yFly[i],sFly[i]);//Draws flies
			xFly[i]=xFly[i]+vxFly[i];//defines x position
			yFly[i]=yFly[i]+vyFly[i];//defines y position
			vxFly[i]=vxFly[i]+axFly[i];//definines x velocity
			vyFly[i]=vyFly[i]+ayFly[i];//defines y velocity
			}
		}
	}



The file that I am reading from says:
50,75,.5
200,50,.6
350,100,.7



How do I get the values to work with the array?

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: How to read from a file into an array

#2 zilstershiner   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-November 11

Re: How to read from a file into an array

Posted 03 November 2011 - 10:38 PM

By the way, this is only a small snippet of the entire code.
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: How to read from a file into an array

Posted 04 November 2011 - 03:51 AM

Objective-C is NOT standard C. Totally, totally different.

Moved to C and C++
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1