How to use function freopen_s

后端 未结 2 886
余生分开走
余生分开走 2021-01-13 21:36

In order to read input from a text file, I wrote the following code:

int main(){
    int x;
#ifndef ONLINE_JUDGE
    freopen(\"input.txt\", \"r\", stdin);
#e         


        
2条回答
  •  北海茫月
    2021-01-13 21:59

    I would suggest you to avoid using the C style I/O library and use IOSTREAM Library. Its more safe and clean solution. Here is the sample code which opens a file and reads each line into the "std::string" object and prints on the console.

    int main() 
    {
    
    std::string  file =  "input.txt";
    std::string line;
    std::ifstream  text_filestream(file.c_str());
    
    while(std::getline(text_filestream, line)) {
     std::cout<

提交回复
热议问题