Từ 1 tới 6 trên tổng số 6 kết quả

Đề tài: KHÓ QUÁ!!_ Em xin hỏi về cách đọc từ file text ra mảng!!!!!!!!

  1. #1
    Ngày gia nhập
    05 2008
    Bài viết
    0

    Mặc định KHÓ QUÁ!!_ Em xin hỏi về cách đọc từ file text ra mảng!!!!!!!!

    Em co 1file text nội dung như sau:
    15
    10
    .8183892 .4929768
    .4060003 .6464021
    .6673119 .3007983
    .3283856 .4356078
    .7079213 .7496262
    .3988946 .9371529
    .1729512 .0135599
    .4489141 .0562685
    .7066958 .1568812
    .6843121 .5391302
    10
    ....................
    ..................
    Có 10 điểm trong mặt fẳng, 2cột la` toạ độ x,y của chúng. bắt đầu file la` số 15,tức là 15bộ điểm. trc' toạ độ mỗi bộ điêm là số 10(file co 15 bộ số nhu thế)
    Em muốn đọc mỗi toạ độ ra 1 một mảng 1chiều 10phần tử. Người dùng có thể chọn bộ số muốn nhập từ bàn phímh.Giups em voi'.
    Attached Files Attached Files

  2. #2
    Ngày gia nhập
    12 2006
    Nơi ở
    US
    Bài viết
    1,917

    Đây tui viết cho cậu từ A->Z luôn T_T, sorry cậu nếu code hơi dài 1 phần vì tui lâu rùi không code nên ngứa tay, thứ 2 bài này khó chịu, tui cũng cố làm cho nó đơn giản mà không ra gì. Thôi thì làm cho ra làm, cậu đọc chỗ nào không hiểu thì nói tui sẽ giải thích.

    Utility.h
    - Trong đây có 4 hàm, đọc vào 1 line ở mọi định dạng, 1 hàm dừng màn hình,
    1 hàm để mở file đọc vào, 1 hàm để chuyển chuỗi sang số. Vì mấy hàm này nó mang ý nghĩa công cụ hơn nên tui quăng hết nó vào 1 namspace tên là util.
    C++ Code:
    1. #include <fstream>
    2. #include <string>
    3. #include <fstream>
    4. #include <sstream>
    5.  
    6. namespace util
    7. {
    8.     template< typename T >
    9.     T readLine( std::ifstream& is )
    10.     {
    11.         std::string line;
    12.         std::getline( is, line );
    13.         std::istringstream iss( line );
    14.         T result;
    15.         iss >> result;
    16.  
    17.         return result;
    18.     }
    19.  
    20.     void pauseScreen()
    21.     {
    22.         std::cout << "..Hit the enter key to continue.\n";
    23.         std::cin.get();
    24.     }
    25.  
    26.     bool openInputFile( std::ifstream& inf )
    27.     {
    28.         std::string file_name;
    29.         std::cout << "Enter file name : ";
    30.         std::getline( std::cin, file_name );
    31.  
    32.         inf.open( file_name.c_str() );
    33.         if( inf.fail() )
    34.         {
    35.             std::cout << "...Error open file.\n";
    36.             return 0;
    37.         }
    38.  
    39.         std::cout << "Successfully open file.\n";
    40.         return 1;
    41.     }
    42.  
    43.     int convertToInt( const std::string& s )
    44.     {
    45.         int value;
    46.         std::istringstream is( s.c_str() );
    47.         is >> value;
    48.         return value;
    49.     }
    50. }

    Còn đây là class Token, dùng để cắt chuỗi, code này tui lấy xài vì tui tìm không ra các class Token ( cổ điển của tui ) T_T, vì tui thấy class này viết cũng rất tốt, nhưng đọc hơi dài 1 tí thôi, đọc cũng dễ hiểu.
    C++ Code:
    1. #ifndef TOKEN_H
    2. #define TOKEN_H
    3.  
    4. #include <cstdio>
    5. #include <cstdlib>
    6. #include <iostream>
    7. #include <string>
    8.  
    9. class StringTokenizer
    10. {
    11. public:
    12.     StringTokenizer( const std::string& _str, const std::string& _delim );
    13.    ~StringTokenizer()
    14.     {    };
    15.     int         countTokens();
    16.     bool        hasMoreTokens();
    17.     std::string nextToken();
    18.     int         nextIntToken();
    19.     double      nextFloatToken();
    20.     std::string nextToken( const std::string& delim );
    21.     std::string remainingString();
    22.     std::string filterNextToken( const std::string& filterStr );
    23. private:
    24.     std::string  token_str;
    25.     std::string  delim;
    26.  
    27. };
    28.  
    29. #endif

    Token.cpp

    C++ Code:
    1. #include "Token.h"
    2.  
    3. StringTokenizer::StringTokenizer(const std::string& _str, const std::string& _delim)
    4. {
    5.     if( ( _str.length() == 0 ) || ( _delim.length() == 0 ) )
    6.         return;
    7.  
    8.     token_str = _str;
    9.     delim     = _delim;
    10.  
    11.     unsigned int curr_pos = 0;
    12.     while( true )
    13.     {
    14.         if( ( curr_pos = token_str.find( delim, curr_pos ) )
    15.               !=
    16.               std::string::npos
    17.           )
    18.         {
    19.             curr_pos += delim.length();
    20.  
    21.             while( token_str.find( delim,curr_pos ) == curr_pos )
    22.             {
    23.                 token_str.erase( curr_pos, delim.length() );
    24.             }
    25.         }
    26.         else
    27.             break;
    28.     }
    29.  
    30.     if( token_str.find( delim, 0 ) == 0 )
    31.     {
    32.         token_str.erase( 0,delim.length() );
    33.     }
    34.  
    35.     curr_pos = 0;
    36.     if( ( curr_pos = token_str.rfind( delim ) )
    37.           !=
    38.           std::string::npos
    39.       )
    40.     {
    41.         if( curr_pos != ( token_str.length() - delim.length() ) )
    42.             return;
    43.         token_str.erase( token_str.length() - delim.length(), delim.length() );
    44.     }
    45.  
    46. }
    47.  
    48. int StringTokenizer::countTokens()
    49. {
    50.     unsigned int prev_pos = 0;
    51.     int num_tokens        = 0;
    52.  
    53.     if( token_str.length() > 0 )
    54.     {
    55.         num_tokens = 0;
    56.  
    57.         unsigned int curr_pos = 0;
    58.         while( true )
    59.         {
    60.             if ((curr_pos = token_str.find(delim,curr_pos)) != std::string::npos)
    61.             {
    62.                 num_tokens++;
    63.                 prev_pos  = curr_pos;
    64.                 curr_pos += delim.length();
    65.             }
    66.             else
    67.                 break;
    68.         }
    69.         return ++num_tokens;
    70.     }
    71.     else
    72.     {
    73.         return 0;
    74.     }
    75.  
    76. }
    77.  
    78. bool StringTokenizer::hasMoreTokens()
    79. {
    80.     return( token_str.length() > 0 );
    81. }
    82.  
    83. std::string StringTokenizer::nextToken()
    84. {
    85.     if( token_str.length() == 0 )
    86.         return "";
    87.  
    88.     std::string  tmp_str = "";
    89.     unsigned int pos     = token_str.find( delim, 0 );
    90.     if( pos != std::string::npos )
    91.     {
    92.         tmp_str   = token_str.substr( 0, pos );
    93.         token_str = token_str.substr( pos + delim.length(), token_str.length() - pos );
    94.     }
    95.     else
    96.     {
    97.         tmp_str   = token_str.substr( 0, token_str.length() );
    98.         token_str = "";
    99.     }
    100.  
    101.     return tmp_str;
    102. }
    103.  
    104.  
    105. int StringTokenizer::nextIntToken()
    106. {
    107.     return atoi( nextToken().c_str() );
    108. }
    109.  
    110.  
    111. double StringTokenizer::nextFloatToken()
    112. {
    113.     return atof( nextToken().c_str() );
    114. }
    115.  
    116.  
    117. std::string StringTokenizer::nextToken( const std::string& delimiter )
    118. {
    119.     if (token_str.length() == 0)
    120.         return "";
    121.  
    122.     std::string  tmp_str = "";
    123.     unsigned int pos     = token_str.find( delimiter, 0 );
    124.  
    125.     if( pos != std::string::npos )
    126.     {
    127.         tmp_str   = token_str.substr( 0, pos);
    128.         token_str = token_str.substr( pos + delimiter.length(), token_str.length() - pos );
    129.     }
    130.     else
    131.     {
    132.         tmp_str   = token_str.substr( 0, token_str.length() );
    133.         token_str = "";
    134.     }
    135.  
    136.     return tmp_str;
    137. }
    138.  
    139.  
    140. std::string StringTokenizer::remainingString()
    141. {
    142.     return token_str;
    143. }
    144.  
    145.  
    146. std::string StringTokenizer::filterNextToken( const std::string& filterStr )
    147. {
    148.     std::string  tmp_str    = nextToken();
    149.     unsigned int currentPos = 0;
    150.  
    151.     while( ( currentPos = tmp_str.find( filterStr,currentPos ) )
    152.              !=
    153.              std::string::npos
    154.          )
    155.     {
    156.         tmp_str.erase( currentPos, filterStr.length() );
    157.     }
    158.  
    159.     return tmp_str;
    160. }

    Còn đây là lớp chính :
    TenOfTen.h
    C++ Code:
    1. #include <vector>
    2.  
    3. #include "Token.h"
    4. #include "Utility.h"
    5.  
    6. class TenOfTen
    7. {
    8. private :
    9.     /*
    10.         Table of all 2D arrays
    11.     */
    12.     std::vector<
    13.         std::vector<
    14.             std::vector<               
    15.                 std::string > > > table;
    16.     int table_size;
    17. private :
    18.     void printSingleTable( const std::vector< std::vector< std::string > >& v ) const;
    19. public :
    20.     TenOfTen();
    21.     void showData( std::ostream& out ) const;
    22.     void showSpecificValue( const unsigned& what_table,
    23.                             const unsigned& row, const unsigned& col
    24.                           ) const;
    25.     void showRoutine() const;
    26.     void readDataFromFile( std::ifstream& inf );
    27.     bool anotherValue( const char* message ) const;
    28. };



    TenOfTen.cpp
    C++ Code:
    1. TenOfTen::TenOfTen()
    2.     :table_size( 0 )
    3. {   }
    4.  
    5. void TenOfTen::printSingleTable( const std::vector< std::vector< std::string > >& v ) const
    6. {
    7.     for( std::vector< std::vector< std::string > >
    8.             ::const_iterator ot = v.begin();
    9.                              ot != v.end();
    10.                             ++ot
    11.        )
    12.     {
    13.         for( std::vector< std::string >
    14.                 ::const_iterator it = ot->begin();
    15.                                  it != ot->end();
    16.                                  ++it
    17.            )
    18.         {
    19.             std::cout << *it << " - ";
    20.         }
    21.         std::cout << "\n";
    22.     }
    23. }
    24. void TenOfTen::showSpecificValue( const unsigned& what_table,
    25.                                   const unsigned& row, const unsigned& col
    26.                                 ) const
    27. {
    28.     std::cout << "[" << row
    29.               << "]["
    30.               << col << "] = ";
    31.     std::cout << table[ what_table ][ row ][ col ];
    32. }
    33.  
    34. bool TenOfTen::anotherValue( const char* message ) const
    35. {
    36.     std::cout << message;
    37.     std::string user_answer;
    38.  
    39.     while( 1 )
    40.     {
    41.         std::getline( std::cin, user_answer );
    42.    
    43.         if( user_answer[ 0 ] == 'Y' || user_answer[ 0 ] == 'y' )
    44.             return true;
    45.         else if( user_answer[ 0 ] == 'N' || user_answer[ 0 ] == 'n' )
    46.             return false;
    47.         std::cout << "...Invalid option. Try again !\n";
    48.     }
    49.    
    50. }
    51.  
    52. void TenOfTen::showRoutine() const
    53. {
    54.     unsigned what_table;
    55.     unsigned dx,
    56.              dy;
    57.     do
    58.     {
    59.         do
    60.         {
    61.             std::cout << "Pick a table : \n";
    62.             std::cin >> what_table;
    63.             if( what_table < 0 || what_table > table.size() )
    64.             {
    65.                 std::cout << "...Invalid range. Try again !\n";
    66.             }
    67.             else
    68.             {
    69.                 printSingleTable( table[ what_table ] );
    70.             }
    71.         }while( what_table < 0 || what_table > table.size() );
    72.        
    73.         do
    74.         {
    75.             std::cout << "Please input row and col of item : \n";
    76.             std::cin >> dx >> dy;
    77.             if( dx < 0
    78.                 ||
    79.                 dx > table[ what_table ].size()
    80.                 ||
    81.                 dy < 0
    82.                 ||
    83.                 dy > table[ what_table ][ dx ].size()
    84.               )
    85.             {
    86.                 std::cout << "...Invalid range. Try again !\n";
    87.             }
    88.         }while( dx < 0
    89.                 ||
    90.                 dx > table[ what_table ].size()
    91.                 ||
    92.                 dy < 0
    93.                 ||
    94.                 dy > table[ what_table ][ dx ].size()
    95.               );
    96.                
    97.         showSpecificValue( what_table, dx, dy );
    98.  
    99.         std::cin.ignore( 80, '\n' );
    100.     }while( anotherValue( "\nAnother value ( y , n ) ? " ) );
    101. }
    102.  
    103. void TenOfTen::showData( std::ostream& out ) const
    104. {
    105.     unsigned tab;
    106.     out << "TABLE SIZE = " << table.size();
    107.     for( tab = 0; tab < table.size(); ++tab )
    108.     {
    109.         out << "\n--------\n";
    110.         out << "TABLE #" << tab;
    111.         out << "\n-------\n";
    112.         printSingleTable( table[ tab ] );
    113.     }
    114. }
    115.  
    116. void TenOfTen::readDataFromFile( std::ifstream& inf )
    117. {
    118.     std::string                one_line;
    119.     std::string                tmpstr;
    120.     int                        count;
    121.     int                        g_row;
    122.     int                        g_col;
    123.     int                        index;
    124.  
    125.     if( util::openInputFile( inf ) )
    126.     {
    127.         g_row = util::readLine< int >( inf );
    128.         for( int x = 0; x < g_row; ++x )
    129.         {
    130.             g_col = util::readLine< int >( inf );
    131.             std::vector< std::vector< std::string > > level2;
    132.  
    133.             for( int y = 0; y < g_col; ++y )
    134.             {
    135.                 std::getline( inf, one_line );
    136.                 StringTokenizer strtok = StringTokenizer( one_line , " " );
    137.                 count = strtok.countTokens();
    138.                 std::vector< std::string > level1;
    139.  
    140.                 for( index = 0; index < count; ++index )
    141.                 {
    142.                     tmpstr = strtok.nextToken();
    143.                     level1.push_back( tmpstr.substr( 1 ) );
    144.                 }
    145.  
    146.                 level2.push_back( level1 );
    147.             }
    148.             table.push_back( level2 );
    149.         }
    150.     }
    151.     table_size = table.size();
    152. }

    class này thì chỉ đơn giản là gom mấy cái tool ở trên để cho nó vào 1 cái table có gồm nhiều table con, và mỗi table con là 1 vector cấp 2. Tuy nó dài nhưng bảo đảm cậu là tui viết rất kĩ và nó cũng khá uyển chuyển, dù cậu có bao nhiêu dòng và cột. Nếu thắc mắc chỗ nào thì cho tui biết, tui sẽ giải thích.

    Và cuối cùng là main thôi
    C++ Code:
    1. int main()
    2. {
    3.     std::ifstream inf;
    4.     TenOfTen      client;
    5.    
    6.     client.readDataFromFile( inf );
    7.     client.showData( std::cout );
    8.    
    9.     util::pauseScreen();
    10.     system( "cls" );
    11.  
    12.     client.showRoutine();
    13.  
    14.     return 0;
    15. }

  3. #3
    Ngày gia nhập
    05 2008
    Bài viết
    0

    Em cám ơn anh!! Để em đọc có rì em hỏi lại anh sau...

  4. #4
    Ngày gia nhập
    02 2008
    Bài viết
    2

    Bài này có cần phải phức tạp thế không nhỉ. Chỉ cần dùng <ifstream> là được thôi mà.

  5. #5
    Ngày gia nhập
    09 2006
    Bài viết
    711

    Good code, RR. Càng ngày cậu càng expert về coding C++ & STL rồi đấy.

  6. #6
    Ngày gia nhập
    12 2006
    Nơi ở
    US
    Bài viết
    1,917

    Mặc định KHÓ QUÁ!!_ Em xin hỏi về cách đọc từ file text ra mảng!!!!!!!!

    Good code, RR. Càng ngày cậu càng expert về coding C++ & STL rồi đấy.
    Thanks anh :"> !

