been trying to fix this without help for awhile but am struggling, could someone explain why my code just refuses to run the line that its being asked to run? thanks
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
void fileOP(std::ifstream& testdata, std::string& line, std::string& csvitem, int linenumsought, int linenum, std::string& address);
int main() {
std::ifstream testdata;
testdata.open("testdata.txt");
std::string line, csvitem;
int linenum = 0;
int linenumsought;
std::string address; // Variable for later use
std::cout << "Please enter address: ";
std::cin >> linenumsought; // Using an int for now so I can just enter the line number
fileOP(testdata, line, csvitem, linenumsought, linenum, address);
return 0;
}
void fileOP(std::ifstream& testdata, std::string& line, std::string& csvitem, int linenumsought, int linenum, std::string& address) {
if (testdata.is_open()) {
while (std::getline(testdata, line)) {
linenum++;
std::cout << "Reading line " << linenum << ": " << line << std::endl; // Debugging output
if (linenum == linenumsought) {
std::cout << "Line number matched: " << line << std::endl; // Debugging output
std::istringstream myline(line);
while (std::getline(myline, csvitem, ',')) {
std::cout << csvitem << std::endl;
}
}
}
testdata.close(); // Close the file after reading
} else {
std::cerr << "Unable to open file!" << std::endl;
}
}
I keep getting the error response from std::cerr << "Unable to open file!" << std::endl;
and cant seem to fix it, if you want any more info feel free to ask. Thanks!