A little confused with I/O with files.

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

42 Replies - 1871 Views - Last Post: 01 October 2010 - 08:14 PM Rate Topic: -----

#1 Munchiester  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 33
  • Joined: 24-September 10

A little confused with I/O with files.

Posted 01 October 2010 - 02:33 PM

Okay, so my last assingment was something similar to this. Basically this assignment I'm being asked to make a console application that displays the city, the province, the maximum temperature, and the minimum temperature for that city. Now he's provided us with a .txt file, which has that exact data, and in the same format as what the output should look like once it's finished. Now of course I've read the rules and I know that you won't do my homework for me, that would be ridiculous. I'm just wondering aren't I supposed to have a two files, and input and output, and can they be the same?

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.

Is This A Good Question/Topic? 0
  • +

Replies To: A little confused with I/O with files.

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,546
  • Joined: 23-August 08

Re: A little confused with I/O with files.

Posted 01 October 2010 - 02:35 PM

The answer to all of your questions is "of course".
Was This Post Helpful? 0
  • +
  • -

#3 Munchiester  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 33
  • Joined: 24-September 10

Re: A little confused with I/O with files.

Posted 01 October 2010 - 02:41 PM

Okay, thanks for the hasty response Jack. So I'll be able to use the text file as both an input and output? I'm also confused on one more thing, he seems to insist that we use set width for this. Like setw(6) which I don't really see the point of using.
Was This Post Helpful? 0
  • +
  • -

#4 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 219
  • View blog
  • Posts: 592
  • Joined: 04-August 09

Re: A little confused with I/O with files.

Posted 01 October 2010 - 02:42 PM

You could read from the file and store it in an array. Then you could use the array to access the data to find the mean etc. I don't see why a second file would be necessary when you can print the data to the screen.
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is online

  • member icon


Reputation: 4140
  • View blog
  • Posts: 12,883
  • Joined: 25-December 09

Re: A little confused with I/O with files.

Posted 01 October 2010 - 02:45 PM

I would use a different file for input and output so you don't mess up the input file.

the setw() function will allow you to space the different items at a repeatable spacing.

Jim
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,546
  • Joined: 23-August 08

Re: A little confused with I/O with files.

Posted 01 October 2010 - 02:45 PM

setw makes it easy to create fields of a fixed width. It's easier than having to do it by yourself.
Was This Post Helpful? 0
  • +
  • -

#7 Munchiester  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 33
  • Joined: 24-September 10

Re: A little confused with I/O with files.

Posted 01 October 2010 - 02:55 PM

So I could just have an input file and then print that onto the screen? I'm starting to get a picture of what my code should look like. I'm still a bit confused about how I'll be able to use the data that is in the text file when I don't think it will recognize it will it? What I'm getting at is it won't recognize the data will it? I'll have to declare variables correct?
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,546
  • Joined: 23-August 08

Re: A little confused with I/O with files.

Posted 01 October 2010 - 03:09 PM

Yes, of course you'll have to declare variables. You'll have to read data from the file into those variables. You will use those variables to make your calculations. You will write the data to the output file using variables. Programming is all about variables.

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

Was This Post Helpful? 1
  • +
  • -

#9 Munchiester  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 33
  • Joined: 24-September 10

Re: A little confused with I/O with files.

Posted 01 October 2010 - 04:02 PM

Alright I'm following some of the online lectures, and for some reason when I choose to compile and execute just to test it out, it gives me an error. It gives me an error on the line "outputfile.open("cities.txt");". It says outputfile is undeclared. I tried putting the full file path but it wouldn't work.
Was This Post Helpful? 0
  • +
  • -

#10 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: A little confused with I/O with files.

Posted 01 October 2010 - 04:09 PM

Quote

It gives me an error on the line "outputfile.open("cities.txt");". It says outputfile is undeclared.
Well, did you declare outputfile somewhere? outputfile is a variable you created, right?
Was This Post Helpful? 0
  • +
  • -

#11 Alex6788  Icon User is offline

  • kitties == adorable


Reputation: 144
  • View blog
  • Posts: 1,667
  • Joined: 15-July 10

Re: A little confused with I/O with files.

Posted 01 October 2010 - 04:11 PM

View PostMunchiester, on 01 October 2010 - 05:02 PM, said:

Alright I'm following some of the online lectures, and for some reason when I choose to compile and execute just to test it out, it gives me an error. It gives me an error on the line "outputfile.open("cities.txt");". It says outputfile is undeclared. I tried putting the full file path but it wouldn't work.

Post your code in code tags :code: and i can help.
Was This Post Helpful? 0
  • +
  • -

#12 Munchiester  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 33
  • Joined: 24-September 10

Re: A little confused with I/O with files.

Posted 01 October 2010 - 04:12 PM

View PostOler1s, on 01 October 2010 - 03:09 PM, said:

Quote

It gives me an error on the line "outputfile.open("cities.txt");". It says outputfile is undeclared.
Well, did you declare outputfile somewhere? outputfile is a variable you created, right?


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?
Was This Post Helpful? 0
  • +
  • -

#13 Alex6788  Icon User is offline

  • kitties == adorable


Reputation: 144
  • View blog
  • Posts: 1,667
  • Joined: 15-July 10

Re: A little confused with I/O with files.

Posted 01 October 2010 - 04:15 PM

View PostMunchiester, on 01 October 2010 - 05:12 PM, said:

View PostOler1s, on 01 October 2010 - 03:09 PM, said:

Quote

It gives me an error on the line "outputfile.open("cities.txt");". It says outputfile is undeclared.
Well, did you declare outputfile somewhere? outputfile is a variable you created, right?


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

Was This Post Helpful? 0
  • +
  • -

#14 Munchiester  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 33
  • Joined: 24-September 10

Re: A little confused with I/O with files.

Posted 01 October 2010 - 04:21 PM

View PostAlex6788, on 01 October 2010 - 03:15 PM, said:

View PostMunchiester, on 01 October 2010 - 05:12 PM, said:

View PostOler1s, on 01 October 2010 - 03:09 PM, said:

Quote

It gives me an error on the line "outputfile.open("cities.txt");". It says outputfile is undeclared.
Well, did you declare outputfile somewhere? outputfile is a variable you created, right?


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?
Was This Post Helpful? 0
  • +
  • -

#15 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: A little confused with I/O with files.

Posted 01 October 2010 - 04:40 PM

you just set the width of the input and output streams. ouputfile<<setw(x) or outputfile.width(x), where x is a integer both, do the same thing. inputfile<<setw(x) or inputfile.width(x) would also work for input. hope this helps :)

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

Was This Post Helpful? 1
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3