Hi all, I have the following code written for work. The point of the program is to take input from the user as to which customer id (phone number) is required. Once the program has this data, it will open a specified folder to search through each line of code in a specified spot to find the user id using

substr(x, y)

All lines that contain that data are copied into a string and this string is written to another external file.

What I am trying to do is make the file choosing dynamic (be able to access a different external file each run without tweaking the code, as well as creating a different external file to write to depending on the user id chosen). Any help is greatly appreciated!

So far, I have the code doing the following (I tried to show only relevant code, but you can ask if you would like to see more/have some help with what I did):

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
//get user id to search for, instantiate necessary variables
ifstream readFile("example.txt");  //opens a file for reading, I would like the be able to specify exactly which file each run
ofstream writeFile;
writeFile.open("example2.txt"); //opens a file for data to be written to, I would like to create a file specific to the user and data each run
return 0;

For now, I have been creating text files, can I also create the file as a .CSV?

Thank you very much in advance for your help everyone! :icon_smile:

Make the filename a variable and then allow the user to input some or all of the variables data. Then pass that variable to the open() method or the constructor of the pertinent file stream. Note that arguments for filestream variables need to be a C style string, not an STL string. Given the ability to convert STL strings to C style strings using the c_str() this isn't a significant restriction.

Yes, you can create the file as a comma separated file (assuming that's what you meant by the CSV extension).

This article has been dead for over six months. Start a new discussion instead.