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.
How to best read from this input file.I'm thinking a loop?
Page 1 of 1
6 Replies - 437 Views - Last Post: 25 January 2010 - 04:08 PM
Replies To: How to best read from this input file.
#2
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
#3
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()
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();
#4
Re: How to best read from this input file.
Posted 24 January 2010 - 01:43 PM
ImaSexy, 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()
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
#5
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
#6
Re: How to best read from this input file.
Posted 24 January 2010 - 02:22 PM
Thats C , not C++
#7
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.
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.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|