2014-11-11 2 views

답변

0

여기 있습니다. 파일 이름을 무엇이든 변경할 수 있으며 필요한 경우 줄 수를 변경할 수 있습니다. 당신이 준 정보에 따라, 나는 이것을 만들었습니다.

#include <iostream> 
#include <fstream> 
#include <cstdlib> 
#include <ctime> 

int main() 
{ 
     const char* myFileName = "data.txt"; 
     const int numberOfLines = 10; 

     std::ifstream myData; 
     std::string lines[numberOfLines]; 
     int index = 0; 

     myData.open(myFileName, std::ifstream::in); 
     while (myData.good() && index < numberOfLines) { 
       myData >> lines[index]; 
       index++; 
     } 
     myData.close(); 

     srand(time(NULL)); // Seed random number generator 
     int randomIndex = rand() % index; // Incase there were less than numberOfLines 
     std::cout << lines[randomIndex] << std::endl; 
     return 0; 
}