473,480 Members | 2,057 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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!!
Jul 19 '05 #1
7 11631
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" <fr**********@hong-kong.crosswinds.net> wrote in message
news:bm***********@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!!

Jul 19 '05 #2
Oops in my previous post that should be ifstream of course.

Regards, Ron AF Greve.
"FrancisC" <fr**********@hong-kong.crosswinds.net> wrote in message
news:bm***********@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!!

Jul 19 '05 #3
"FrancisC" <fr**********@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
Jul 19 '05 #4
"FrancisC" <fr**********@hong-kong.crosswinds.net> wrote in message news:<bm***********@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:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>
Jul 19 '05 #5
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;
}
Jul 19 '05 #6
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
Jul 19 '05 #7

"FrancisC" <fr**********@hong-kong.crosswinds.net> wrote in message
news:bm***********@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

Jul 19 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5311
by: gary smith | last post by:
I am trying to open a file (file --> open --> file) at an FTP location using visual Studio.net. Unfortunately, I get an "unspecified error" alert whenever I select a file. Can anyone help...
3
26322
by: puzzlecracker | last post by:
I want to read lines and skip blank lines: would this work considering the lines can contain tabs, spaces, etc.? file.in: ------ line1 line2
2
6484
by: Corne' Cornelius | last post by:
Hi, When you open a file for writing/appending with open() or fopen(), and you have multiple applications that might want to write to the same file at the same time, could that cause weirdness...
12
926
by: FrancisC | last post by:
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 ...
2
12579
by: agphoto | last post by:
There is big or problem in open file in read and write mode.. $file = "data.txt"; $fp = fopen($file,"w+"); $line = fgets($fp,"120"); // i need only 1st line to read and upto 120 bytes echo...
5
11158
by: Ryan Liu | last post by:
Hi, Both way works, I'd just ask some experts which way is better? My application creates a log file daily. Now each time when I write a log, I will open the file and append to the end....
6
1653
by: gonzlobo | last post by:
I've been using Python for a few days. It's such the perfect language for parsing data! I really like it so far, but I'm having a hard time reading a file, reading the first few hex characters &...
3
20546
by: tjuiowa | last post by:
Hi I am trying to import a text file into a MS Access database. The first 11 lines of the text file are in a different format, so i would like to skip those lines and start at line 12. is there...
18
6251
by: luckyyyyyy | last post by:
I appriate your work but... guys i have another problm.....i wana skip first 9 lines from my data file and after skipping i wana read x,y,z at the end of file. i did but after while loop is end it...
7
11295
by: JeremyI | last post by:
Today I've been trying out a personal project I have been interested in for a while. MS Access 2000. I get a daily list of currency exchange rates via email, but it's a bit of a pain to go through...
0
6911
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7050
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6743
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6966
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4787
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4488
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2988
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1303
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
564
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.