0
#include<stdio.h>

main()
{
    FILE *p,*q;
    char b;
    int a;
    p = fopen("three", "w");
    for(a=1; a<=50; a++)
        fprintf(p, "%c", a);
    fclose(p);

    q = fopen("three", "r");
    while(!feof(q))
    {
        fscanf(q, "%c", &b); //dont print after 25! ?
        printf("%d",b);
    }
    fclose(q);
}

The question is why it does not print after 25. I tried by removing feof also but it showed that only 25th char is read to file. I guess this line is creating some problem!

fprintf(p,"%c",26);

but have no idea why!

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
Sagar Kar
  • 177
  • 1
  • 2
  • 10

2 Answers2

1

There are two reasons for failure. If your system opens files in text mode by default, it will respond to control characters. So you must open the files in binary mode:

p = fopen("three", "wb");
...
q = fopen("three", "rb");

The second failure is the use of the useless feof(). It does not inform when you reach the end of file, but only after you made a bad read beyond the end of file. That is why the 25 or in the corrected case the 50 is printed twice.

Here is the feof() link.

This shows what works

#include<stdio.h>

int main()                              // main needs a return type
{
    FILE *p,*q;
    char b;
    int a;
    p = fopen("three", "wb");           // binary mode
    if (p == NULL)                      // test file open
        return 1;
    for(a=1; a<=50; a++)
        fprintf(p, "%c", a);
    fclose(p);

    q = fopen("three", "rb");           // binary mode
    if (q == NULL)                      // test file open
        return 1;
    while (1 == fscanf(q, "%c", &b))    // test the read result
        printf("%d ",b);
    fclose(q);
    return 0;
}

Program output

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
Community
  • 1
  • 1
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
  • thanks that helped a lot! the text mode respond to control char and binary mode dont.... but y someone down voting my question i have no idea! – Sagar Kar Apr 29 '15 at 19:41
  • @SagarKar please do not overlook the `feof()` issue, follow up the link: http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – Weather Vane Apr 29 '15 at 19:49
  • I found the code works correctly, even if the file is opened in text mode. To me, the problem is the printf statement needs to include a '\n' so the output buffer is flushed. – user3629249 Apr 30 '15 at 20:29
0

here is how I wrote, tested the code.

Notice the corrections to the main function declaration

Notice the addition of the return statement at the end

Notice, just for output formatting, the format string for the printf

the '\n' in the printf format string causes the output buffer to be immediately flushed.

Notice the addition of the error checking after the two fopen statements

#include <stdio.h>
#include <stdlib.h>

int main( void )
{
    FILE *p,*q;
    char b;
    int a;
    if( NULL == ( p = fopen("three", "w") ) )
    { // then fopen failed
        perror( "fopen for write: three failed" );
        exit( EXIT_FAILURE );
    }

    // implied else,  fopen successful

    for(a=1; a<=50; a++)
        fprintf(p, "%c", a);
    fclose(p);

    if( NULL == (q = fopen("three", "r") ) )
    { // then fopen failed
        perror( "fopen for read: three failed" );
        exit( EXIT_FAILURE );
    }

    // implied else, fopen successful

    while(1 == fscanf(q, "%c", &b) ) //dont print after 25! ?
    {
        printf("%d\n",b);
    }
    fclose(q);

    return(0);
} 
user3629249
  • 16,402
  • 1
  • 16
  • 17