ตัวอย่างนี้อ่านค่าจาก file โดยระบุตำแหน่งที่ต้องการจาก ฟังค์ชั่น substr(int start,int end) สังเกตนิดนึงนะครับว่าก่อนที่จะทำการใช้ฟังค์ชั่นนี้จะมีการเช็คความยาวของ buf (buffer) ด้วยเพื่อไม่ให้ pointer ทำงานผิดพลาด

Here is an example to read an integer from file
test.txt
[src]
1018
2125
3565
[/src]

ReadInt.cpp
[src]
#include < iostream.h >
#include < fstream.h >
#include < string >

using std::string;

void main()
{


string temp;
int x;
char buf[255];
ifstream infile("test.txt");

while(!infile.eof())
{
infile.getline(buf,255);
temp=buf;
if(strlen(buf)>1)temp=temp.substr(3,4); //แยกอักขระตำแหน่งที่3-4
x=atoi(temp.data());
cout < < x < < endl;
}
}

[/src]

Output
[src]
18
25
65
[/src]