473,757 Members | 10,007 Online
Bytes | Software Development & Data Engineering Community
+ 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\e 00\e00noLog\100 6\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 11649
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**********@h ong-kong.crosswinds .net> wrote in message
news:bm******** ***@news.hgc.co m.hk...
How can I open a file, skip first 2 lines and get the 50th character?

EXP 0 R:\000\un\111\e 00\e00noLog\100 6\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**********@h ong-kong.crosswinds .net> wrote in message
news:bm******** ***@news.hgc.co m.hk...
How can I open a file, skip first 2 lines and get the 50th character?

EXP 0 R:\000\un\111\e 00\e00noLog\100 6\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**********@h ong-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\e 00\e00noLog\100 6\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**********@h ong-kong.crosswinds .net> wrote in message news:<bm******* ****@news.hgc.c om.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.co m> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.co m/>
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**********@h ong-kong.crosswinds .net> wrote in message
news:bm******** ***@news.hgc.co m.hk...
How can I open a file, skip first 2 lines and get the 50th character?

EXP 0 R:\000\un\111\e 00\e00noLog\100 6\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
5328
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 explain why this should be the case. Thanks Gary
3
26379
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
6516
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 in the file ? do i have to add manual file locking, or does open()/fopen() provide file locking ? Thanks,
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 i.e., I want to get the "7" in the third line, how can I do that? thx!!
2
12605
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 $line; fclose($fp); coding is not problem, The problem is file modifier as mention in PHP
5
11223
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. Ocz, if the file is not exist(e.g. another day), it will creates the file first.
6
1665
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 & converting them to an integer. Once the characters are converted to an integer, I'd like to write the data to another file. Here's the code snipped to the bare minimum: ---
3
20642
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 a quick and easy way to skip lines using VB in Access. Here is a sample of my text file: SDS 2.2 AD Results 1.0 Filename PlateID Assay Type
18
6273
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 again skip these 9 lines. how to solve this. and also i wana distinguish these float data and unsigned data, as you can see in data file. after 9 lines, 6 lines are the float data, after this 6 lines are unsigned int. actually this file has a...
7
11318
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 them and extract the few I need, and I don't keep up on it very well! However, I did determine that the actual message files are nice and readable as plain text (I still use Eudora 7). So I thought it might be possible to write a bit of VBA code to...
0
9489
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10072
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9885
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8737
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7286
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5172
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3829
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 we have to send another system
3
3399
muto222
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.