-3

I know that there are many topics about it but i've got a problem.

I have a code like this.

    void ADDAso()
    {
    fstream file;
    int n, number=0;
    file.open("Asortymenty.txt", ios::in| ios::out| ios::app);
    string line;
    while(getline(file, line)) number++;
    cout<<"Ile Asortymentow chcesz wprowadzic do bazy: ";
    cin>>n;
    DaneAso tab_aso[n];
    if(file.good())
    {
        int i=0;
        do
        {
            cout<<"\nWporwadz Kod Asortymentu "<<i+1<<": ";
            cin>>tab_aso[i].kod;
            cout<<"Wprowadz Nazwe Asortymentu "<<i+1<<": ";
            cin>>tab_aso[i].nazwa;
            cout<<"Wprowadz Jednostke miary "<<i+1<<":";
            cin>>tab_aso[i].jm;
            file <<number++<<";";
            file <<tab_aso[i].kod<<";";
            file <<tab_aso[i].nazwa<<";";
            file <<tab_aso[i].jm<<";"<<endl;
            i++;
        }
        while(i<n);
    }
    else cout<<"\tFILE IS BROKEN!!\n";
    plik.close();
    system("pause");
    }

End after I try to use it, it says that file is broken. Main thing is to read existing file, count it's lines and add new line with current number.


@molbdnilo

Here is my whole code. Please show me what am i supposed to change.

#include <iostream>
#include <fstream>
#include <windows.h>
#include <conio.h>
#include <vector>

using namespace std;


struct DaneAso
{
    char kod[10];
    char nazwa[50];
    char jm[3];
};
struct Stany
{
    char ilosc[10];
};
void menu()
{
    cout<<"\t\t\tMENU GLOWNE\n\n\n";
    cout<<"\t\t1. Wprowadz Asortyment do systemu\n";
    cout<<"\t\t2. Wyswietl baze Asortymentow\n";
    cout<<"\t\t3. Wstaw Towar na magazyn\n";
    cout<<"\t\t4. Wydaj Towar z magazynu\n";
    cout<<"\t\t5. Obecne stany magazynowe\n";
    cout<<"\t\t6. Wyjdz\n";
}

void DodajAso()
{
    fstream plik;
    int n, numer=0;
    plik.open("Asortymenty.txt", ios::in| ios::out| ios::app);
    cout<<"Ile Asortymentow chcesz wprowadzic do bazy: ";
    cin>>n;
    DaneAso tab_aso[n];
    if(plik.good())
    {
        string linia;
        while(getline(plik, linia)) numer++;
        int i=0;
        do
        {
            cout<<"\nWporwadz Kod Asortymentu "<<i+1<<": ";
            cin>>tab_aso[i].kod;
            cout<<"Wprowadz Nazwe Asortymentu "<<i+1<<": ";
            cin>>tab_aso[i].nazwa;
            cout<<"Wprowadz Jednostke miary "<<i+1<<":";
            cin>>tab_aso[i].jm;
            plik <<numer++<<";";
            plik <<tab_aso[i].kod<<";";
            plik <<tab_aso[i].nazwa<<";";
            plik <<tab_aso[i].jm<<";"<<endl;
            i++;
        }
        while(i<n);
    }
    else cout<<"\tPLIK ZOSTAL USZKODZONY!!\n";
    plik.close();
    system("pause");
}
void WyswietlAso(string Asortymenty)
{
    char linia[50],slowo,litera;
    bool znak=0;
    ifstream otworz;
    cout<<"Numer           Kod             Nazwa           Jednostka Miary"<<endl;
    otworz.open(Asortymenty);
    while(!otworz.eof())
    {
        for(int i=0;i<4;i++)
        {
            for(int j=0;j<23;j++)
            {
                otworz >> litera;
                if(litera!=';')
                    cout << litera;
                else
                    for (int k=j;k<16;k++)
                    {
                        cout << " ";
                        j=24;
                    }
            }
        }
        cout << endl;
    }
    otworz.close();
    cout << endl<<endl;
    system("pause");
}

