473,408 Members | 1,874 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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 11625
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
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
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
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
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
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
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
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
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
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
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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
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...
0
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...

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.