Thread: file processing - strange behavior

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    7

    file processing - strange behavior

    This code works, opens file "myfile.txt" for reading and writing and writes a-z characters in the file.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main ( void )
    {
    	int c;
    	FILE * fp;
    	fp = fopen ( "myfile.txt", "r+" );
    	for ( c = 'a'; c <= 'z'; c++ )
        	putc ( c, fp );
    	/*while ( ( c = getc ( fp ) ) != EOF )
    		putchar ( c );*/
    	fclose ( fp );
        return 0;
    }
    This code also works ( reads characters from the file )
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main ( void )
    {
    	int c;
    	FILE * fp;
    	fp = fopen ( "myfile.txt", "r+" );
    	/*for ( c = 'a'; c <= 'z'; c++ )
        	putc ( c, fp );*/
    	while ( ( c = getc ( fp ) ) != EOF )
    		putchar ( c );
    	fclose ( fp );
        return 0;
    }
    But when I uncomment all code (code for writing characters and reading characters ) I get garbage characters both in my file and console window.
    Something like this in console window:
    ═══════════════════════════════════
    ═══════════════════════════════════
    ═══════════════════════════════════
    ═══════════════════════════════════
    ═══════════════════════════════════
    ═══════════════════════════════════
    ═══════════════════════════════════
    ═══════════════════════════════════
    ═══════════════════════════════════...

    and like this in the file:

    abcdefghijklmnopqrstuvwxyzÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ
    ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ...

    This is the code that should only write characters a-z in the file and than read them in console window, but it doesn't work. Why?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main ( void )
    {
    	int c;
    	FILE * fp;
    	fp = fopen ( "myfile.txt", "r+" );
    	for ( c = 'a'; c <= 'z'; c++ )
        	putc ( c, fp );
    	while ( ( c = getc ( fp ) ) != EOF )
    		putchar ( c );
    	fclose ( fp );
        return 0;
    }

  2. #2
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    You have to go back to the beginning of the file in order to read the things that you just wrote to it. Check out fseek: fseek - C++ Reference

    Code:
    fseek(fp, 0, SEEK_SET);

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    17

    Follow Below code

    Hello,
    Code:
    int main ( void )
    {
    	int c;
    	FILE * fp;
    	fp = fopen ( "MyFile.txt", "r+" );
    	if(fp==NULL)
    	{
    	  printf("Unable to open the file");
    	  exit(0);
    	}
    	for ( c = 'a'; c <= 'z'; c++ )
        	putc ( c, fp );  //fp will be pointing to last character
    
                  fseek(fp, 0, SEEK_SET); //Reset it to beginning
    
    	while ( ( c = getc ( fp ) ) != EOF ) //Read from 'a'
    		putchar ( c );
    	fclose ( fp );
        return 0;
    }
    Hope you understood

    Regards,
    Siddu

  4. #4
    Registered User matrixx333's Avatar
    Join Date
    Mar 2009
    Posts
    67
    You could also use:

    Code:
    rewind(fp);
    In place of fseek().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM