Problem mit ifstream
-
was mich aber wundert ist, dass selbst nach nem neustart noch die werte da drinne sind
-
Keine Ahnung obs gehen wird:
int anzahlEvents(char *file) { //ermitteln wie viele events int anzahl=1; char werte; ifstream stream; stream.clear(); stream.sync(); stream.open(file,ios::in); if(stream.fail()) { cout<<"\n\n\nKonnte datei nicht öffnen\n\n\n\n"; } else { while(stream.eof()) { werte=stream.get(); if(werte == '\n') { anzahl++; } } } return anzahl; }
cu max
-
also so gehts nicht weil sonst das file nicht gefunden werden kann, wieso auch immer
-
Zeig mal dein gesamtes programm
PS: Ich bin mir eigentlich sicher das es mit stream.sync() gehen sollte
cu max
-
#include <iostream> #include <fstream.h> #include <math.h> #include <string.h> #include <stdio.h> //Die Struktur fürs Event struct Event { char name[5]; int periode; char action_id[5]; int exec_time; int priority; int block_time; int deadline; Event(); Event(char *nam,int period,char *action,int exec,int priorit,int block,int dead); Event *readFile(char* file); //einlesen void ausgabe(Event *tab); //ausgeben ~Event(); //DEstruktor int * tabTest(Event* tab, int anzahl); //komplette Tabelle testen int eventTest(Event* tab, int temp, int anzahl); //einzelnes ereignis testen }; //wie viele events int anzahlEvents(char *file); //Beginn von main int main() { Event*tab; int anz_events=anzahlEvents("tabelle.txt")+1; int auswahl=0, zahl=0; tab=tab->readFile("tabelle.txt"); //user-menu do { cout<<"**********************************************************\n"; cout<<"Bitte waehlen Sie aus!\n"; cout<<"Die Tabelle ausgeben (1)\n"; cout<<"Einen bestimmten Event ueberpruefen (2)\n"; cout<<"Die gesamte Tabelle ueberpruefen (3)\n"; cout<<"Das Programm beenden (4)\n"; cout<<"**********************************************************\n"; cin>>auswahl; switch(auswahl) { case 1: tab->ausgabe(tab); break; case 2: cout<<"Bitte geben Sie an, welchen Event Sie ueberpruefen wollen: \n"; cin>>zahl; if(zahl<=anz_events) { tab->eventTest(tab,zahl,anz_events); } else { cout<<"Sie haben ein nicht vorhanden Event angewählt\n"; } break; case 3: tab->tabTest(tab,anz_events); break; case 4: return 0; default: cout<<"Sie haben sich verdrückt, bitte nochmal versuchen!\n"; } }while(auswahl != 4); //speicher freigeben delete[]tab; return 0; } //ende Main //beginn funktionen Event::Event() { //std konstruktor } Event::Event(char *nam, int period,char *action, int exec,int prior, int block, int dead) { //konsturktor //zuweisen der richtigen parameter strcpy(name,nam); periode=period; strcpy(action_id,action); exec_time=exec; priority=prior; block_time=block; deadline=dead; } Event * Event::readFile(char *file) { //einlesen des files int anzahl=anzahlEvents(file); Event * tabelle = new Event[anzahl+1]; ifstream stream; stream.clear(); stream.open(file,ios::in); if(stream.fail()) { cout<<"Datei nicht gefunden"; } else { for(int i=0;i<=9;i++) { //speichern der werte in ein array tabelle stream>>tabelle[i].name; stream>>tabelle[i].periode; stream>>tabelle[i].action_id; stream>>tabelle[i].exec_time; stream>>tabelle[i].priority; stream>>tabelle[i].block_time; stream>>tabelle[i].deadline; } } //stream.sync(); stream.close(); return tabelle; } void Event::ausgabe(Event *tabelle) { //ausgabe des files int anzahl=anzahlEvents("tabelle.txt"); //maske fuer ausgabe cout<<"Event | Periode | Action_ID | Exec_Time | Priority | Blocking_Delay | Deadline\n"; cout<<"*****************************************************************************\n"; for(int i=0;i<=anzahl;i++) { cout<<tabelle[i].name; cout<<" |\t"; cout<<tabelle[i].periode; cout<<" |\t"; cout<<tabelle[i].action_id; cout<<" |\t"; cout<<tabelle[i].exec_time; cout<<" |\t"; cout<<tabelle[i].priority; cout<<" |\t"; cout<<tabelle[i].block_time; cout<<" |\t"; cout<<tabelle[i].deadline; cout<<" "; cout<<"\n\n"; } } int *Event::tabTest(Event *tabelle, int anzahl) { int * plan = new int[anzahl]; for(int i=0;i<anzahl;i++) { plan[i]=tabelle->eventTest(tabelle,i,anzahl); } return plan; } //der eigentliche algorithmus int Event::eventTest(Event *tabelle, int temp, int anzahl) { //ausrechnen von h1 nach algo double h1=tabelle[temp].exec_time+tabelle[temp].block_time; double hn=0.0; int anzahl_hn=1; double effNutzTime, border, borderFirst; for(int i=0;i<anzahl;i++) { //überprüufen auf anforderungen des algos if((tabelle[i].priority <= tabelle[temp].priority) && (i!=temp)) { if(tabelle[i].periode >= tabelle[temp].deadline) { //h1 ausrechnen h1=h1+tabelle[i].exec_time; } else { //hn ausrechnen hn=hn+(tabelle[i].exec_time) / (tabelle[i].periode); anzahl_hn++; } } } //algo anwenden effNutzTime = hn + h1/tabelle[temp].periode; borderFirst = (tabelle[temp].deadline) / (tabelle[temp].periode); if(borderFirst >= 0.0 && borderFirst <=0,5) { border = borderFirst; } else if(borderFirst > 0.5 && borderFirst <=1.0) { border = anzahl_hn*(pow((2.0*borderFirst),(1.0/anzahl_hn))-1.0) +1.0 - borderFirst; } else { cout<<"Ein Grenze liegt ausserhalb des zulässigen Bereichs\n"; return -1; } if(effNutzTime <= border) { cout<<"Das Ereignis "<<temp<<" ist planbar\n"; return 1; } else { cout<<"Das Ereignis "<<temp<<" ist nicht planbar\n"; return 0; } } Event::~Event() { //std destruktor } int anzahlEvents(char *file) { //ermitteln wie viele events int anzahl=1; char zeichen; ifstream stream; stream.clear(); //stream.sync(); stream.open(file,ios::in); if(stream.fail()) { cout<<"\n\n\nKonnte datei nicht oeffnen\n\n\n\n"; } else { while(stream.eof()) { zeichen=stream.get(); if(zeichen == '\n') { anzahl++; } } } anzahl=10; stream.close(); return anzahl; }
-
Hi,
ich habe es gerade mal überflogen, du must in einer do while schleife auch das selbe mit cin machen:cin.clear(); cin.sync();
Werde es mal anschauen, bin aber selber noch Noob.
cu max
-
nein ich weiss woran es liegt, nur habe ich keine ahnung warum das so ist.
in der letzten funktion anzahlEvents();
gibt er ja anzahl zurueck.
in anzahl steht allerdings immer eine 2 drinne! wenn ich anzahl manuell einen wert groesser 2 gebe dann gehts, also wenn ich 9 eingebe findet der alle meine 10 zeilen, warum das so ist frag gott
-
Diese Anwendung wird aufgrund eines ungültigen vorgangs geschlossen.
Tut mir leid, ich kann dir wohl eher nicht helfen.
cu max
-
while(stream.eof())
Tue etwas solange über die Datei hinausgelesen wurde
Irriteirt mich nun ein wenig. Sollte es nicht heissen:
Tue etwas solange NICHT über das Dateiende hinausgelesen wurde?
-
while(!(stream.eof())) { //... }