我正在尝试在Mac上使用C ++中的C ++读取文件。我通常在课堂上阅读Windows计算机上的文件,这是一个更容易的过程,但在我的Mac上它打印文件的名称而不是文件的内容。
这是我的代码:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
//Function Prototypes for reading grades and calculations
void findMaxGrade (double [], double);
void findMinGrade ( double [], double);
void findAverageGrade (double [], double);
const int GRADES = 10; // Array size
int main()
{
double number [GRADES]; //The Array has 10 elements
ifstream inputFileStream;
//Set up file path
string readGrades = ("/home/Lidah/Desktop/grades.txt");
//Store values in the Array
for (int counter = 0; counter < GRADES ; counter++)
{
number [counter] = counter;
}
// Check if file opened correctly
if (! inputFileStream)
{
cout << " Unable to open file for reading! ";
return 1;
}
//read contents of the file
inputFileStream.open (readGrades); //Come back and fix this format!
for (int counter = 0; counter < GRADES; counter++)
{
cout << readGrades << endl;
}
inputFileStream.close();
return 0;
}
//Define the function
答案 0 :(得分:0)
您实际上并没有读取文件,但是您正在使用文件路径打印字符串。您需要读取输入流,例如:
inputFileStream.open(readGrades);
std::string grade;
while (std::getline(inputFileStream, grade)){
cout << grade;
}