C++ can't write to file

I'm struggling with this code for some time now. To simply put my question I want to read 2 Names from file1 and then write it to the line of the file2. It's reads the name just fine but it doesn't write it to the file2.

#include <iostream>
#include <fstream>
#include <string>
#include <time.h>
int main()
{
int rand1 = 0,line_num = 0;
string Meno,Dni;


fstream ZoznamMien ("C:/Programovanie/Generator_dot/Dotaznik.txt");
fstream VolneDni ("C:/Programovanie/Generator_dot/Dni.txt");

srand((unsigned int)time(NULL));
rand1 = rand() % 16;

for (int i = 0; i < 2; i++)
{
    while (getline(ZoznamMien, Meno))
    {
        if (line_num == rand1)
        {
            getline(VolneDni, Dni);
            if (i == 0)
            {
                Dni = Dni + ' ' + Meno + ',';
            }
            else
            {
                Dni = Dni + ' ' + Meno;

            }
            cout << Dni << endl;
            cout << Meno << endl;
            break;
        }
        line_num++;
    }
        VolneDni << Dni;
}
}

What's the logic beyond this condition: if (line_num == rand1).

It's based on random numbers, so file2 will only be written if this rand1 only has a value of 0 in the first iteration, 1 in the second iteration or 2 at the third iteration.

C Programming Tutorial for Beginners, Learn C the Hard Way: Practical Exercises on the Computational Subjects You Keep Avoiding (Like C) This is a list of operators in the C and C++ programming languages.All the operators listed exist in C++; the fourth column "Included in C", states whether an operator is also present in C. Note that C does not support operator overloading.

Lets take a look at the code where you create and write the new string to the file (with some added comments):

if (i == 0)
{
    // Create the new string
    Dni = Dni + ' ' + Meno + ',';

    // Don't write the new string to the file
}
else
{
    // Create the new string
    Dni = Dni + ' ' + Meno;

    // Write the new string to the file
    VolneDni << Dni;
}

You only write to the string in the else part, not when i == 0.

One solution is to do the writing after the if:

// Create the new string
if (i == 0)
{
    Dni = Dni + ' ' + Meno + ',';
}
else
{
    Dni = Dni + ' ' + Meno;
}

// Write the new string to the file
VolneDni << Dni;

"C" Programming Language: Brian Kernighan, "C" is one of the most widely used programming languages of all time. Prof Brian Kernighan Duration: 8:26 Posted: Aug 18, 2015 C:US-0.86%. Sector. Financials-0.61%. Industry. Banking-1.25%. Sponsored By. About Citigroup Inc Citigroup Inc. is a diversified financial services holding company that provides a broad range of

There are multiple changes. You should check if files are opened successfully or not. Random number should be regenerated each time. Also file should be read from start at each iteration.Write to ouput file before breaking loop.

Working code should be:

#include <iostream>
#include <fstream>
#include <string>
#include <time.h>
using namespace std; 
int main()
{
int rand1 = 0,line_num = 0;
string Meno,Dni;



fstream VolneDni ("C:/Programovanie/Generator_dot/Dni.txt");


for (int i = 0; i < 2; i++)
{
    srand((unsigned int)time(NULL));
    rand1 = rand() % 16;
    fstream ZoznamMien ("C:/Programovanie/Generator_dot/Dotaznik.txt");
    if( !ZoznamMien.is_open() ) {
        cout<<" file did not open "<<endl;
        return 0;
    }
    Dni="";
    while (getline(ZoznamMien, Meno))
    {
        if (line_num == rand1)
        {
            getline(VolneDni, Dni);
            if (i == 0)
            {
                Dni = Dni + ' ' + Meno + ',';
            }
            else
            {
                Dni = Dni + ' ' + Meno;

            }
            cout << Dni << endl;
            cout << Meno << endl;
            VolneDni << Dni;
            break;
        }
        line_num++;
    }

}
}

Learn C, C is a powerful general-purpose programming language. Our C tutorials will guide you to learn C programming one step at a time with the help of examples. C--(pronounced cee minus minus) is a C-like programming language.Its creators, functional programming researchers Simon Peyton Jones and Norman Ramsey, designed it to be generated mainly by compilers for very high-level languages rather than written by human programmers.

Learn C Programming, C programming is a general-purpose, procedural, imperative computer programming language developed in 1972 by Dennis M. Ritchie at the Bell Telephone� C programming is a general-purpose, procedural, imperative computer programming language developed in 1972 by Dennis M. Ritchie at the Bell Telephone Laboratories to develop the UNIX operating system. C is the most widely used computer language. It keeps fluctuating at number one scale of popularity

C Tutorial, C Language - Overview - C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system � C provides a compound assignment operator for each binary arithmetic and bitwise operation (i.e. each operation which accepts two operands). Each of the compound bitwise assignment operators perform the appropriate binary operation and store the result in the left operand.

C Language - Overview, This page contains all topics of C with clear explanations and examples. It also contains job interview questions, MCQ quizzes and output based questions. Learn C# programming - for beginning developers, developers new to C#, and experienced C# / .NET developers

Comments
  • Please, give us a fully working example of your code
  • How do your files look like? What's the content?
  • How do you make sure that if (line_num == rand1) is true so that later write can be executed?
  • I suggest you do some rubber duck debugging. When and where do you write to the stream VolneDni?
  • @NutCracker in the file named ZoznamMien i have 16 lines and in each line i have one name of my clasmates.And in the file named VolneDni i have 8 lines and in each line i have the date of appointment.
  • i moved the writting right after the if (line_num== rand1) statment and it still doesn't work.
  • @PeterKoszorus You forgot to close the file.
  • No i didn't forgot to close the file .
  • @PeterKoszorus What about getline(VolneDni, Dni); ? It reads from the file that's supposed to write to.
  • @PeterKoszorus Anyway, I'd like to suggest that you edit your question with your up-to-date code till now.
  • I moved the VolneDni<<Dni right after the if condition like you told me and it still doesnt write anything to the file