How can I open a file, skip first 2 lines and get the 50th character?

F

FrancisC

How can I open a file, skip first 2 lines and get the 50th character?

EXP 0 R:\000\un\111\e00\e00noLog\1006\bdry_arc.e00
ARC 2
1 1 0 0 0 0 7

i.e., I want to get the "7" in the third line, how can I do that?

thx!!
 
M

Moonlit

Hi,

#include <fstream>
#include <string>
using namespace std;

int main()
{
int RetVal = 0;
ofstream Input( "Filename" );
if( !Input.is_open() )
{
// Could use exceptions, but since it is so small...
RetVal = 1;
}
else
{
string Line;
getline( Input, Line );
getline( Input, Line );
getline( Input, Line );
cout << Input[ 49 ] << endl;
}

return RetVal;
}

TODO add some more error checking.

Regards, Ron AF Greve.
 
F

Frank Schmitt

FrancisC said:
How can I open a file, skip first 2 lines and get the 50th character?

EXP 0 R:\000\un\111\e00\e00noLog\1006\bdry_arc.e00
ARC 2
1 1 0 0 0 0 7

i.e., I want to get the "7" in the third line, how can I do that?

The 7 is the 70th character on this line, not the 50th.

Some things are better done with standard unix tools than reinvented:

head -3 bla | tail -1 | cut -b 50

kind regards
frank
 
D

Dietmar Kuehl

FrancisC said:
How can I open a file, skip first 2 lines and get the 50th character?

std::ifstream in("file.name");
std::istreambuf_iterator<char> beg(in), end;

beg = std::find(std::find(beg, end, '\n'), end, '\n');
for (int i = 0; i < 50 && beg != end; ++beg)
;
if (beg != end)
std::cout << "the 50th character on the third line is '"
<< *beg << "'\n";
else
std::cout << "some error occured while looking for the character\n";
 
M

Moonlit

Hi

Now without the typo's ;-)

#include <fstream>
#include <string>
using namespace std;

int main()
{
int RetVal = 0;
ifstream Input( "Filename" );
if( !Input.is_open() )
{
// Could use exceptions, but since it is so small...
RetVal = 1;
}
else
{
string Line;
getline( Input, Line );
getline( Input, Line );
getline( Input, Line );
cout << Line[ 49 ] << endl;
}

return RetVal;
}
 
D

Default User

FrancisC said:
How can I open a file, skip first 2 lines and get the 50th character?



I recommend you decide which language you are working in. This same
question appeared in comp.lang.c.




Brian Rodenborn
 
H

Howard

FrancisC said:
How can I open a file, skip first 2 lines and get the 50th character?

EXP 0 R:\000\un\111\e00\e00noLog\1006\bdry_arc.e00
ARC 2
1 1 0 0 0 0 7

i.e., I want to get the "7" in the third line, how can I do that?

thx!!

one way:

1) Open the file for reading
2) Read the first line into a string
3) Read the next line into the same string
4) Read the third line into the same string
5) The character you want is in the string...just use it! (i.e.,
myString[49])

-Howard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top