473,396 Members | 1,804 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,396 software developers and data experts.

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 5196
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(const 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.begin(), value.end(), ostream_iterator<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(const 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.google.c om...
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> MyStringToVector( const std::string& Str );

int main()
{
ifstream In("c:\\myprojects\\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 = MyStringToVector<int>( Line );
if( LineContents.size() <= 2 && LineContents.size() > 0 ) {
getline( In, Line );
cout << Line << endl;
}
}
}

return true;
}
////////////////////////////////////////////////////////////////////////////
//
template< typename T>
std::vector<T> MyStringToVector( 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::istringstream iss( Str );
std::vector<T> MyVec;
std::copy( std::istream_iterator<T>(iss), std::istream_iterator<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
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...
3
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...
2
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...
4
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...
8
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...
0
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...
10
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...
7
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...
32
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,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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
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...

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.