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

ifstream character read problem

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

Aug 21 '07 #1
1 3642

faz <fa*********@gmail.comwrote in message...

/* """ 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()

--
Bob R
POVrookie
Aug 21 '07 #2

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

Similar topics

2
by: Dave Johnston | last post by:
Hi, I'm currently trying to create a wrapper that uses C functions but behaves like ifstream (from fstream.h) - this is because the platform I'm using (WinCE) doesn't support streams and this is...
1
by: wtnt | last post by:
Hello. I've searched all over and haven't seen another thread with this problem. Please bear with me as I try to explain. thanks. :) I have some programs that need to be cross-platform...
6
by: csvka | last post by:
Hello, I wonder if I could pick your brains. I'm beginning to learn about C++. I have opened a file in my program and I want to read lines from it. I would like this to be done in a separate...
5
by: Jacek Dziedzic | last post by:
Hi! Consider the following program #include <fstream> #include <iostream> using namespace std; int main() { ifstream in("test.txt");
1
by: inkapyrite | last post by:
Hi all. I'm using ifstream to read from a named pipe but i've encountered an annoying problem. For some reason, the program blocks on reading an ifstream's internal buffer that's only half-filled....
6
by: Dave | last post by:
In .Net 2003 if a line, read from a text file is larger than a size parameter, the ifstream getline(buff, sze) put the file pointer to the EOF, so next peek() returns EOF. I saw this problem...
3
by: toton | last post by:
Hi, I want to unread a few things while I am reading a file. One solution I know is putback the characters read to the buffer. But as I need frequent moving file pointer , to a few steps back, I...
5
by: mastern200 | last post by:
I need to make a program where it can read from a file and write to it. But the problem is, all i can do is read from it and not write to it. This is the code i have so far: # include <iostream>...
7
by: Boltar | last post by:
Hi I'm using ifstream (which I hardly ever use) to read an ascii text file (containing numbers delimited by newlines) in a loop using something like: ifstream infile("filename") int value; ...
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
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...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.