FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


This C code is little unclear to me.





shamil
Code:

#include<conio.h>
#include<stdio.h>

FILE *f;
main()
{
   clrscr();
   f=fopen("sham.txt","w");
   fputc(65,f);
   fputc(65,f);
   fputc(65,f);
   fputc(26,f);
   fputc(65,f);
   fputc(65,f);
   fputc(65,f);
   fclose(f);
   f=fopen("sham.txt","r");
   do{ printf("%c",fgetc(f));}while(!feof(f));
   fclose(f);
   getch();
return 0;
}


Do you know why this code doesn't work unless I change the open mode to(wb,rb)?
In both when I open the file I get same content. But, in above code it recognizes the char 26 as end of file(→) and everything after that become unreachable. Why ? If it doesn't work with that char why writes it to file and then can't read it.
elekis
try that

Code:

#include<conio.h>
#include<stdio.h>

FILE *f;
main()
{
   clrscr();
   f=fopen("sham.txt","w");
   fputc(65,f);
   fputc(65,f);
   fputc(65,f);
   fputc(26,f);
   fputc(65,f);
   fputc(65,f);
   fputc(65,f);
   fclose(f);
   f=fopen("sham.txt","r");
   while(!feof(f)){
                  printf("%c",fgetc(f));
                          }
   fclose(f);
   getch();
return 0;
}

shamil
Please read carefully before you post.
Naif
If you dont specify 'b', by default the file is opened in text mode. I guess you need to open a file in binary mode to read the special characters like the TAB character, i.e. 26 etc.
vignesh_natraj
Try using two different file pointers.Why use one. Question
Why should you use binary mode
v3nd0r
hm i ran it a all it's ok Smile
I ran it on linux, must get comment #include <conio.h> , clrscr() and getch()

But all it's oki for me , it it writes A and it read it, and output Razz

get install linux.
adam4u
Try out this an watch :
--------------------------
#include<conio.h>
#include<stdio.h>
#include<fcntl.h>

FILE *f;
main()
{
clrscr();
f=fopen("sham.txt","w");
fputc(65,f);
fputc(65,f);
fputc(65,f);
fputc(26,f);
fputc(65,f);
fputc(65,f);
fputc(65,f);
fclose(f);
/********************************************/
/*** Please include <fcntl.h> for raw I/O ***/
{
int fd, err; unsigned char c;

fd=open("sham.txt", O_RDWR);
while (err=read(fd, &c, 1)>0) printf(" %02d", c); printf("\n");
if(err<0) printf("Read Err: %d\n",err);
close(fd);

fd=open("sham.txt", O_RDWR | O_BINARY);
while (err=read(fd, &c, 1)>0) printf(" %02d", c); printf("\n");
if(err<0) printf("Read Err: %d\n",err);
close(fd);
}
/********************************************/
f=fopen("sham.txt","r");
do{ printf("%c",fgetc(f));}while(!feof(f));
fclose(f);
getch();
return 0;
}

Reason:
It is not TAB=0x09 which stops the read stream
it is SUB=0x1A it causes here actually an end of file,
but of course it does not if you open the file in BINARY mode.
shamil
I do not want to open file in binary mode becaue it needs more space than text file. Let me explain the whole story.
I have written a program that compresses text file using Huffman algorithm. So i read each symbol from text file, find its frequency and then generate a code for it. Then i group this code with 8 bits in each group then find the corresponding symol for it then write this symbol to compressed file. It is inevitable that i can get the file end symbol from a group and write it to the file. So if I wrote it to text file when i read it it recognizes it as end of file and stops reading. And if I open it in binary mode it takes more space and sometimes more space than the original file. That's what my problem is.
Related topics
C++ TUTORIAL : INTRODUCTION TO C++
What's wrong with this C code???
C++
www.poxyproxy.com is BACK!!!- Access blocked sites@school
C++ code help
C++ code
Explaination about this C++ code needed
Howcome the universe is larger than it's old?
C++ Codes Tips Tricks for competition...
Funny with C code.
Newsletter E-mails
C++ code problem
10 frih$ contest: what does this c++ code do?
Slight problem with C code
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.