LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-22-2003, 03:34 PM   #1
clowns119
LQ Newbie
 
Registered: Jul 2003
Posts: 27

Rep: Reputation: 15
Reading Files


I am fairly new to c++ and trying to write a program that reads and displays information from a certian file type and I cannot figure out were to begin to start this prosses. If anyone can help I would appreaciate it.
 
Old 09-22-2003, 07:46 PM   #2
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Rep: Reputation: 30
Code:
#include<string>
#include<iostream>
#include<fstream>
#include<vector>
using namespace std;

int main ()
{
   vector<string> v;
   ifstream in ("peloponnesian_a.txt");
   string line;
   while (getline (in, line))
      v.push_back(line); //Add the line to the end //Add line numbers:
   for (int i=0; i<v.size(); i++)
      cout<<i<<": "<<v[i]<<endl;
}
//Copy an entire file into a vector of string
The code is from Chapter 2 of <<thinking in c++>>.
In this case, there already is a file, peloponnesian_a.txt in the directory that the program is located.
http://www.cplusplus.com/doc/tutorial/tut6-1.html
The link above have some information too.


Last edited by Xiangbuilder; 09-22-2003 at 08:01 PM.
 
Old 09-22-2003, 08:51 PM   #3
clowns119
LQ Newbie
 
Registered: Jul 2003
Posts: 27

Original Poster
Rep: Reputation: 15
Thanks,
Once I get the file into a string how do I view its contents?
 
Old 09-22-2003, 08:59 PM   #4
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 33
Code:
#include<fstream>
#include<cstdlib>
main(int argc,char *argv[])
{
        char ch;
        std:: ifstream from(argv[1]);
        std:: ofstream to(argv[2]);
        while(from.get(ch)) to.put(ch);
}
 
Old 09-22-2003, 09:01 PM   #5
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 33
Quote:
Originally posted by clowns119
Thanks,
Once I get the file into a string how do I view its contents?
Well to view the contents of the string just use cout.

<edit >
This way
Code:
for (int i=0; i<v.size(); i++)
      cout<<i<<": "<<v[i]<<endl;
as Xiangbuilder has already posted.


Last edited by SaTaN; 09-22-2003 at 09:05 PM.
 
Old 09-23-2003, 12:34 AM   #6
clowns119
LQ Newbie
 
Registered: Jul 2003
Posts: 27

Original Poster
Rep: Reputation: 15
Ok thanks for the help,
That works good for .txt files but what if I want to view contents of another type of file?
 
Old 09-23-2003, 12:39 AM   #7
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Rep: Reputation: 30
Why not give it a go?
I guess it will work well with some other files, such as *.cpp, however not with all types of files.
 
Old 09-23-2003, 12:46 AM   #8
clowns119
LQ Newbie
 
Registered: Jul 2003
Posts: 27

Original Poster
Rep: Reputation: 15
The file I need to read into is a .grb file It is a gridded binary file that contains weather data. I tried to do it with that code and it didn't work. You have any other ideas
 
Old 09-23-2003, 12:51 AM   #9
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Rep: Reputation: 30
Sorry, I am poor about .grb file.
 
Old 09-23-2003, 06:02 AM   #10
dakensta
Member
 
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
You need to find out the file format!

A quick google (grib format) turned up this link:
http://weather.unisys.com/wxp/Append...mats/GRIB.html
but it is fairly long - there is also a summary here: http://netghost.narod.ru/gff/graphics/summary/grib.htm
which, somewhat ominously, contains the text
"GRIB is extremely complex"

My advice would be to have a thorough search for a utility that already does what you want.

Failing that, know that you are reading in binary data. For this you will want to have an idea about what hardware wrote the files in question, as different hardware may write bytes in different orders, e.g. a two byte integer could be written 11111111 00000000 or 00000000 11111111, the actual numerical value being the same. If you are using grb files and weather data, I am guessing that you may have some slightly esoteric hardware around (just a guess, I repeat) so you will want to check this.

