天天看點

clion打開方式_CLion C ++無法讀取/打開項目目錄中的.txt檔案

clion打開方式_CLion C ++無法讀取/打開項目目錄中的.txt檔案

I have a .txt file in my project directory that I made and populated with data.

directory structure looks like:

/Users/asd/ClionProjects/ProjectWithTemplates/

main.cpp

cmake

twoday.txt

here is my code:

#include

#include

#include

#include

using namespace std;

template

void printArray(const array & arr);

template

double meanArray(const array & arr);

template

double sDeviation(const array & arr);

int main() {

string date1;

string date2;

array day1Temps;

array day2Temps;

array testarr = {75,70,65};

printArray(testarr);

sDeviation(testarr);

cout << "standard deviation of array is: " << sDeviation(testarr) << endl;

cout << "mean of array is: " << meanArray(testarr) << endl;

ifstream inputFile;

inputFile.open("twoday.txt");

if(inputFile.is_open())

{

inputFile >> date1;

inputFile >> date2;

for(int i = 1; i < day1Temps.size(); ++i)

{

inputFile >> day1Temps[i];

}

for (int j = 1; j < day2Temps.size(); ++j) {

inputFile >> day2Temps[j];

}

} else cout << "File unable to open. File does not exist." << endl;

return 0;

}

template

void printArray(const array & arr){

for(const auto & i : arr)

{

cout << i << " ";

}

cout << endl;

}

template

double meanArray(const array & arr){

double sum;

for (const auto & i : arr) {

sum+=i;

}

return sum/arr.size();

}

template

double sDeviation(const array & arr){

double mean = meanArray(arr);

double total;

for (const auto & i : arr){

total+=pow(i - mean, 2);

}

return sqrt(total/arr.size());

}

Eveything else works fine besides my file IO. Very strange.

adding some details..............more details? :(

解決方案

if inputFile.is_open() always returns false, inputFile.open("twoday.txt"); is not opening the file correctly, presumably because it can't find "twoday.txt"

Try setting an explicit path like "c:/path/twoday.txt" or "/path/twoday.txt" if you're using Linux. You could also try writing a file instead to see where it shows up, or something more exotic to return the current path.