void DodajStan(string Asortymenty, string stany)
{
    bool znak=0;
    char linia[50],slowo,litera;
    ifstream otworz;
    cout<<"Numer           Kod             Nazwa           Jednostka Miary"<<endl;
    otworz.open(Asortymenty);
    while(!otworz.eof())
    {
        for(int i=0;i<4;i++)
        {
            for(int j=0;j<23;j++)
            {
                otworz >> litera;
                if(litera!=';')
                    cout << litera;
                else
                    for (int k=j;k<16;k++)
                    {
                        cout << " ";
                        j=24;
                    }
            }
        }
        cout << endl;
    }
    otworz.close();
    cout << endl<<endl;
    cout<<"\nWybierz z listy ktory asortyment chcesz wprowadzic na magazyn\n\n";
    fstream plik;
    plik.open("stany.txt", ios::in| ios::out| ios::app);
    if(plik.good())
    {

    }
    plik.close();
    system("pause");
}
void ZdejmijStan()
{

}
void WyswietlStan(string stany)
{
    char linia[50],slowo,litera;
    bool znak=0;
    int index=0;
    ifstream otworz;
    otworz.open("stany.txt");
    while(!otworz.eof())
    {
        for(int i=0;i<4;i++)
        {
            for(int j=0;j<23;j++)
            {
                otworz >> litera;
                if(litera!=';')
                    cout << litera;
                else
                    for (int k=j;k<22;k++)
                    {
                        cout << " ";
                        j=24;
                    }
            }
        }
        cout << endl;
    }
    otworz.close();
    cout << endl<<endl;
    system("pause");
}

int main()
{
    SetConsoleTitleA("Ksiega Inwentarzowa");
    string wybor;
    bool running = true;
    while(running)
    {
        cout<<"Program wspomagajacy zarzadzanie ksiega inwentarzowa\n\n\n";
        menu();
        cout<<"\n\n\tWybierz co chcesz zrobic: ";
        cin>>wybor;
        if(wybor == "6")
        {
            cout<<"\n\n\tDziekuje za korzystanie z programu\n\n";
            system("pause");
            return 0;
        }
        else if(wybor == "1")
        {
            DodajAso();
        }
        else if(wybor == "2")
        {
            WyswietlAso("Asortymenty.txt");
        }
        else if(wybor == "3")
        {
            DodajStan("Asortymenty.txt", "stany.txt");
        }
        else if(wybor == "4")
        {
            ZdejmijStan();
        }
        else if(wybor == "5")
        {
            WyswietlStan("stany.txt");
        }
        else
        {
            cout<<"\tWybrales zla opcje, wcisnij poprawny przycisk\n\n";
            system("pause");
        }
    system("cls");
    }
}
ChrisF
  • 134,786
  • 31
  • 255
  • 325
Trewur
  • 5
  • 2
  • 1
    Note that `tab_aso[n]` requires non-standard [variable length arrays](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) because the value of `n` must be known at compile time, not runtime. Instead prefer `std::vector tab_aso(n);` in this case. – Cory Kramer Feb 09 '22 at 12:42
  • It's unclear what you are trying to do. The question is "Counting lines in text file" but there is much more going on in this code. Please edit your question to clarify. – Thomas Feb 09 '22 at 12:48
  • @CoryKramer after adding vector i still have a file is broken message. – Trewur Feb 09 '22 at 12:52
  • 1
    The `while(getline(file, line))` loop continues until the file isn't "good" anymore. It doesn't magically return back to "good" when you exit the loop. – molbdnilo Feb 09 '22 at 13:05
  • What is the point of having a whole array `tab_aso;? You overwrite each element, write it to file immediately, and then you ignore that element for the rest of the program. – molbdnilo Feb 09 '22 at 13:09
  • @molbdnilo so, when i move while into the if, i couldn't save the file. – Trewur Feb 09 '22 at 13:09
  • @Trewur You still need to clear the error state. There aren't separate "goodnesses" for reading and writing. – molbdnilo Feb 09 '22 at 13:11
  • @molbdnilo tab_aso is a structure. It suppose to be a table. – Trewur Feb 09 '22 at 13:13
  • @Trewur So the plan is to eventually use the array for something after writing its elements to the file? – molbdnilo Feb 09 '22 at 13:15
  • @molbdnilo Yes, i just doesn't go that far. – Trewur Feb 09 '22 at 13:17
  • Your code would be easier for other people to understand if you provided a [mre] in English language. – Andreas Wenzel Feb 09 '22 at 13:52

1 Answers1

0

I've managed to do it.

All i had to do is use file.clear(); after while function.

Thanks for your help!

ChrisF
  • 134,786
  • 31
  • 255
  • 325
Trewur
  • 5
  • 2