0

Please can you advise, why the inner loop runs only once? I'd like to add suffix to each line of input file and then store the result in output file.

thanks

For example: Input file contains:

AA
AB
AC

Suffix file contains:

_1
_2

Output file should contain:

AA_1
AB_1
AC_1
AA_2
AB_2
AC_2

My result is :

AA_1
AB_1
AC_1

Code:

int main()
{
    string line_in{};
    string line_suf{};
    string line_out{};
    ifstream inFile{};
    ofstream outFile{"outfile.txt"};
    ifstream suffix{};

    inFile.open("combined_test.txt");
    suffix.open("suffixes.txt");

    if (!inFile.is_open() && !suffix.is_open()) {
        perror("Error open");
        exit(EXIT_FAILURE);
    }

    while (getline(suffix, line_suf)) {
        while (getline(inFile, line_in))
        {
            line_out = line_in + line_suf;
            outFile << line_out << endl;
        }
        inFile.close();
        outFile.close();
    }

}
4
  • 2
    How many times should getline(inFile, line_in) read a new line? Commented Nov 2, 2021 at 19:53
  • If inFile is closed at the end of the first iteration of the outer loop, what's there to read on the next iteration? Rewinding the file with seekg looks like a better idea. Commented Nov 2, 2021 at 19:56
  • @Drew Dormann - getline(inFile, line_in) should read the 2* 3 = 6 times total. 2 entries in the first loop and 3 lines in inner loop: suffix: _1 _2 inFile: AA AB AC Result: AA_1 AB_1 AC_1 AA_2 AB_2 AC_2 Commented Nov 2, 2021 at 20:05
  • Hint: one file will need to be repeatedly rewound. Be aware to clear any flags before rewinding. Commented Nov 2, 2021 at 20:34

1 Answer 1

2

IMHO, a better method is to read the files into vectors, then iterate through the vectors:

std::ifstream word_base_file("combined_test.txt");
std::ifstream suffix_file("suffixes.txt");
//...
std::vector<string> words;
std::vector<string> suffixes;
std::string text;
while (std::getline(word_base_file, text))
{
    words.push_back(text);
}
while (std::getline(suffix_file, text))
{
    suffixes.push_back(text);
}
//...
const unsigned int quantity_words(words.size());
const unsigned int quantity_suffixes(suffixes.size());
for (unsigned int i = 0u; i < quantity_words; ++i)
{
    for (unsigned int j = 0; j < quantity_suffixes; ++j)
    {
        std::cout << words[i] << suffix[j] << "\n";
    }
}

Edit 1: no vectors
If you haven't learned about vectors or like to thrash your storage device you could try this:

std::string word_base;
while (std::getline(inFile, word_base))
{
    std::string  suffix_text;
    while (std::getline(suffixes, suffix_text))
    {
        std::cout << word_base << suffix_text << "\n";
    }
    suffixes.clear();  // Clear the EOF condition
    suffixes.seekg(0);  // Seek to the start of the file (rewind).
}

Remember, after the inner while loop, the suffixes file is at the end; no more reads can occur. Thus the file needs to be positioned at the start before reading. Also, the EOF state needs to be cleared before reading.

Sign up to request clarification or add additional context in comments.

3 Comments

thank you, looks a bit more complicated but it works.. :)
@midas_96: In modern times, reading from memory is faster than reading from a file, and there is enough memory to handle most cases. Long time ago, memory was precious, small and expensive (more expensive than tape drives or other storage devices), so thrashing a file was expected.
Thomas answer is Ok, but it would be nice to guide beginner into writing code with good practices, like splitting code into smaller more readable pieces: wandbox.org/permlink/PitbPMJrKP154FRc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.