473,779 Members | 2,047 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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..usin g 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(correc tStr,revStr,cou nt);
cout<<"\nno of characters in file :"<<count<<endl ;
cout<<"correct set of characters in file :\n"<<correctSt r<<endl;
cout<<"rev of characters in file :\n"<<revStr<<e ndl;
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 3663

faz <fa*********@gm ail.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(correc tStr,revStr,cou nt);
cout<<"\nno of characters in file :"<<count<<endl ;
cout<<"correct set of characters in file :\n"<<correctSt r<<endl;
cout<<"rev of characters in file :\n"<<revStr<<e ndl;
}
file://config.txt

001010000000000 000000000110000 000000000100

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_bas e::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_iterato r
// + any I missed <G>

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

std::cout<<"\nS data size="<<Sdata.s ize()<<'\n';
std::cout<<"Sda ta="<<Sdata<<st d::endl;
if( 8 < Sdata.size() ){
std::cout<<"\nT he 9th char is "<<Sdata.at ( 8 )<<std::endl;
}

std::string RevIt( Sdata );
std::reverse( RevIt.begin(), RevIt.end() ); // reverse it
std::cout<<"Rev It="<<RevIt<<st d::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
13780
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 the easiest way to take a huge project across onto it. Basically, I've hit a problem. I have no idea how the ifstream class handles directories. In the code I have (which I didn't write), there are several places whereby an ifstream stream is...
1
3614
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 compatible (unix and windowsXP). The first program parses a text file and records where snippets are in terms of where it begins (char offset from begin of the file) and length (number of chars). One can almost use "byte" and "char" interchangeably...
6
4285
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 function called readline() because I would also like to do some processing on the line each time (ignoring comments and so on). I have:
5
6035
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
6252
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. Only when the buffer becomes full does it resume execution. Here's my test code for reading from a pipe: //(compiled with g++ -std=c++98) //--------------------------------------------- #include <iostream>
6
5296
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 also when size was 2000 but line was 1200 bytes long. There is no such problem with .Net 2002 For .Net 2003 I used : #include <string> #include <fstream>
3
7242
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 was thinking of seekg & tellg function. But it is not workings as I expect.... here is the test code, std::string word; std::string value;
5
2677
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> # include <fstream> # include <cstdlib> # include <iomanip> # include <string> # include <vector>
7
3233
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; infile >value;
0
9632
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10302
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10136
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10071
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9925
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8958
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5501
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3631
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.