Các đề tài tương tự

  1. Loại bỏ text trùng nhau từ một file text(.txt) - File văn bản
    Gửi bởi bossnabito trong diễn đàn Thắc mắc chung
    Trả lời: 7
    Bài viết cuối: 31-07-2011, 03:34 AM
  2. Đọc dữ liệu file midi dạng số HEX và lấy lời nhạc trong file ra file text
    Gửi bởi kimngockhtn_2007 trong diễn đàn Thắc mắc lập trình C#
    Trả lời: 0
    Bài viết cuối: 29-06-2011, 04:40 PM
  3. Text File Protector - Bảo vệ file text bằng password (siêu ẩn)
    Gửi bởi gianghoplus trong diễn đàn Sản phẩm phần mềm của bạn
    Trả lời: 10
    Bài viết cuối: 08-05-2009, 09:27 AM
  4. Đọc file text? Đọc file dung lượng lớn? Lỗi lưu kết quả khi đọc nhiều lần?
    Gửi bởi totoise trong diễn đàn Thắc mắc lập trình C/C++/C++0x
    Trả lời: 6
    Bài viết cuối: 19-04-2009, 08:21 PM
  5. Bai tap ve file!Tạo một file text trong đó mỗi dòng lưu 3 số nguyên
    Gửi bởi sonsdc trong diễn đàn Thắc mắc lập trình C/C++/C++0x
    Trả lời: 2
    Bài viết cuối: 06-03-2009, 10:59 PM

Quyền hạn của bạn

  • Bạn không thể gửi đề tài mới
  • Bạn không thể gửi bài trả lời
  • Bạn không thể gửi các đính kèm
  • Bạn không thể chỉnh sửa bài viết của bạn