c++ extract string
i have 26/01/10 09:20:20 MAL BIN BIN275 TSTCB U8L5 O/CR ..N UCOS Operated
in开发者_JS百科 string
i want to extract column 36 into 60 that is
BIN275 TSTCB U8L5 O/CR
the last output i want to include
O/CR
is there any simple solution to settle this? already make this but not work.
#include <iostream>
#include <string.h>
#include <fstream>
using namespace std;
int main()
{
FILE * pFile;
char mystring [100];
int string_length;
ofstream output;
pFile = fopen ("input.txt" , "r");
output.open("output.txt", ios:: out);
fgets (mystring , 100 , pFile);
puts (mystring);
string_length = strlen(mystring);
int i=36;
while (i < 60)
{
output<<mystring[i];
++i;
}
fclose (pFile);
output.close();
return 0;
}
thank you
Your program basically works but your column numbers are not correct. Try:
int i=26;
while (i < 48)
It gives me the result you are specifying.
Since you seem to want to use C++, we could write it slightly more elegantly as:
#include <fstream>
#include <string>
int main()
{
int const colLeft = 36; // or 26
int const colRight = 60; // or 48
std::ifstream input("input.txt");
std::ofstream output("output.txt");
std::string line;
std::getline(input,line);
output << line.substr(colLeft,(colRight-colLeft)+1);
}
精彩评论