Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 5 của 5
  1. #1
    Ngày tham gia
    Sep 2015
    Bài viết
    0

  2. #2
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Tại sao bạn không dùng std::string mà phải dùng char[], bạn đã viết = C++ dùng fstream class rùi thì mình thiết nghĩ bạn nên dùng std::string thì hay hơn.

  3. #3
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    ẹc đâu có ảnh hưởng đâu, khi mình chọn save file, thì nó đứng im màn hình, nhấn x để tắt thì nó hỏi có lưu ko, chọn yes to all,debug lại,load lại lần 2 không chạy được nữa, vẫn cứ đúng im màn hình
    mình xin đính chính là hàm loaddata bị sai, nhưng mình vẫn chưa tìm ra lỗi

  4. #4
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Bạn chuyển về std::string hết đi cho nó dễ làm, bạn theo cái demo của mình mà chỉnh lại bài của bạn

    Mã:
    #include <iostream>#include <string>#include <fstream>#include <sstream> using namespace std; int to_int( const string& s ){    istringstream i( s );    int data;    i >> data;    return data; } struct Data{    string movie, director, publisher;    int year, number;     friend    istream& operator >>( istream& in, Data& d )    {        string year_str;        string number_str;         getline( in, d.movie );        getline( in, d.director );        getline( in, d.publisher );        getline( in, year_str );        getline( in, number_str );         d.year = to_int( year_str );        d.number = to_int( number_str );         return in;    }     friend    ostream& operator <<( ostream& out, const Data& d )    {        out << d.movie << endl;        out << d.director << endl;        out << d.publisher << endl;        out << d.year << endl;        out << d.number << endl;        return out;    }}; int main( ){    ifstream inf( "data.txt", ios::in );    string empty_line;    Data d;     while( inf >> d )    {        cout << d << endl;        getline( inf, empty_line );     }      inf.close( );     return 0;}

  5. #5

    Lưu dữ liệu bị lỗi gì đây?

    Mình mô tả thế này cho dễ hiểu nhé, mình tạo 1 cái menu
    1. load dữ liệu
    2. save dữ liệu

    khi mình nhấn 1, load thành công, display all đúng
    nhưng khi chọn 2 để lưu, thì màn hình cứ đứng im, mình đang đau đầu chỗ xử lí khoảng trắng, mong mọi người giúp
    Mã nguồn PHP:
    void LinkedListBook::LoadData_book(char cFilebook[]){ ifstream fInb; fInb.open(cFilebook); //check open file if (!fInb) { return; } //bHead = new bNode; bNode * current = bHead; //create variable to hold the input int iTemp; float fTemp; char cTemp[50]; while (!fInb.eof()) { //add bNode at first //because head = NULL so if current = head = NULL and then current = new Node //it cause segmentation fault if (bHead == NULL) { current = new bNode; bHead = current; } else { //add Node at the end ///we must new bNode before current points to next current->bNext = new bNode; current = current->bNext; } //allowcate data current->data_b = new CBook; ///////////////////// fInb.get(cTemp,50,'
    '); fInb.ignore(50,'
    '); current->data_b->SetTitle(cTemp); ///////////////////// fInb.get(cTemp,50,'
    '); fInb.ignore(50,'
    '); current->data_b->SetAuName(cTemp); /////////////////////// fInb.get(cTemp,50,'
    '); fInb.ignore(50,'
    '); current->data_b->SetPublisher(cTemp); /////////////////////// fInb >> iTemp; current->data_b->SetPubYear(iTemp); fInb.get(); /////////////////////// fInb >> fTemp; current->data_b->SetPrice(fTemp); fInb.get(); fInb.ignore();--> doan nay ne,fai them moi load dung,nhung ghi file thi sai /////////////////////////// } current->bNext = NULL; fInb.close();}///////////////////////////////////////void LinkedListBook::SaveData_book(char cFile[]){ ofstream fO; fO.open(cFile,ios::ate); bNode * p = bHead; while(p!=NULL) { fO << "

    "<< p->data_b->GetTitle()<<endl; fO << p->data_b->GetAuName()<<endl; fO << p->data_b->GetPublisher()<< endl; fO << p->data_b->GetPubYear()<<endl; fO << p->data_b->GetPrice(); p = p->bNext; } fO.close();}  
    file text ban đầu của mình
    Mã nguồn PHP:
    Phuc SinhLep Ton XToiNXB Kim Dong1991200Nhiem vu bat kha thiJAME BONDSNXB Van Hoa200021AvatarCameronMegastar2009100.55  

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •