Calling data from a file

I believe that I have my code ready to go without any errors. The problem I'm encountering is that when I'm entering the data file that is to be linked to this program, it isn't being found. The data file that is to be linked to this program is weatherdata.txt. Am I missing a piece of code which lets the compiler know that this is the file I am to be using to determine average wind speed and temperature for a 30 day period? Thank you for helping out.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  #include <iostream>
#include <fstream>
#include <string>
#include<iomanip>
#include<stdlib.h>

using namespace std;
int main ()
{

char filename[20];
ifstream inFile;
float speed,temperature,avgspeed=0,avgtemp=0;
int count,getcount;

    cout << "Enter the file name\n;";
    cin >> filename;

    inFile.open(filename);//open the file
    if (inFile.fail())//error handling
    {
    cout<<"Input filename cannot be found\n";
    exit(1);
    }

    inFile>>count>>speed>>temperature;//file details

    while(inFile.good())
    {
    //calculate on the go
    avgspeed=avgspeed+speed;
    avgtemp=avgtemp+temperature;
    getcount=count;
    cout<<count<<" "<<speed<<" "<<temperature<<endl;
    inFile>>count>>speed>>temperature;
    }

    inFile.close();//close the file after reading
    avgspeed=avgspeed/getcount;
    avgtemp=avgtemp/getcount;
    cout << fixed; //set the precision after decimal to 2 values
    cout <<setprecision(2);
    cout<<"The average of the wind speed in the file is "<<avgspeed<<endl;
    cout<<"The average of the temperatures in the file is "<<avgtemp<<endl;

return 0;
}
closed account (SECMoG1T)
if the file is on a different directory from the one your program is running in then when you enter your file name you should incude the whole path

e.g if the file is on you desktop in windows:
path == "C:\\Users\\username\\Desktop\\yoour_filename.txt"
This file was just given as a .txt file on Notepad. Should I download the .txt file to my desktop? Also, where would I include the code for the path?
closed account (SECMoG1T)
you should have the file locally on your computer: download it, place it in a directory you like .

e.g if you download it to Documents: when entering filename you should enter the path details.

where would I include the code for the path?

1
2
3
4
///when this runs, enter the path to your file
///"C:\\Users\\your username\\Documents\\weatherdata.txt" ///your path
cout << "Enter the file name\n;";
    cin >> filename;




that simple.

EDIT:
also note your char filename[20]; might not be enough for such a long file name, just change file name to string or:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
///option 1:
std::string filename{};

std::cout << "Enter the file name\n;";
std::cin>>filename;

///option 2:
///if you already know the path; e.g file in documents folder, you can:

std::string path("C:\\Users\\your username\\Documents\\");
std::string filename{};

std::cout << "Enter the file name\n;";
std::cin>>filename;///just enter weatherdata.txt only

//then

inFile.open(path + filename);
Last edited on
Thanks!
I've tried implementing the path and I am still unable to read the file. Any other suggestions?
closed account (SECMoG1T)
in what directory is your file located in?
Topic archived. No new replies allowed.