stanlee Δημοσ. 14 Ιανουαρίου 2008 Δημοσ. 14 Ιανουαρίου 2008 Καλησπερες... μπορεί καποιος να με βοηθήσει;;; το αρχειο .ΤΧΤ που διαβάζω εχει στην πρωτη γραμμή που διαβάζω αυτά: MThd | Format=1 | # of Tracks=18 | Division=192 εγω θέλω να κρατάω το # of Tracks δηλαδή το 18 και το Division δηλ το 192 και να τα κανω απο stings σε int. o κώδικας που χρισιμοποιώ είναι αυτός.... Στην περίπτωση για το Division η μετατροπή γίνετε μια χαρα. Οταν επαναλαμβάνω ομως την διαδικασία για το # of tracks ενώ βρισκει τον αριθμό 18 στο string δεν μου κανει την μετατροπή απο string σε int. συγνώμη για τα greeklish αλλα είναι αντιγραφή απο τον κώδικά μου. if (!myfile) { cout << "Unable to open " << filename_ << " for reading."<<endl; cout<<"Program exit..."<<endl; getch(); return 0; } else { //////////////////////////////////////////////////////////////////////// //////////////////////////DIABASMA ARXEIOY////////////////////////////// /* while (myfile.getline(line_,100)) { cout << line_<<endl; getch(); } cout << "\n***End of file contents.***\n"; */} //////////////////////FIND DIVISION & No OF TRACKS////////////////////// if (myfile.is_open() ) { string division,tracks; int pos; myfile.getline(line_,100); division=line_;//metatropi char se string tracks=line_; pos = division.find("Division="); if(pos==-1) { cout<<endl; cout<<"------ERROR------"<<endl; cout<<"DIVISION NOT FOUND"<<endl; } else { string temp; int div_,trks_; temp=division.substr(pos+9,3); cout<<"Division="; //metartopi string se int// istringstream ss( temp );// ss >> div_; // cout<<div_<<endl; // /////////////////////////// pos = tracks.find("Tracks="); if(pos==-1) { cout<<endl; cout<<"---------ERROR---------"<<endl; cout<<"No OF TRACKS NOT FOUND"<<endl; } else { cout<<endl; temp=tracks.substr(pos+7,2); cout<<temp<<endl; cout<<endl; cout<<"Tracks="; ss >> trks_; cout<<ss<<endl; cout<<trks_<<endl; } getch(); } }//if Προτάσεις και λυσεις ευπροσδεκτες. Ευχαριστώ
bokarinho Δημοσ. 14 Ιανουαρίου 2008 Δημοσ. 14 Ιανουαρίου 2008 Το παρακάτω πρόγραμμα αποθηκεύει σε ένα vector container που αποθηκεύουμε integers δηλαδή ακεραίους τις τιμές που θέλεις ανοίγοντας ένα αρχείο κειμένου που έχει εγγραφές του τύπου που παραθέτεις. Με την χρήση της substr function της STL παίρνουμε το υποstring που έχει την τιμή που θέλουμε και το αποθηκεύουμε μέσα στον δυναμικό πίνακα ακεραίων που έχει ορισθεί. Τώρα εσύ έχοντας τον πίνακα χρησιμοποιώντας είτε τα κατάλληλα iterators είτε απλά με index processing μπορείς να αντιμετωπίσεις τον vector πίνακα μας ως ένα απλό πίνακα από ακεραίους και να παίρνεις τα περιέχομενα του. Πρόσεχε όμως, σου προτείνω να χρησιμοποιήσεις την μέθοδο .at(nIdx) γιατί αυτή ρίχνει exception σε περίπτωση που το nIdx βγει εκτός ορίων του πίνακα ενώ η απλή συνηθισμένη μέθοδος δεν το ελέγχει. Το πρόγραμμα τρέχει ως εξής: Από το command prompt πήγαινε στο φάκελο που υπάρχει το εκτελέσιμο και το αρχείο και δώσε την εντολή: C:\><Folder>progname.exe filename.txt Πολύ πιθανόν να υπάρχουν bugs δεν έχω και τρομερή εμπειρία στην χρήση της STL. > //--------------------------------------------------------------------------- #include <iostream> #include <fstream> #include <string> #include <vector> #include <algorithm> using namespace std; #pragma hdrstop //--------------------------------------------------------------------------- #pragma argsused int main(int argc, char* argv[]) { /* Index. */ static int VecIndex = 0; /* Values that will be read from file. */ vector<int>nVectorIntValues; /* Create A string. */ string filename, LineStr = ""; /* Create an array of strings. */ vector <string> nVectorStrings; /* For Getline. */ char TBuffer[256] = ""; const char *nSeperator = "="; /* Get Space for filename. */ filename.reserve(64); if(argc != 2) { cout << "Syntax Error: Correct Syntax is: <ProgName> <InputFilename> " << endl; return -1; } else { /* Copy argv[1] into filename. */ filename = argv[1]; /* Open File. */ ifstream inputFile(filename.c_str()); if(!inputFile) { cout << "Error Can Open:" << filename << endl; return -2; } else { while(inputFile.getline(TBuffer, 256)) { int nIdx = 0; LineStr = TBuffer; string::size_type idx = 0; idx = LineStr.find(nSeperator); if(idx == string::npos) { cout << "Invalid Line Type." << endl; break; } else { while((idx = LineStr.find(nSeperator, idx+1)) != string::npos) { /* Init nIdx. */ nIdx = idx+1; /* Advance to find the end of number. */ while(isdigit(LineStr[nIdx])) nIdx++; /* Append the result string into the vector of strings. */ nVectorStrings.push_back(LineStr.substr(idx+1, (nIdx-1) - idx)); /* Transform to int, atoi will do the trick. */ nVectorIntValues.push_back(atoi(nVectorStrings[VecIndex++].c_str())); } } } /* Print Results. */ for(unsigned int i = 0; i < nVectorIntValues.size(); i++) cout << nVectorIntValues[i] << endl; /* Clear Eof Bit. */ inputFile.clear(); /* Destroy Vectors and Free memory. */ nVectorIntValues.~vector<int>(); nVectorStrings.~vector<string>(); } } cout << "Hit enter to continue..." << endl; cin.get(); return 0; } //--------------------------------------------------------------------------- Εκτυπωμένα αποτελέσματα είναι απλά το 18 και το 192 δηλαδή οι δύο αριθμοί που διαβάστηκαν από το αρχείο αλλά και όλοι οι αριθμοί που θα υπάρχουν σε ανάλογες γραμμές στο αρχείο.
bilco Δημοσ. 15 Ιανουαρίου 2008 Δημοσ. 15 Ιανουαρίου 2008 .... string temp; int div_,trks_; temp=division.substr(pos+9,3); cout<<"Division="; //metartopi string se int// istringstream ss( temp );// ss >> div_; // cout<<div_<<endl; // /////////////////////////// pos = tracks.find("Tracks="); if(pos==-1) { cout<<endl; cout<<"---------ERROR---------"<<endl; cout<<"No OF TRACKS NOT FOUND"<<endl; } else { cout<<endl; temp=tracks.substr(pos+7,2); cout<<temp<<endl; cout<<endl; cout<<"Tracks="; ss >> trks_; cout<<ss<<endl; cout<<trks_<<endl; .... Κάνεις δύο λάθη με το stringstream. Όταν διαβάσεις την πρώτη φορά τον ακέραιο από το ss (ss >> div_ το stream φτάνει στο τέλος του (σημαίνεται η eof flag). Απο 'κει και πέρα δεν μπορείς να ξαναδιαβάσεις από το stringstream. Άρα κατ'αρχήν πρέπει να καθαρίσεις τα flags με την ss.clear(); Το ότι δημιούργησες το ss με το string temp δεν σημαίνει ότι αν αλλάξουν τα περιεχόμενα του temp θα αλλάξουν και τα περιεχόμενα του ss. Ο δημιουργός απλά αντιγράφει τον tmp στον read buffer του. Πρέπει να ανανεώσεις τα περιεχόμενα του read buffer για να πάρεις τον δεύτερο ακέραιο (ss.rdbuf()->str(temp) Δηλαδή ο κώδικας στο σημείο που διαβάζεις τα tracks πρέπει να γίνει > temp=tracks.substr(pos+7,2); cout<<temp<<endl; cout<<endl; cout<<"Tracks="; // ΜΟΝΟ ΑΥΤΑ ΠΡΟΣΘΕΣΑ ss.clear(); ss.rdbuf()->str(temp); // END: MONO ΑΥΤΑ ΠΡΟΣΘΕΣΑ ss >> trks_; cout<<ss<<endl; cout<<trks_<<endl;
Προτεινόμενες αναρτήσεις
Αρχειοθετημένο
Αυτό το θέμα έχει αρχειοθετηθεί και είναι κλειστό για περαιτέρω απαντήσεις.