473,804 Members | 2,122 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reading a mixed file

Hi, I have a file which I wish to read from C++. The file, created by
another programme, contains both text and numbers, all as ascii (it's
a .txt file). A sample of the file is shown below:

<< LEDAR V1.3 - Real Time Detection >>

<LEFT 144>
<TOP 165>
<RIGHT 265>
<BOTTOM 376>
<STEP 4>
<Kx 2.13068>
<Ky 0.882353>
<Detection 1>

25600055 1
299 299 299 299 299 297 297 297 297 297 297 297 297
299 299 299 299 299 297 297 297 297 297 297 297 297
299 299 299 299 299 297 297 297 297 297 297 297 297
299 299 299 299 299 297 297 297 297 297 297 297 297

26800055 2
300 300 300 300 299 297 297 297 297 298 299 299 299
300 300 300 300 299 297 297 297 297 298 299 299 299
300 300 300 300 299 297 297 297 297 298 299 299 299
300 300 300 300 299 297 297 297 297 298 299 299 299

28000055 3
300 300 300 300 301 301 301 300 301 301 301 301 300
300 300 300 300 301 301 301 300 301 301 301 301 300
300 300 300 300 301 301 301 300 301 301 301 301 300
300 300 300 300 301 301 301 300 301 301 301 301 300

Of the long lines, I want to read one line from each group, and ignore
everything else. I have relatively little experience with C++, and can
not find a suitable way to do this. Any suggestions? Please type a
small bit of sample code to show how suggested functions should be
used.

Thanks! Andrew
Jul 19 '05 #1
2 5248
adpsimpson wrote:
Hi, I have a file which I wish to read from C++. The file, created by
another programme, contains both text and numbers, all as ascii (it's
a .txt file). A sample of the file is shown below:

<< LEDAR V1.3 - Real Time Detection >>

<LEFT 144>
<TOP 165>
<RIGHT 265>
<BOTTOM 376>
<STEP 4>
<Kx 2.13068>
<Ky 0.882353>
<Detection 1>

25600055 1
299 299 299 299 299 297 297 297 297 297 297 297 297
299 299 299 299 299 297 297 297 297 297 297 297 297
299 299 299 299 299 297 297 297 297 297 297 297 297
299 299 299 299 299 297 297 297 297 297 297 297 297

26800055 2
300 300 300 300 299 297 297 297 297 298 299 299 299
300 300 300 300 299 297 297 297 297 298 299 299 299
300 300 300 300 299 297 297 297 297 298 299 299 299
300 300 300 300 299 297 297 297 297 298 299 299 299

28000055 3
300 300 300 300 301 301 301 300 301 301 301 301 300
300 300 300 300 301 301 301 300 301 301 301 301 300
300 300 300 300 301 301 301 300 301 301 301 301 300
300 300 300 300 301 301 301 300 301 301 301 301 300

Of the long lines, I want to read one line from each group, and ignore
everything else.


I assumed in the code below that you could assume that the header will
always be the same number of lines. If that's not the case, then you
should write some type of function that will look for a sentinal or
whatever is appropriate. I also assumed that each group will have four
long lines.

Some other points about the code: note that the return types of the two
helper functions are string and vector<int>. Returning large types by
value requires creating and copying a temporary object. This, in some
applications is an unnecessary overhead, but it probably won't be a
significant bottleneck in this case.

/////////////////////////////////////////////
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <sstream>

using namespace std;

string read_group();
vector<int> parse_line(cons t string&);

int
main()
{
string junk;

const int LEADING_TEXT = 11;

for (int i = 0; i < LEADING_TEXT; ++i)
getline(cin, junk);

int line_number = 1;
while (true) {
string line = read_group();
if (line == "")
break;

cout << "line " << line_number << ": \'" << line << "\'\n";

vector<int> value = parse_line(line );
cout << "{ ";
copy(value.begi n(), value.end(), ostream_iterato r<int>(cout, " "));
cout << " }\n";
}

return 0;
}

