Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   How can I open a file, skip first 2 lines and get the 50th character? (http://www.velocityreviews.com/forums/t278286-how-can-i-open-a-file-skip-first-2-lines-and-get-the-50th-character.html)

FrancisC 10-10-2003 01:24 AM

How can I open a file, skip first 2 lines and get the 50th character?
 
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!!



Moonlit 10-10-2003 02:09 AM

Re: How can I open a file, skip first 2 lines and get the 50th character?
 
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.


"FrancisC" <francischeng@hong-kong.crosswinds.net> wrote in message
news:bm52r3$2vac$1@news.hgc.com.hk...
> 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!!
>
>




Moonlit 10-10-2003 02:11 AM

Re: How can I open a file, skip first 2 lines and get the 50th character?
 
Oops in my previous post that should be ifstream of course.

Regards, Ron AF Greve.


"FrancisC" <francischeng@hong-kong.crosswinds.net> wrote in message
news:bm52r3$2vac$1@news.hgc.com.hk...
> 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!!
>
>




Frank Schmitt 10-10-2003 11:49 AM

Re: How can I open a file, skip first 2 lines and get the 50th character?
 
"FrancisC" <francischeng@hong-kong.crosswinds.net> writes:

> 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

--
Frank Schmitt
4SC AG phone: +49 89 700763-0
e-mail: frankNO DOT SPAMschmitt AT 4sc DOT com

Dietmar Kuehl 10-10-2003 12:14 PM

Re: How can I open a file, skip first 2 lines and get the 50th character?
 
"FrancisC" <francischeng@hong-kong.crosswinds.net> wrote in message news:<bm52r3$2vac$1@news.hgc.com.hk>...
> 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";
--
<mailto:dietmar_kuehl@yahoo.com> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>

Moonlit 10-10-2003 01:25 PM

Re: How can I open a file, skip first 2 lines and get the 50th character?
 
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;
}



Default User 10-10-2003 05:05 PM

Re: How can I open a file, skip first 2 lines and get the 50thcharacter?
 
FrancisC wrote:
>
> 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

Howard 10-10-2003 07:11 PM

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

"FrancisC" <francischeng@hong-kong.crosswinds.net> wrote in message
news:bm52r3$2vac$1@news.hgc.com.hk...
> 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





All times are GMT. The time now is 08:56 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.