ifstream character read problem

F

faz

HI all,,

I used linux g++ 3.2.3 ...In my c++ code i am reading bit values from
a file as character..using the following:


char revStr[41],correctStr[41];
int count=0
ifstream in("config.txt",ios::in | ios::binary);
if(!in)
{
cout << "Cannot open read file.";
return 1;
}
while(in)
{ // in will be false when eof is reached
in.get(ch);
if(in)
{
cout << ch;
correctStr[count]=ch;
count++;
}
}
correctStr[count]='\0';
fn_revrs(correctStr,revStr,count);
cout<<"\nno of characters in file :"<<count<<endl;
cout<<"correct set of characters in file :\n"<<correctStr<<endl;
cout<<"rev of characters in file :\n"<<revStr<<endl;


Actually the number of characters in a file is 42....But it is taking
and giving the count value as 43,,,

Really i dont know why this is happening...

pls help me

regards,
fazal
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

HI all,,

I used linux g++ 3.2.3 ...In my c++ code i am reading bit values from
a file as character..using the following:


char revStr[41],correctStr[41];
int count=0
ifstream in("config.txt",ios::in | ios::binary);
if(!in)
{
cout << "Cannot open read file.";
return 1;
}
while(in)
{ // in will be false when eof is reached
in.get(ch);
if(in)
{
cout << ch;
correctStr[count]=ch;
count++;
}
}
correctStr[count]='\0';
fn_revrs(correctStr,revStr,count);
cout<<"\nno of characters in file :"<<count<<endl;
cout<<"correct set of characters in file :\n"<<correctStr<<endl;
cout<<"rev of characters in file :\n"<<revStr<<endl;


Actually the number of characters in a file is 42....But it is taking
and giving the count value as 43,,,


If there are 42 then you ought to create arrays of size 43 and not 41.
Further more the above code is obviously not what you are using (ch is
not declared anywhere). Try to reduce your code to a minimal working
(meaning that we should be able to copy paste it into a file and compile
without any modifications) and then post that.

A though, is the file saved as UTF-8? Windows applications sometimes
start such files with a "byte-order-mark" which won't be seen when
opened in a text editor.
 
F

faz

I used linux g++ 3.2.3 ...In my c++ code i am reading bit values from
a file as character..using the following:
char revStr[41],correctStr[41];
int count=0
ifstream in("config.txt",ios::in | ios::binary);
if(!in)
{
cout << "Cannot open read file.";
return 1;
}
while(in)
{ // in will be false when eof is reached
in.get(ch);
if(in)
{
cout << ch;
correctStr[count]=ch;
count++;
}
}
correctStr[count]='\0';
fn_revrs(correctStr,revStr,count);
cout<<"\nno of characters in file :"<<count<<endl;
cout<<"correct set of characters in file :\n"<<correctStr<<endl;
cout<<"rev of characters in file :\n"<<revStr<<endl;
Actually the number of characters in a file is 42....But it is taking
and giving the count value as 43,,,

If there are 42 then you ought to create arrays of size 43 and not 41.
Further more the above code is obviously not what you are using (ch is
not declared anywhere). Try to reduce your code to a minimal working
(meaning that we should be able to copy paste it into a file and compile
without any modifications) and then post that.

A though, is the file saved as UTF-8? Windows applications sometimes
start such files with a "byte-order-mark" which won't be seen when
opened in a text editor.



But the array should consider 0-41(42 bits)
int main()
{
char ch;
char revStr[41],correctStr[41];
int count=0
ifstream in("config.txt",ios::in | ios::binary);
if(!in)
{
cout << "Cannot open read file.";
return 1;
}
while(in)
{ // in will be false when eof is reached
in.get(ch);
if(in)
{
cout << ch;
correctStr[count]=ch;
count++;
}
}
correctStr[count]='\0';
fn_revrs(correctStr,revStr,count);
cout<<"\nno of characters in file :"<<count<<endl;
cout<<"correct set of characters in file :\n"<<correctStr<<endl;
cout<<"rev of characters in file :\n"<<revStr<<endl;
}

//config.txt

001010000000000000000000110000000000000100

pls suggest me where i am wrong...

regards,
fazal
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