string
read_group()
{
const int LINES_PER_GROUP = 4;
string junk;
getline(cin, junk); // '25600055 1'
if (!cin)
return "";

string line;
getline(cin, line); // this is the line of interest

for (int i = 0; i < LINES_PER_GROUP ; ++i)
getline(cin, junk); // 3 more lines, plus a blank.
// The blank may not exist after
// the last group, but you may be
// able to ignore that.

return line;
}
vector<int>
parse_line(cons t string& line)
{
vector<int> v;
int i;
istringstream iss(line);

while (iss >> i)
v.push_back(i);

return v;
}
////////////////////////////////////////
--
Adam Fineman

(Reverse domain name to reply.)

Jul 19 '05 #2

"adpsimpson " <a_***********@ hotmail.com> wrote in message
news:51******** *************** ***@posting.goo gle.com...
Hi, I have a file which I wish to read from C++. The file, created by
another programme, contains both text and numbers, all as ascii (it's
a .txt file). A sample of the file is shown below:

<< LEDAR V1.3 - Real Time Detection >>

<LEFT 144>
<TOP 165>
<RIGHT 265>
<BOTTOM 376>
<STEP 4>
<Kx 2.13068>
<Ky 0.882353>
<Detection 1>

25600055 1
299 299 299 299 299 297 297 297 297 297 297 297 297
299 299 299 299 299 297 297 297 297 297 297 297 297
299 299 299 299 299 297 297 297 297 297 297 297 297
299 299 299 299 299 297 297 297 297 297 297 297 297

26800055 2
300 300 300 300 299 297 297 297 297 298 299 299 299
300 300 300 300 299 297 297 297 297 298 299 299 299
300 300 300 300 299 297 297 297 297 298 299 299 299
300 300 300 300 299 297 297 297 297 298 299 299 299

28000055 3
300 300 300 300 301 301 301 300 301 301 301 301 300
300 300 300 300 301 301 301 300 301 301 301 301 300
300 300 300 300 301 301 301 300 301 301 301 301 300
300 300 300 300 301 301 301 300 301 301 301 301 300

Of the long lines, I want to read one line from each group, and ignore
everything else. I have relatively little experience with C++, and can
not find a suitable way to do this. Any suggestions? Please type a
small bit of sample code to show how suggested functions should be
used.

Thanks! Andrew


You have to find patterns that you can look for that identify where you are
in your file. This means that you should read in each line and parse it. If
I get you correctly then you want to read only one line of the blocks of 4
lines, each containing 13 numbers. There are many ways you can do this. The
basic idea is that you have to find the start and the end of the block. Thus
you should be looking for things that definitely won't change. If every
block has a header of two numbers you can look for a line containing only
two numbers and read the next line. Go on reading until you reach the next
line containing only two integers. Of course this works only under the
assumption that there will always be a line with two numbers only before
your block starts!

The following code will show you one way to do it though it definitely is
not the only or probably the best way. Nevertheless it should give you an
idea and you should be able to come up with something yourself.

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

template< typename T>
std::vector<T> MyStringToVecto r( const std::string& Str );

int main()
{
ifstream In("c:\\myproje cts\\test\\test .dat");
vector<int> LineContents;
string Line;

if( !In ) {
cerr << "Can't open input file" << endl;
return false;
}

while( getline( In, Line ) ) {
if( Line.find("<") == string::npos ) {
LineContents = MyStringToVecto r<int>( Line );
if( LineContents.si ze() <= 2 && LineContents.si ze() > 0 ) {
getline( In, Line );
cout << Line << endl;
}
}
}

return true;
}
////////////////////////////////////////////////////////////////////////////
//
template< typename T>
std::vector<T> MyStringToVecto r( const std::string& Str )
// Tokenize a passed line/string into a vector
// e.g:
// std::string str = "1 2 3 4";
// std::vector<int > vec = StringToVector< int>(str);
////////////////////////////////////////////////////////////////////////////
//
{
std::istringstr eam iss( Str );
std::vector<T> MyVec;
std::copy( std::istream_it erator<T>(iss), std::istream_it erator<T>(),
back_inserter( MyVec ) );
return MyVec;
}