Secondly, study the file format documentation, so that you know what bytes contain which values and how to store them. If this is program is going to be used in the future, and as it may take some effort, then it it probably worth considering how the data will be used and design a class (or maybe classes...) to hold that data. You will need to match the correct variable type to the correct number of bytes in the file. You might find it easier to have a header file containing some aliases so that if needed you can change the types with minimal hassle (easiest with #define but lots of ways esp. if you know about namespaces, scope) should the code be ported to new and different hardware
e.g.
Code:
#define char_8 char
#define int_16 short

class GribHeader
{
  char_8  file_id;
  int_16 weather_id;
  int_16 cloud_cover;
};
Read these in one at a time (i.e. don't use a single pointer and the total number of bytes).
e.g.
Code:
  template<typename TYPE>
  std::streamsize
  read_built_in( std::istream& is, TYPE& item )
  {
    return is.rdbuf()->sgetn( reinterpret_cast<char*>( &item ), sizeof( item ) );
  }
//... elsewhere ....
 ifstream ifs( "data.grb", ifstream::in | ifstream::binary );
 read_built_in( ifs, file_id );
 read_built_in( ifs, weather_id ); 
 read_built_in( ifs, cloud_cover );
Remember I mentioned byte order? This may need to be changed and you will often find that there is a "magic number" or something unique to the file format - check the value - work out what (if anything) needs to be swapped about. Check the specifications for what it says about floating point format but usually swapping bytes is a matter of creating a few masks and bit shifting the values, e.g.
Code:
  void byte_swap( int& i )
  {
    int tmp = (i & 0x000000FF);
    tmp = ((i & 0x0000FF00) >> 8) | (tmp << 8);
    tmp = ((i & 0x00FF0000) >> 16) | (tmp << 8);
    tmp = ((i & 0xFF000000) >> 0x18) | (tmp << 8);
    i = tmp;
  }
(Please do check the code snippits above before using them)

Reverse the whole process for writing the file - generally polite to write the file exactly as you found it although your institute may have some guidelines on how the files are to be stored so check with them.

Hope all this helps ... good luck

Last edited by dakensta; 09-23-2003 at 06:09 AM.
 
Old 09-23-2003, 08:33 AM   #11
dakensta
Member
 
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
Sorry - previous post is going to be a bit of a red herring in this case...

GRIB appears to consist of packed bytes and also appears to be a standard for bit-wise numerical representation, so don't worry about (most of) the byte swapping part.

Read every byte individually, using the first link I posted as a reference. When more than one byte (or octet as they refer to them, suggesting unusual hardware use) contains a single value or field, you will need to work out the numerical value of those bytes (as a contigous bit stream, I would have thought) and assign that to a suitable type - you will need to do some bit shifting (as well as being aware of your own internal byte representation)

Read the bit about floating point representation.

Some software exists, linked from, here:
http://wesley.wwb.noaa.gov/reading_grib.html

Sorry for any confusion.
 
Old 09-23-2003, 05:26 PM   #12
clowns119
LQ Newbie
 
Registered: Jul 2003
Posts: 27

Original Poster
Rep: Reputation: 15
I have used wgrib and converted the grib file to a binary file. now can I read it with c++
 
Old 09-23-2003, 06:06 PM   #13
clowns119
LQ Newbie
 
Registered: Jul 2003
Posts: 27

Original Poster
Rep: Reputation: 15
I have a file which displays this
132 343
1212
1213
46367
878
...

Now how do I convert that data to actual words that say what the weather data is?

Last edited by clowns119; 09-23-2003 at 08:43 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading files in C++, using *. poeta_boy Programming 4 07-08-2004 01:01 AM
Reading WPS files TazG Linux - Software 0 06-12-2004 05:25 PM
reading files over the network derfaust Linux - Software 7 06-08-2004 02:39 PM
reading files MrGardenHoseMan Linux - Newbie 2 07-06-2003 05:58 PM
Not reading all files on CDRom AnthonyM Linux - Newbie 2 03-26-2002 09:16 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration