6 Replies - 546 Views - Last Post: 25 January 2010 - 04:08 PM Rate Topic: -----

#1 Terryox  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 24-February 09

How to best read from this input file.

Posted 24 January 2010 - 12:58 PM

The assignment:
I'm given an input file with temperatures with timestamps attached. The timestamps need to be arranged from YYYYMMDDHHMM to a more readable format. The temperatures need to be converted to Celsius if in Fahrenheit, and also arranged in a more readable format. Lastly I need to find the average temperature.

I missed the lecture explaining how to do the assignment. After analyzing the source code, I've got most of it figured out, my only problem is reading from the input file.

The first line of the input file is the number of temperatures I'm given. Every line after that is a timestamp and a temperature. It will look like this:

2
200707211245 F70.5
200708220812 C19.97


My first thought was an array, but the instructions say we're not permitted to do so. Right now I'm thinking a loop, only I'm not quite sure how to skip the fist number. Or how to terminate the loop once I'm finished. If someone could help me figure out how to go about this I would be very grateful.

Is This A Good Question/Topic? 0
  • +

Replies To: How to best read from this input file.

#2 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 282
  • View blog
  • Posts: 1,456
  • Joined: 16-May 09

Re: How to best read from this input file.

Posted 24 January 2010 - 01:03 PM

[ This ] might help you alot, thanks to Bench with this awesome tutorial. Look at his example, it's similar to what you have here,just change as what you need :)

This post has been edited by Anarion: 24 January 2010 - 01:04 PM

Was This Post Helpful? 0
  • +
  • -

#3 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1075
  • View blog
  • Posts: 4,536
  • Joined: 09-June 09

Re: How to best read from this input file.

Posted 24 January 2010 - 01:19 PM

your correct about your loop idea.
This loop will read to the end of the file. The number at the top of the file seems pretty irrelavant since your not using an array or vector ect.. but heres a quick example of how to loop with getline()
	ifstream file("file.txt");

	string line = "";
	while(getline(file,line))
	{
		//parse each line at space " " to get timestamp and F/C degrees
		//If C is found convert to F	
	}

	file.close();
	


Was This Post Helpful? 0
  • +
  • -

#4 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 282
  • View blog
  • Posts: 1,456
  • Joined: 16-May 09

Re: How to best read from this input file.

Posted 24 January 2010 - 01:43 PM

View PostImaSexy, on 24 Jan, 2010 - 10:49 PM, said:

your correct about your loop idea.
This loop will read to the end of the file. The number at the top of the file seems pretty irrelavant since your not using an array or vector ect.. but heres a quick example of how to loop with getline()
	ifstream file("file.txt");

	string line = "";
	while(getline(file,line))
	{
		//parse each line at space " " to get timestamp and F/C degrees
		//If C is found convert to F	
	}

	file.close();
	



Just a little note here, string line = ""; can be shortened to string line; there is no need to make it empty at first because it doesn't contain any characters at initialization time.

This post has been edited by Anarion: 24 January 2010 - 01:44 PM

Was This Post Helpful? 0
  • +
  • -

#5 gda2004  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 111
  • Joined: 08-January 09

Re: How to best read from this input file.

Posted 24 January 2010 - 01:49 PM

 void read()
{
	FILE *walls;

	if((walls=fopen("walls", "r")) == NULL) 
	{
		printf("Cannot open file\n");
	}
	while (fscanf(walls, "%f%f%f%f%f%f%f%f%f%f%f%f",&t,&face,&angle,&rotx,&roty,&rotz,&x,&y,&z,&w,&l,&h)!= EOF)
	{
		check = check+1;
		t1 = t;
		//fprintf(stdout,"%d\n %f\n %f\n %f\n %f\n %f\n %f\n",t1,x,y,z,w,l,h); 
		drawobj(t1,face,angle,rotx,roty,rotz,x,y,z,w,l,h);



	}
	angle = 0.00;
	fclose( walls );



}



That is what i did for loading in an object from a file you can put them straight into variables and
Was This Post Helpful? 0
  • +
  • -

#6 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1075
  • View blog
  • Posts: 4,536
  • Joined: 09-June 09

Re: How to best read from this input file.

Posted 24 January 2010 - 02:22 PM

Thats C , not C++
Was This Post Helpful? 0
  • +
  • -

#7 Terryox  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 24-February 09

Re: How to best read from this input file.

Posted 25 January 2010 - 04:08 PM

Thanks everyone, but I figured out that I was just over thinking it.


I just have to tell the loop to go through however many times the first number in the input file is equal to, and then have it process the strings as it reads them. Hahaha.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1