Home > Software engineering >  How to read multiple file (file has been named by user) in c
How to read multiple file (file has been named by user) in c

Time:01-05

This is the input file

    file = name  ".txt";
    fileoutput.open(file.c_str());
    fileoutput << name << endl << password << endl;
    fileoutput.close();

So, the file will be named like sarah.txt, kevin.txt and so on.

How to read and display all the data in that multiple file?

CodePudding user response:

I think there are two parts to your problem: Firstly, "finding" all the files to read, as the names are provided at runtime rather than compile time, and secondly reading all of them.

"Finding" the files

Your code will produce a bunch of different text files, eg sarah.txt and kevin.txt. The program needs to know these names somehow, in between runtimes, and the way I would incorporate this is by using a config file. You could have one file that never changes name, say, "config.txt". Every time you create a new file and write to it, you write the filename to the config file. And then when it is time to load all the files, you simply read the config file. You could have a delimiter, eg. what separates different filenames, as a comma or any other character you know won't come up in the filenames. Example below of "config.txt":

sarah.txt,kevin.txt,michael.txt,

Reading the files

I would use a while loop to get every line from the config file, and inside that while loop, have another while loop to read all the data inside these files. Please have a look at the code below.

First, you open the config file and make sure it opened correctly.

// Open the config file
ifstream configfile;
configfile.open("config.txt");

// Check that the file opened correctly
if(!configfile.is_open()) {
    cout << "Failed to open config file.\n";
    return -1;
};

Then you can read all the filenames using a while loop, and the getline() function gets the next filename if we give it the file and the delimiter.

// Read the filename
while(getline(configfile, filename, ',')) {
    // Read the file
}

Then, in the while loop, you can open a file with the filename.

// Read the contents of the file
ifstream temporaryfile;
temporaryfile.open(filename);

// Check that the file opened correctly
if(!temporaryfile.is_open()) {
    cout << "Failed to open " << filename << "\n";
    continue;
}

Now it's just left to read the file.

// Read the file
string content;
while(getline(temporaryfile, content)) {
    cout << content << "\n";
}

And then we can close the files. The full code for reading is below.

#include <iostream>
#include <vector>
#include <string>
#include <fstream>

using namespace std;

int main(void) {
    
    // Open the config file
    ifstream configfile;
    configfile.open("config.txt");

    // Check that the file opened correctly
    if(!configfile.is_open()) {
        cout << "Failed to open config file.\n";
        return -1;
    };

    // Read the config file
    string filename;
    while(getline(configfile, filename, ',')) {
        
        // Read the contents of the file
        ifstream temporaryfile;
        temporaryfile.open(filename);

        // Check that the file opened correctly
        if(!temporaryfile.is_open()) {
            cout << "Failed to open " << filename << "\n";
            continue;
        }

        // Read the file
        string content;
        while(getline(temporaryfile, content)) {
            cout << content << "\n";
        }

        // Close the file
        temporaryfile.close();

    }

    // Close the file
    configfile.close();

}

I'm guessing you also want to be able to separate the password and name in each file. You could do the same that I did with getline(), except you do it on every text file instead of the config file. You just need to make sure to use a delimiter that won't be used as a password or username, like a newline character ('\n').

Writing to the config file

Writing to the config file would be easy. Just append the new filename, for example, the variable file you provided in your code, followed by a comma. See the example below.

// Open the config file
ofstream configfile
configfile.open("config.txt", ios::app); // IOS:APP stands for append, meaning we add to the end of the file.

// Check that the file opened correctly
if(!configfile.is_open()) {
    cout << "Failed to open config file.\n";
    return -1;
};

// Write the filename to the end of the config file.
configfile << file << ',';

// Close the file.
configfile.close();

If you have any more questions, don't hesitate to ask!

  • Related