not sure what you want to do with the data once you read it, but this will put each line into a vector. Also note, for this to work your need to change argc…doing this in Xcode you go to product menu, then scheme, edit scheme, click the + and type in the path to your file
#include <iostream>
#include <iomanip> // for setw() and ws
#include <string>
#include <fstream>
#include <cstdlib>
#include<vector>
using namespace std;
/* function prototype(s) */
void addContents(ifstream&);
int main(int argc, char* argv[])
{
if (argc < 2) {
cerr << "Usage: " << argv[0] << "/path/to/your/file" << endl;
exit (1);
}
ifstream datafile {argv[1]}; /* first arg is filename */
if (!datafile.is_open()) {
cout << "Can't read input from " << argv[1] << endl;
exit (1);
}
addContents(datafile);
datafile.close();
return 0;
}
void addContents(ifstream& ff){
vector<string> fileContents;
string currentLine;
while(getline(ff, currentLine)){
fileContents.push_back(currentLine);
}
for (int i = 0; i<fileContents.size(); i++){
cout << fileContents.at(i) << endl;
}
}
solved I’m having trouble creating a function to read in a file [closed]