AllExperts > Experts 
Search      

C

Volunteer
Answers to thousands of questions
 Home · More Questions · Answer Library  · Encyclopedia ·
More C Answers
Question Library

Ask a question about C
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Mohamed Ameer Irshad.H
Expertise
can ask me questions regarding general concepts,algorithms in C.STLs(c++)TSR programmin,virus writing,basic level of device driver programming(linux),network programmming(UNIX C) ,UNIX shell scripting,grapihcs using TURBO C and a bit of hardware interaction..

Experience
just a student level experience but hav worked out of my academic contexts..

Organizations
Vellore Institute of Technology(University Of VIT)

Publications
Technical Publication Of NACISS(National Conference On Computational Intelligence and Security Systems)

Paper titled REMOTE DESKTOP ACCESS USING MOBILE PHONES AND AN ENHANCEMENT FOR INTRA-MOBILE SEARCH got selected in an international conference SNPD 2008,Thailand.And the paper will be published IEEE Journal by August 2008.

Paper titled REMOTE DESKTOP ACCESS USING MOBILE PHONES AND AN ENHANCEMENT FOR INTRA-MOBILE SEARCH got selected in International Conference on Information and Communication Technologies(ICT 2008) Paris,France.The same will be published in Proceedings of World Academy of Science, Engineering and Technology, Volume 30, July 2008. Education/Credentials
did a few courses in comp institutions in C and a few projects

Awards and Honors
Presented Papers in National Conferences

 
   

You are here:  Experts > Computing/Technology > C/C++ > C > C++ binary i/o

Topic: C



Expert: Mohamed Ameer Irshad.H
Date: 3/31/2008
Subject: C++ binary i/o

Question
Hi,
I am trying to write two simple functions, one which writes a single string to a binary file and one which reads the string from the file. I have looked on the internet for how to do it but do I not understand. Please could you help me?

Answer
#include <string.h>
#include <stdio.h>

int main(void)
{
  FILE *stream;
  char msg[] = "hey this the string i wrote in the bin file";
  char buf[20];
//open your .bin file in write mode
  if ((stream = fopen("e:\\myfile.bin", "w+"))
      == NULL)
  {
     fprintf(stderr, "Cannot open output file.\n");
     return 1;
  }

  /* write some data to the file */
  fwrite(msg, strlen(msg)+1, 1, stream);

  /* seek to the beginning of the file */
  fseek(stream, SEEK_SET, 0);

  /* read the data and display it */
  fread(buf, strlen(msg)+1, 1, stream);
  printf("%s\n", buf);

  fclose(stream);
  return 0;
}


you have asked to write these as two functions.,am leaving that to you to give a try.
just place the fwrite() part in a function and the fread() in another.

rest all in the main() function..now call the tow functions in main() and its done :)

but still if you are not able to do get a good output feel free to ask anytime,
all the best
Ameer Irshad

Add to this Answer    Ask a Question



  Rate this Answer
   Was this answer helpful?
Not at allDefinitely              
   12345  

     
About Us | Advertise on This Site | User Agreement | Privacy Policy | Help
Copyright  © 2008 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.