PeterKoszorus
PeterKoszorus

Reputation: 5

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;
}
}

Upvotes: 0

Views: 591

Answers (3)

akib khan
akib khan

Reputation: 451

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++;
    }

}
}

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409462

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;

Upvotes: 0

Mohammed Deifallah
Mohammed Deifallah

Reputation: 1330

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.

Upvotes: 3

Related Questions