-1

I am trying to do my homework (fairly new to c++) and it requires me to create a program that will read any .csv files from yahoo finance. I know how to read a specific file, but i do not know how to generalize this

I have tried searching on google but nothing really answered my question.

this is what i did to read a specific file

ifstream file;
file.open("GLD.csv");
if (file.is_open()) {
        while (getline(file, line)) {
            cout << line << endl;

we are required to change the csv file in the folder of our main .cpp file, so the file name is not going to be "GLD.csv" all the time. How would i "generalize" the reading of the input to different .csv files? thank you

dbc
  • 104,963
  • 20
  • 228
  • 340
kentcyrel
  • 1
  • 3
  • 1
    https://en.wikipedia.org/wiki/Comma-separated_values#Specification – bolov Mar 23 '19 at 03:13
  • Have you looked at existing questions including [How can I read and parse CSV files in C++?](https://stackoverflow.com/q/1120140)? Please, also read this [Open letter to students with homework problems](https://softwareengineering.meta.stackexchange.com/q/6166). – dbc Mar 23 '19 at 03:26
  • Do you have these .csv files on hand? Or do you have to scrape them from somewhere? – Sailanarmo Mar 23 '19 at 03:42
  • See #1 from @alvinalvord's answer. Then `int i = 1; while (argv[i]) { file.open (argv[i]); ... i++ }` – David C. Rankin Mar 23 '19 at 03:48

1 Answers1

0

I can think of 3 ways to do this:

  1. Use command line arguments. You can get the command line arguments by defining main as int main (int argc, char *argv[]) {} wherein argc is the count of arguments and argv is the values. So when you run the program you can just do program.exe filename.csv
  2. Scan for the input filename. cin >> filename;
  3. Get the current directory and get all the csv files. Here's a link on how to get the files in a directory.
alvinalvord
  • 394
  • 2
  • 11
  • 1
    Helpful to remind that the first argument `argv[0]` is always the program name being executed so the first user supplied argument will start at index `1` (e.g. `argv[1]`). – David C. Rankin Mar 23 '19 at 03:41