I used linux g++ 3.2.3 ...In my c++ code i am reading bit values from
a file as character..using the following:
char revStr[41],correctStr[41];
int count=0
ifstream in("config.txt",ios::in | ios::binary);
if(!in)
{
cout << "Cannot open read file.";
return 1;
}
while(in)
{ // in will be false when eof is reached
in.get(ch);
if(in)
{
cout << ch;
correctStr[count]=ch;
count++;
}
}
correctStr[count]='\0';
fn_revrs(correctStr,revStr,count);
cout<<"\nno of characters in file :"<<count<<endl;
cout<<"correct set of characters in file :\n"<<correctStr<<endl;
cout<<"rev of characters in file :\n"<<revStr<<endl;
Actually the number of characters in a file is 42....But it is taking
and giving the count value as 43,,,

If there are 42 then you ought to create arrays of size 43 and not 41.
Further more the above code is obviously not what you are using (ch is
not declared anywhere). Try to reduce your code to a minimal working
(meaning that we should be able to copy paste it into a file and compile
without any modifications) and then post that.

A though, is the file saved as UTF-8? Windows applications sometimes
start such files with a "byte-order-mark" which won't be seen when
opened in a text editor.



But the array should consider 0-41(42 bits)

Yes, and the number you write between the brackets is the size of the
array, not the index of the last element. And you have to make space for
the final \0, thus the number is 43. (Could The Hitchhiker's Guide to
the Galaxy been wrong? <G>)

I still can't just copy-paste the code and compile, I first have to
int main()
{
char ch;
char revStr[41],correctStr[41];

Missing ;
ifstream in("config.txt",ios::in | ios::binary);
if(!in)
{
cout << "Cannot open read file.";
return 1;
}
while(in)
{ // in will be false when eof is reached
in.get(ch);
if(in)
{
cout << ch;
correctStr[count]=ch;
count++;
}
}
correctStr[count]='\0';
fn_revrs(correctStr,revStr,count);

I have no idea what fn_revrs() is.
}

//config.txt

001010000000000000000000110000000000000100

pls suggest me where i am wrong...

After having fixed the code and created a config.txt file with the text
above I get 42.
 
B

BobR

/* """ quote

But the array should consider 0-41(42 bits)
int main(){
char ch;
char revStr[41],correctStr[41];
int count=0
ifstream in("config.txt",ios::in | ios::binary);
if(!in){
cout << "Cannot open read file.";
return 1;
}
while(in){ // in will be false when eof is reached
in.get(ch);
if(in){
cout << ch;
correctStr[count]=ch;
count++;
}
}
correctStr[count]='\0';
fn_revrs(correctStr,revStr,count);
cout<<"\nno of characters in file :"<<count<<endl;
cout<<"correct set of characters in file :\n"<<correctStr<<endl;
cout<<"rev of characters in file :\n"<<revStr<<endl;
}
file://config.txt

001010000000000000000000110000000000000100

pls suggest me where i am wrong...
regards, fazal

""" */ quote end

Is this homework?

// --------
// .....
std::ifstream in("config.txt", std::ios_base::in|std::ios_base::binary );
if( not in ){
cout << "Cannot open read file.";
return 1;
}
while( in.get( ch ) ){
cout << ch;
correctStr[ count++ ] = ch;
}
// .....
// --------

If NOT homework:

#include <iostream>
#include <string>
#include <fstream>
#include <algorithm> // for copy, reverse
#include <iterator> // for istream_iterator
// + any I missed <G>

int main(){
std::ifstream in( "config.txt" );
std::string Sdata;
std::copy( std::istream_iterator<char>( in ),
std::istream_iterator<char>(),
std::back_inserter( Sdata ) ); // put file in string

std::cout<<"\nSdata size="<<Sdata.size()<<'\n';
std::cout<<"Sdata="<<Sdata<<std::endl;
if( 8 < Sdata.size() ){
std::cout<<"\nThe 9th char is "<<Sdata.at( 8 )<<std::endl;
}

std::string RevIt( Sdata );
std::reverse( RevIt.begin(), RevIt.end() ); // reverse it
std::cout<<"RevIt="<<RevIt<<std::endl;

return 0;
} // main()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top