C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

stream problems - icc (icpc), gcc

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Markus Flohberger
Guest





PostPosted: Thu Jan 27, 2005 9:16 pm    Post subject: stream problems - icc (icpc), gcc Reply with quote



i want to read from a file, that contains hex data like this:

3D72 EF99 3D72 EF99 3D72 EF99 3D72 EF99
....

i have added a short code example. if i compile it with gcc 2.96, i get
the correct data. however if i compile it with intels icpc 8.1 i get
confused data. (mostly zeros and wrong data values) it seems like the
intel compiler interprets the data values as wrong data types. (my
guess) then i used c code using fscanf() and it works with both compilers.

regards,
markus

code example:

#include <iostream>
#include <fstream>

using namespace std;

const int N = 1024;

int main()
{
short real[N];
short imag[N];

ifstream read_file("data.txt");

for(int row_count=0; row_count<N/4; row_count++)
{
for(int col_count=0; col_count<4; col_count++)
{
read_file >> hex >> real[4*row_count+col_count];
read_file >> hex >> imag[4*row_count+col_count];
}
}
read_file.close();
}

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
wittempj@hotmail.com
Guest





PostPosted: Sat Jan 29, 2005 5:08 am    Post subject: Re: stream problems - icc (icpc), gcc Reply with quote



when I add some checks to what I'm reading, like below I get an error
printed, as you make assumptions about the number of elements in your
file.
-#include <cstdlib>
-#include <iostream>
-#include <fstream>
-#include <vector>
-#include <sstream>
-#include <string>
-#include <stdexcept>

-using namespace std;

-const int N = 1024;

-class error : public runtime_error
-{
-public:
- error(const string& m):runtime_error(m) {};
- virtual const char* what() const throw() { return
std::runtime_error::what(); }
-};

-int main()
-{
- vector<int> real;
- vector<int> imag;

- stringstream errmsg;-

- ifstream read_file;
- read_file.open("c:\temp\data.txt", ios_base::in);
-
- real.reserve(N);
- imag.reserve(N);
-
- try
- {
- for(int row_count=0; row_count<N/4; row_count++)
- {
- for(int col_count=0; col_count<4; col_count++)
- {
- read_file >> hex >> real[4*row_count+col_count];
- if (read_file.fail())
- {
- errmsg.str("");
- errmsg << "error reading row " << row_count <<
" col " << col_count << -endl;
- throw error(errmsg.str().c_str());
- }
- if (read_file.eof()) break;
- cout << real[4*row_count+col_count] << "t";
- read_file >> hex >> imag[4*row_count+col_count];
- if (read_file.fail())
- {
- errmsg.str("");
- errmsg << "error reading row " << row_count <<
" col " << col_count << -endl;
- throw error(errmsg.str().c_str());
- }
- if (read_file.eof()) break;
- cout << imag[4*row_count+col_count] << "t";
- }
- }
- }
- catch (error& e)
- {
- cerr << e.what() << endl;
- }


- cout << "finished reading" << endl;
- for(int row_count=0; row_count - {
- for(int col_count=0; col_count - {
- cout << real[4*row_count+col_count];
- cout << imag[4*row_count+col_count];
- }
- cout << endl;

- }

- read_file.close();
- system("Pause");
- return EXIT_SUCCESS;
-}


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Teng Lin
Guest





PostPosted: Sat Jan 29, 2005 5:39 am    Post subject: Re: stream problems - icc (icpc), gcc Reply with quote



It could be a standard c++ library problem.
I know there is a bug in libstdc++ 3.2 which will cause the same
problem. You
may need to check it first


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Ben Hutchings
Guest





PostPosted: Mon Jan 31, 2005 12:01 am    Post subject: Re: stream problems - icc (icpc), gcc Reply with quote

Markus Flohberger wrote:
Quote:
i want to read from a file, that contains hex data like this:

3D72 EF99 3D72 EF99 3D72 EF99 3D72 EF99
...

i have added a short code example. if i compile it with gcc 2.96, i get
the correct data. however if i compile it with intels icpc 8.1 i get
confused data. (mostly zeros and wrong data values) it seems like the
intel compiler interprets the data values as wrong data types. (my
guess) then i used c code using fscanf() and it works with both compilers.
snip
int main()
{
short real[N];
short imag[N];

I think the array elements should have a type of unsigned short, since
the values shown above are outside the range that signed short is
required to be capable of representing (which is -32767 to 37267).

Quote:
ifstream read_file("data.txt");

for(int row_count=0; row_count<N/4; row_count++)
{
for(int col_count=0; col_count<4; col_count++)
{
read_file >> hex >> real[4*row_count+col_count];
read_file >> hex >> imag[4*row_count+col_count];
}
}
read_file.close();
}

You should check whether the input operations actually succeed before
even looking at the values they yielded.

--
Ben Hutchings
Having problems with C++ templates? Your questions may be answered by
<http://womble.decadentplace.org.uk/c++/template-faq.html>.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.