42 Replies - 1360 Views - Last Post: 01 October 2010 - 08:14 PM
#1
A little confused with I/O with files.
Posted 01 October 2010 - 02:33 PM
Basically what I'm asking is, can I take the text file with the stats that he gave me in there, and just get my application to read the txt file and display it? My last task is to after I've done that, is display the average temperature of all cities put together, and the standard deviation of the average temperatures.
This is what it is suppose to look like when it's all said and done : http://i51.tinypic.com/30kx3yf.jpg
So basically my questions are, how exactly do I read the text file he provided us if he only provided us with 1 file? Then can I directly use that file information (max and min temperatures) to calculate the mean and standard deviation?
Again, thanks for reading.
Replies To: A little confused with I/O with files.
#2
Re: A little confused with I/O with files.
Posted 01 October 2010 - 02:35 PM
#3
Re: A little confused with I/O with files.
Posted 01 October 2010 - 02:41 PM
#4
Re: A little confused with I/O with files.
Posted 01 October 2010 - 02:42 PM
#5
Re: A little confused with I/O with files.
Posted 01 October 2010 - 02:45 PM
the setw() function will allow you to space the different items at a repeatable spacing.
Jim
#6
Re: A little confused with I/O with files.
Posted 01 October 2010 - 02:45 PM
#7
Re: A little confused with I/O with files.
Posted 01 October 2010 - 02:55 PM
#8
Re: A little confused with I/O with files.
Posted 01 October 2010 - 03:09 PM
EDIT: Maybe this tutorial will be helpful to you. This page is also likely to be helpful.
This post has been edited by JackOfAllTrades: 01 October 2010 - 03:11 PM
#9
Re: A little confused with I/O with files.
Posted 01 October 2010 - 04:02 PM
#10
Re: A little confused with I/O with files.
Posted 01 October 2010 - 04:09 PM
Quote
#11
Re: A little confused with I/O with files.
Posted 01 October 2010 - 04:11 PM
Munchiester, on 01 October 2010 - 05:02 PM, said:
Post your code in code tags
#12
Re: A little confused with I/O with files.
Posted 01 October 2010 - 04:12 PM
Oler1s, on 01 October 2010 - 03:09 PM, said:
Quote
Well it seems there's 2 different ways of doing it I suppose. I read up on the tutorial posted above, but there's also lessons by my prof. I used the one in the tutorial and was sucessful, it read and displayed the text file that I wanted it to. I suppose things are going alright, now I'm guessing I just declare my variables from the txt, and do the calculations to get the mean and standard deviation.
Here's what I wasn't sure of though. When you enter in C++ code that says it's to read a file, do I need a SPECIFIC file path, or will it literally search the whole computer until it finds the specified .txt?
#13
Re: A little confused with I/O with files.
Posted 01 October 2010 - 04:15 PM
Munchiester, on 01 October 2010 - 05:12 PM, said:
Oler1s, on 01 October 2010 - 03:09 PM, said:
Quote
Well it seems there's 2 different ways of doing it I suppose. I read up on the tutorial posted above, but there's also lessons by my prof. I used the one in the tutorial and was sucessful, it read and displayed the text file that I wanted it to. I suppose things are going alright, now I'm guessing I just declare my variables from the txt, and do the calculations to get the mean and standard deviation.
Here's what I wasn't sure of though. When you enter in C++ code that says it's to read a file, do I need a SPECIFIC file path, or will it literally search the whole computer until it finds the specified .txt?
I never have to specify the file path.
It usually saves the file to where the exe is located.
This post has been edited by Alex6788: 01 October 2010 - 04:17 PM
#14
Re: A little confused with I/O with files.
Posted 01 October 2010 - 04:21 PM
Alex6788, on 01 October 2010 - 03:15 PM, said:
Munchiester, on 01 October 2010 - 05:12 PM, said:
Oler1s, on 01 October 2010 - 03:09 PM, said:
Quote
Well it seems there's 2 different ways of doing it I suppose. I read up on the tutorial posted above, but there's also lessons by my prof. I used the one in the tutorial and was sucessful, it read and displayed the text file that I wanted it to. I suppose things are going alright, now I'm guessing I just declare my variables from the txt, and do the calculations to get the mean and standard deviation.
Here's what I wasn't sure of though. When you enter in C++ code that says it's to read a file, do I need a SPECIFIC file path, or will it literally search the whole computer until it finds the specified .txt?
I never have to specify the file path.
Alright thanks. I'm guessing it must search for that specific file on the system and it's there or it's not I suppose? I'm sorry for all the questions, I just really like understanding the why and how, rather than doing this assignment. Right now I could care less if I get it done, I'd rather learn everything I need to know to complete it, rather than cheat in some way and not understand anything.
Here's my code so far:
#include <iostream>
#include <fstream>
#include <string> // used for getline and string
using namespace std;
int main()
{
ifstream in("cities.txt");
if(!in.is_open())
exit(1);
string line = ""; // The file will buffer every line into this string
while(getline(in,line)) // loop through the file
{
cout<<line<<endl; // output every line to the screen
}
in.close();
//pause window
cin.ignore();
cin.get();
return 0;
}
It's pretty much like from the tutorial except obviously I've replaced it with my own text file. Now here comes another question. Like I said previously, I need to use set width. Will I have to do this with every single variable I declare? Or is there a way to make a setw constant throughout my entire code?
#15
Re: A little confused with I/O with files.
Posted 01 October 2010 - 04:40 PM
edit: also if you want to fill it with a specific charter then use stream<<setfill(somechar) or stream.fill(somechar).
This post has been edited by ishkabible: 01 October 2010 - 04:42 PM
|
|

New Topic/Question
Reply




MultiQuote







|