#include <iostream>
#include <conio.h>
#include <fstream>
#include <cstdlib>
#include <windows.h>
using namespace std;
int ile2, ilosc2;
string sciezka, notka;
void gotoxy(const int x, const int y)
{
HANDLE g_hConsol = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord = {x, y};
SetConsoleCursorPosition(g_hConsol, coord);
}
void header()
{
system("cls");
HANDLE kol = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(kol,14);
gotoxy(27,2);
cout << "|------------------------|" << endl;
gotoxy(27,3);
cout << "| Baza danych v1.0 |" << endl;
gotoxy(27,4);
cout << "| by ************** |" << endl;
gotoxy(27,5);
cout << "|------------------------|" << endl;
SetConsoleTextAttribute(kol,7);
}
int pokaz()
{
header();
int index;
string temp2;
string wpis[ilosc2];
cout << "Podaj numer indexu z bazy: ";
cin >> index;
if (index > ile2)
{
cout << "Blad! Podaje ostatni index.";
index = ile2;
}
fstream plik2;
plik2.open(sciezka.c_str());
getline(plik2, temp2);
for (int z = 0; z < index*ilosc2-ilosc2; z++)
{
getline(plik2, temp2);
}
for (int x = 0; x < ilosc2; x++)
{
getline(plik2, wpis[x]);
}
plik2.close();
cout << endl << "Index nr: " << index << endl;
int a = 0;
for (int y = 0; y < ilosc2; y++)
{
cout << wpis[a] << endl;
a++;
}
char c = getch();
if (c == '0')
{
cout << notka;
getch();
}
return 0;
}
int dodaj()
{
header();
string wpis, temp3;
int ktory;
cout << "Jako ktory index dodac? ";
cin >> ktory;
if (ktory >= ile2+2)
{
cout << endl
<< "Blad! Index nie moze byc wiekszy niz: " << ile2+1 << " !"
<< endl
<< "Dodaje jako: " << ile2+1 << " !";
ktory += 1;
}
else if (ktory < 0)
{
cout << endl
<< "Blad! Index nie moze byc mniejszy niz: 0 !"
<< endl
<< "Dodaje jako: " << ile2+1 << " !";
ktory = ile2+1;
}
fstream plik3;
plik3.open(sciezka.c_str());
for (int x = 0; x < (ktory-1)*ilosc2; x++)
{
getline(plik3, temp3);
}
cout << endl << endl << "'/' - by dodac pusty wpis!" << endl << endl;
ofstream plik4;
plik4.open(sciezka.c_str(), ios::app);
for (int x = 0; x < ilosc2; x++)
{
cout << "Podaj wpis: ";
cin >> wpis;
if (wpis == "/"){wpis = "";}
plik4 << wpis;
plik4 << "\n";
}
plik4.close();
return 0;
}
int main()
{
SetConsoleTitle("Baza danych v1.0 by WINNIK");
int klawisz1 = 0;
string temp, temp2, ilosc;
int ile, dlugosc;
cout << "Podaj sciezke bazy danych: ";
cin >> sciezka;
//-------------------------------------MENU--------------------------------------//
while (klawisz1 != 27)
{
HANDLE kol = GetStdHandle(STD_OUTPUT_HANDLE);
header();
fstream pIlosc;
pIlosc.open(sciezka.c_str());
getline(pIlosc, ilosc);
pIlosc.close();
ilosc2 = strtol(ilosc.c_str(), NULL, 10); // konwersja string na int
fstream plik;
plik.open(sciezka.c_str());
ile = 0;
do
{
getline(plik, temp);
notka+=temp;
notka+="\n";
ile++;
}
while(!plik.eof());
plik.close();
dlugosc=notka.length();
ile2 = ile/ilosc2-1;
string indexy[2];
indexy[0] = " index.";
indexy[1] = " indexy.";
indexy[2] = " indexow.";
int b;
if (ile2 == 1){b = 0;}
else if (ile2 < 5){b = 1;}
else {b = 2;}
gotoxy(0,0);
cout << "Baza zawiera: " << ile2 << indexy[b];
cout << endl;
int W, wielkosc_menu;
wielkosc_menu = 2; // zmienna mowiaca o ilosci lini !!! nie sugerowac sie tablica !!!
string menu[wielkosc_menu];
menu[0] = "Pokaz index";
menu[1] = "Dodaj index";
for (W = 0; W < wielkosc_menu; W++)
{
gotoxy(32,7+W);
cout << menu[W];
}
gotoxy(32,11);
SetConsoleTextAttribute(kol,11);
cout << "ESC by wyjsc." << endl;
W = 0;
do
{
SetConsoleTextAttribute(kol,79);
gotoxy(32, 7+W);
cout << menu[W];
SetConsoleTextAttribute(kol,7);
gotoxy(32, 7+W);
klawisz1=getch();
cout << menu[W];
if (klawisz1 == 80)
W++;
if (klawisz1 == 72)
W--;
if (W == -1)
W = wielkosc_menu - 1;
if (W == wielkosc_menu)
W = 0;
}
while(klawisz1 != 13 && klawisz1 != 27);
if (klawisz1 == 27){W = 2;}
if (W == 0){pokaz();}
else if (W == 1){dodaj();}
}
return 0;
}
Prosty program napisany wg. nauk na lekcjach programowania, w Code Blocks nie ma żadnych błędów, Visual wywala ich pełną listę, o co tu do cholery chodzi?