Propozycja
plik.txt
==============
=
=
=
=
==============
#include <fstream>
#include <iostream>
#include <string>
std::string GetDataFromLine(std::string file_name,
unsigned int line_num,
unsigned int pos, unsigned int length)
{
std::fstream file(file_name);
file.seekg(std::ios::beg);
for(int i=0; i < line_num - 1; ++i){
file.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
}
std::string output;
std::getline(file, output);
file.close();
output = output.substr(pos - 1, length);
return output;
}
int main()
{
const std::string FILE_NAME = "plik.txt";
std::cout << "\n";
std::string output;
std::fstream file(FILE_NAME);
if (file.good())
{
while(std::getline(file, output))
{
std::cout << output << "\n";
}
}
file.close();
output = "";
std::cout << "\n";
std::string x, Postion[2];
x = GetDataFromLine(FILE_NAME, 6, 1, 5);
Postion[1] = GetDataFromLine(FILE_NAME, 6, 1, 1);
std::cout << x << "\n" << Postion[1] << std::endl;
return 0;
}
...program odczytywał np. tylko pięć liter z linii pliku .txt, a następnie zapisywał je do zmiennej x ...
x = GetDataFromLine(FILE_NAME, 6, 1, 5);
PLIK.txt
==============
=
=
=
=
==============
^
\--------------------------------------------\
Program teraz zapisuje do zmiennej Postion[1] ten znak.
Postion[1] = GetDataFromLine(FILE_NAME, 6, 1, 1);