does fstream read/write move file pointer

前端 未结 2 582
我在风中等你
我在风中等你 2021-01-12 22:55

This is kind of a simple question that I hope can be answered easily, do the file stream read and write operations move the pointer along? As an example:

cpo         


        
2条回答
  •  暖寄归人
    2021-01-12 23:21

    Yes, that is the way it works. Your examples aren't quite the same, though. Your first example reads from 10000, then 10001, then 10002, etc. The second needs a seek outside the loop to set the initial position. To be 100% equivalent, you need to have your second example look like:

    cpos=10000;
    dataFile.seekg(cpos,ios::beg);
    for (i=0;i<20;i++) {
       dataFile.read(carray[i],1);
    }
    

提交回复
热议问题