Mám problém . Udělal jsem si program ,který mi do souboru zapíše data . A z jiného programu , který je určen ke čtení toho souboru vybrat ty data a udělat z nich proměnou a dále s nimi pracovat.
Nevíte jak na to ?
Fórum › C / C++
Jde vyvolat ze souboru proměnná

To KIIV [: No nevím jak to myslíš .Tohle je kód na zapsání :
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
cout << " Zadejte název torrentu : ";
char nazevTorentu[80];
cin.getline(nazevTorentu, 80);
cout << " Datum vytvoreni day: ";
int day;
cin.clear();
cin >> day;
cout << " Datum vytvoreni mesic : ";
int month;
cin >> month;
cout << " Datum vytvoreni rok : ";
int year;
cin >> year;
int dayNew = day + 14;
if(dayNew > 31)
{
int dayNewNew = 30 - dayNew;
dayNewNew = dayNewNew * -1;
int monthNew = month + 1;
cout << " Torrent se muzete stahnout za 14 dni coz je "<<dayNewNew<<"."<<monthNew<<"."<<year<<"\n";
ofstream soubor1;
soubor1.open("torrent.txt", ios::app);
soubor1<<" "<<dayNewNew<<"."<<monthNew<<"."<<year<<"\t"<<nazevTorentu<<"\n";
soubor1.close();
}
else
{
cout << " Torrent si můžete stáhnout za 14 dní což je "<<dayNew<<"."<<month<<"."<<year<<"\n";
ofstream soubor2;
soubor2.open("torrent.txt", ios::app);
soubor2<<" "<<day<<"."<<month<<"."<<year<<"\t"<<nazevTorentu<<"\n";
soubor2.close();
}
system("PAUSE");
return 0;
}
nevim jestli jsem te spravne pochopil, ale pravdepodobne bych toto resil pohodlneji pres binarni soubory - vytvoril si strukturu (jeji elementy jsou smyslene)
struct torrentInfo{
char tor_name;
int tor_year;
int tor_size;
};
V kodu bych ji pote jen naplnil a celou ji zapsal do souboru, ktery otevres v binarnim rezimu
soubor.open ("/foo/bar", ios::app | ios::binary);
pomoci write (http://cplusplus.com/reference/iostream/ostream/write/)
soubor.write (struktura, sizeof (struktura));
Kdyz chces pak ze souboru ziskat data, staci precist jednou "sizeof (struktura)" a mel by jsi ziskat potrebna data... Omlouvam se za nejake nesrovnalosti/preklepy/nesmysly, jsem spise orientovany na C a tohle pisu po pameti.
To Grungy : Nevim jak to děláte ,ale pořád v tom mám větší bordel
Nastuduj si slozene datove typy a praci s nimi.hejnallukas napsal:
To Grungy : Nevim jak to děláte ,ale pořád v tom mám větší bordel
Muzes me nasmerovat, popr. napsat neco o teto problematice?Grungy napsal:
Presne tak ako povedal Earl Cash, len si daj veľký pozor na ukladanie smerníkov v štruktúre, lebo adresy sa pri každej alokácii menia.
To Earl Cash : Grungy mysli hlavne neco jako:
struct {
char * text, // .... a podobne
}
ze je tam ulozena pouze adresa a kdyz bys ulozil jen strukturu, tak obnovis adresu, kde ty data byly.. ale ted uz urcite nebudou
Přemýšlel jsem jestli by to nešlo nějak takhle
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
cout << " Chcete zadat nazev dila stisknete 1 : ";
int pokracovani;
cin >> pokracovani;
if (pokracovani == 1)
{
cout <<" Zadejte nazev filmu / hry : ";
char nazevTorrentu;
char nazevTorentu;
int day;
int dayNew;
int dayNewNew;
int month;
int monthNew;
int year;
cin >> nazevTorrentu;
ifstream soubor1;
soubor1.open("torrent.txt",ios::in);
while (!soubor1.eof())
{
soubor1 >> nazevTorentu >>dayNew >> month>> year;
if(nazevTorentu == nazevTorrentu)
{
cout << nazevTorentu <<"\t"<<dayNew<<"."<<month<<"."<<year<<"";
}
else
{
cout <<" NOT FOUND\n";
}
soubor1.close();
}
}
else
system("PAUSE");
return 0;
}
To KIIV : Takže by to fungovalo jo nebo ne ??

Přidej příspěvek
Ano, opravdu chci reagovat → zobrazí formulář pro přidání příspěvku
×Vložení zdrojáku
×Vložení obrázku
×Vložení videa
































Uživatelé prohlížející si toto vlákno
Podobná vlákna
Proměnná v souboru — založil ONDRA
Proměnná z načteného souboru — založil Atheo
Název souboru jako proměnná — založil Honza
PHP proměnná do proměnná Javascript — založil Marek
Jak vyvolat více operací po spuštění události? — založil Machta
Moderátoři diskuze