473,722 Members | 2,458 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Read/write problem using fstream in binary mode.

21 New Member
I have a binary file,which contains strings of 30 bytes each.I need to open the file,read the strings one by one and if the string is not found i need to write it.But unfortunately both read and write using fstream is not not working.If i close the file and open it again it works.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <sys/stat.h>
  3. #include <fstream>
  4.  
  5. int main(){
  6. fstream fs;
  7. char write[30]= {"A0000.label"};
  8. char read[30];
  9. struct stat sb;
  10.  
  11.    if((stat ("test.txt",&sb)) > -1 )
  12.     {
  13.      fs.open ("test.txt",ios::in|ios::out|ios::app|ios::binary);
  14.      fs.seekg (0,ios::beg);
  15.  
  16.       while(!fs.read ((char*)(&read),sizeof(read)).fail()){
  17.            if(!strncmp(write,read,30))
  18.            {
  19.             std::cout<<"found"<<std::endl;
  20.             return 1;
  21.            }
  22.      }
  23.  
  24.  fs.seekg (0,ios::end);
  25.  fs.write((char*)(&write),sizeof(write))         >>> Here write fails  
  26.  fs.close();
  27. }
  28. return 1;
  29. }
Oct 28 '06 #1
1 5490
Banfa
9,065 Recognized Expert Moderator Expert
I think that when fs.read reaches the end of file it set's some sort of flag that causes all further file operations to fail.

You need to clear that flag before continuing and there is a member function to do that.
Nov 1 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

17
10527
by: Guyon Morée | last post by:
what is the difference? if I open a text file in binary (rb) mode, it doesn't matter... the read() output is the same.
7
1779
by: Christian Henke | last post by:
Hi! maybe someone can help me?! I want to use fstream to read an .au file . This .au file includes multiple information in its header. a MagicNumber, the No of Channels used, and so on. In hex code the data is 2e736e64 (the magic number). But when i read the number with fstream i get 646e732e. Why are the numbers messed up in order?! I am using g++ 2.95.
2
6085
by: dumboo | last post by:
hi there i m using fstream object for reading through and writing to my file, using following code char c; while(!m_aa.eof()) { m_aa.read(&c, sizeof(c)); m_oo.write(&c, sizeof(c));
6
3289
by: Jon Slaughter | last post by:
I'm trying to replace some bytes in a file using fstream but all I seem to be able to do is append or 0 the file then write... Basicaly I just need to replace the first 512 bytes of the file with some buffer(but eventually I will need to replace random blocks in the file at different locations). Is there anyway to do this efficiently using fstream? (I know I can duplicate the file, but it seems a waste to rewrite the same data for no...
2
2410
by: zhushenli | last post by:
Hello all, How to random read/write the file in fstream? BTW, how to access the file in block? Regards, Davy
8
23904
by: a | last post by:
I have a struct to write to a file struct _structA{ long x; int y; float z; } struct _structA A; //file open write(fd,A,sizeof(_structA)); //file close
3
18960
by: nicolasg | last post by:
Hi, I'm trying to open a file (any file) in binary mode and save it inside a new text file. After that I want to read the source from the text file and save it back to the disk with its original form. The problem is tha the binary source that I extract from the text file seems to be diferent from the source I saved. Here is my code: 1) handle=file('image.gif','rb')
6
3757
by: cooldisk | last post by:
Is it possible at all to read a binary file larger than 2GB on a 32- bit system? I tried the following: #include <iostream> #include <fstream> using namespace std; int main(int argc, char *argv) { fstream fsBin; fsBin.open(argv, fstream::in | fstream::binary);
1
3927
by: Sachin Garg | last post by:
I have a program which opens a fstream in binary input+output mode, creating the file if it doesn't exists. But writing doesn't works after reading, it must be something obvious that I am not aware of. f.open(filename,ios::in | ios::out | ios::binary | ios::trunc) The program flow is 1) write some data 2) read the data
0
8740
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9239
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...
0
9090
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
8059
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
5996
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4503
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...
1
3208
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2606
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2148
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.