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

fread and fwrite in C/C++

i am writing a program in c,c++....... I want to read one file and write its contents using fread and fwrite functions. the program i hv written has no errors. but output file is not same as that of read file. main() of program is like this........

Expand|Select|Wrap|Line Numbers
  1. void main()
  2. {
  3.         FILE *readfile,*writefile;
  4.  
  5.         unsigned long ml,mr;
  6.         char ch;
  7.         int noc=0,blen=0;
  8.         long int pos=0;
  9.  
  10.         readfile=fopen("c:\\1.txt","rb");
  11.         writefile=fopen("c:\\2.txt","wb");    
  12.  
  13.         fseek(readfile,0,SEEK_END);
  14.         noc=ftell(readfile);
  15.         fseek(readfile,0,SEEK_SET);
  16.         cout<<endl<<"NOC="<<noc<<endl<<endl;
  17.  
  18.  
  19.  
  20.     while(noc)
  21.     {
  22.         ml=mr=0UL;
  23.  
  24.         for(blen=0;blen<4;blen++)
  25.         {
  26.             fread(&ch,1,1,readfile);
  27.             ml=ml<<8;
  28.  
  29.             if(noc)
  30.             {
  31.                 ml+=ch;
  32.                 noc--;
  33.  
  34.             }
  35.             else
  36.                 ml+=0;
  37.         }
  38.  
  39.         for(blen=0;blen<4;blen++)
  40.         {
  41.             fread(&ch,sizeof(char),1,readfile);
  42.             mr=mr<<8;
  43.  
  44.             if(noc)
  45.             {
  46.                 mr+=ch;
  47.                 noc--;
  48.  
  49.             }
  50.             else
  51.                 mr+=0;
  52.         }
  53.  
  54.         fwrite(&ml,sizeof(ml),1,writefile);
  55.         fwrite(&mr,sizeof(mr),1,writefile);
  56.     }
  57.  
  58. }
  59.  
__________________________________________________ ____________


suppose readfile is having following text......
ABCDEFGHIJKLMNOPQRSTUVWXYZ

1234567890





then output file does not show alphabets and numbers on separate lines.....!!

what should i do to get text same to that of source???
(i know this is very "HUGE" question........!!!!!)
Feb 14 '07 #1
1 5540
questionit
553 512MB
You need to use fgets().

this function reads line by line.
Feb 14 '07 #2

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

Similar topics

8
by: Brady | last post by:
Hi, I'm having a problem reading and writing to a file. What I'm trying to do is read a file, modify the portion of the file that I just read, and then write the modified data back to the same...
2
by: Luc Holland | last post by:
Hey, I'm working on a program that reads a binary file. It's opened with ==== if ((f1=fopen(argv,"rb"))==NULL) { fprintf(stderr,"Error opening %s for reading . . .\n",argv); exit(2); } ====...
4
by: janssenssimon | last post by:
//de structure om de highscores in op de slagen typedef struct score{ char *naam; int veld; int score; struct score *volg; }HIGH; void toonhighscores(void)
2
by: Richard Hsu | last post by:
// code #include "stdio.h" int status(FILE * f) { printf("ftell:%d, feof:%s\n", ftell(f), feof(f) != 0 ? "true" : "false"); } int case1() { FILE * f = fopen("c:\\blah", "wb+"); int i = 5;
8
by: M. Åhman | last post by:
I'm reading "C: A Reference Manual" but still can't understand a very basic thing: is there any functional difference between fgetc/fputc and fread/fwrite (when reading/writing one unsigned char)?...
2
by: elisa | last post by:
Dear all, I have problems in writeing and reading a block of data (long array) with fread and fwrite. If I write and read an integer array, everything looks fine, but when I try long array, sth...
5
by: loudking | last post by:
Dear all, I encountered a problem with fread and fwrite. If I am going to copy a file using the same string, it will succeed char *file_content; struct stat buf; FILE *fp, *new_fp; ...
30
by: empriser | last post by:
How to use fread/fwrite copy a file. When reach file's end, fread return 0, I don't konw how many bytes in buf.
4
by: Highlander2nd | last post by:
Hello there. I'm Andrew Lucas, I'm a programmer for Half-Life. I've been working on stencil shadows lately, and I've been having problems saving mesh data for my models. When I store mesh data, I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.