HTH
Chris
Jul 19 '05 #3

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

Similar topics

8
9539
by: Yeow | last post by:
hello, i was trying to use the fread function on SunOS and ran into some trouble. i made a simple test as follows: i'm trying to read in a binary file (generated from a fortran code) that contains the following three floating-point numbers: 1.0 2.0 3.0
3
8439
by: Sivaraj G via .NET 247 | last post by:
We created a unicode file using java application. It usesmethods like writeUTF(), writeInt() of java.io.DataOutputStreamclass to write the content of the file. We are able to read datausing java.io.DataInputStream.readUTF() method. It's workingwell in java environment. When we tried to read the above unicode file in .net environment.We received junk content not the original content. Actually we tried a sample program in c# and...
2
3182
by: Matt McGonigle | last post by:
Hi all, Please help me out with this. Perhaps it is a dumb question, but I can't seem to make it work. I am doing a file conversion using an unformatted binary file for input and outputting to a normal text file. I need to read in a float from the binary file, but it sets my input stream to failbit. Is there a special way I can read in floats? All I am doing is
4
1780
by: Ganesh Muthuvelu | last post by:
Hi STAN, Stan: Thanks for your response to my previous post on reading a XSD file using your article in "https://blogs.msdn.com/stan_kitsis/archive/2005/08/06/448572.aspx". it works quite well but I have one problem.. I am not able to read a Complex Content.. Here is a portion of the XSD that contains the complex content. I need to read the elements under it and could not get an handle to it.. Could you please help?
8
2325
by: Edward Diener | last post by:
By reuse, I mean a function in an assembly which is called in another assembly. By a mixed-mode function I mean a function whose signature has one or more CLR types and one or more non-CLR types. The problem: I have a number of mixed-mode functions which I want reuse. These functions revolve around converting a CLR String to a C++ std::string or
0
3003
by: emu | last post by:
Hi All, I have an unmanaged C++ application that references a mixed mode image DLL (mixed managed and unmanaged). Under .NET 1.1 we could trust the dll (the mixed mode dll) by running the following command line: caspol.exe -polchgprompt off -machine -addgroup 1 -url "file://<UNC path to dll>\mixedMode.dll" FullTrust ame "GroupName" -polchgprompt on
10
8362
by: Tyler | last post by:
Hello All: After trying to find an open source alternative to Matlab (or IDL), I am currently getting acquainted with Python and, in particular SciPy, NumPy, and Matplotlib. While I await the delivery of Travis Oliphant's NumPy manual, I have a quick question (hopefully) regarding how to read in Fortran written data. The data files are not binary, but ASCII text files with no formatting and mixed data types (strings, integers,...
7
4810
by: =?Utf-8?B?SmltIFdhbHNo?= | last post by:
I'm new to working with mixed assemblies. All of my previous experience has been with VC++/MFC in native, unmanaged applications. When I create a mixed assembly in which one or more of the files compiles with /clr the instructions say that I need to change the switch for Debug information format from Program Database for Edit & Continue to disabled. At runtime I find that I am not able to set breakpoints in the managed code, nor trace...
32
2370
by: Bill Cunningham | last post by:
I am interested in writing a numeric text reader. This only reads numbers of securities and stores them. Nice practice. I have determined that these functions are needed. isalpha, isdigit, isspace, and file io. The thing is I don't know how C scans lines. This is a parser of sorts. price average
0
9716
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
10604
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...
0
10354
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10359
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,...
1
7643
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
6870
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4314
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
2
3837
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3005
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.