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........ -
void main()
-
{
-
FILE *readfile,*writefile;
-
-
unsigned long ml,mr;
-
char ch;
-
int noc=0,blen=0;
-
long int pos=0;
-
-
readfile=fopen("c:\\1.txt","rb");
-
writefile=fopen("c:\\2.txt","wb");
-
-
fseek(readfile,0,SEEK_END);
-
noc=ftell(readfile);
-
fseek(readfile,0,SEEK_SET);
-
cout<<endl<<"NOC="<<noc<<endl<<endl;
-
-
-
-
while(noc)
-
{
-
ml=mr=0UL;
-
-
for(blen=0;blen<4;blen++)
-
{
-
fread(&ch,1,1,readfile);
-
ml=ml<<8;
-
-
if(noc)
-
{
-
ml+=ch;
-
noc--;
-
-
}
-
else
-
ml+=0;
-
}
-
-
for(blen=0;blen<4;blen++)
-
{
-
fread(&ch,sizeof(char),1,readfile);
-
mr=mr<<8;
-
-
if(noc)
-
{
-
mr+=ch;
-
noc--;
-
-
}
-
else
-
mr+=0;
-
}
-
-
fwrite(&ml,sizeof(ml),1,writefile);
-
fwrite(&mr,sizeof(mr),1,writefile);
-
}
-
-
}
-
__________________________________________________ ____________
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........!!!!!)
1 5540
You need to use fgets().
this function reads line by line.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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);
}
====...
|
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)
|
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;
|
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)?...
|
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...
|
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;
...
|
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.
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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...
| |