Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Position 15 to 30

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
How can I get the text from the text file of the position 15 to 30?

Ex:
Into the text file:

abcdefghijklmnopqrstuvwxyzabcdefghijk

Result:

pqrstuvwxyzabcd

Thank you in advanced

Paola S



 
there are multiple ways. If you are using cfile you can do a Seek to position 15 from the beginning and then read 16 characters.

If using ifstream you can do 14 gets and then do a getline with a buffer and read in 16 characters Or do 2 getlines and get one for 14 and then 1 for 16.


Matt
 
you can also use the standard C functionality, such as fseek on a FILE variable
 
Use strncpy function.
Below is an example from MSDN:

#include <string.h>
#include <stdio.h>

void main( void )
{
char string[100] = &quot;Cats are nice usually&quot;;
printf ( &quot;Before: %s\n&quot;, string );
strncpy( string, &quot;Dogs&quot;, 4 );
strncpy( string + 9, &quot;mean&quot;, 4 );
printf ( &quot;After: %s\n&quot;, string );
}

Output
Before: Cats are nice usually
After: Dogs are mean usually


Hope this helps, s-)

Blessed is he who in the name of justice and goodwill, sheperds the weak through the valley of darkness...
 
see a short typical C++ sample for strings:

#include<string>
#include<iostream>
using namespace std;
int main(int argc, char* argv[])
{
string cccc = &quot;gahahiajhihello worldajhaihjatihjaihjahihafhiphjahj&quot;;
string xxx = cccc.substr(10,11);
cout<<xxx.c_str()<<endl;
return 0;
} John Fill
1c.bmp


ivfmd@mail.md
 
strncpy() or string.substr() are fine but she wants to read the substring directly from a textfile.
 
The she should use firs fseek and fread to read the necesary string into a buffer (or char*). s-)

Blessed is he who in the name of justice and goodwill, sheperds the weak through the valley of darkness...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top