file processing - strange behavior

This is a discussion on file processing - strange behavior within the C Programming forums, part of the General Programming Boards category; This code works, opens file "myfile.txt" for reading and writing and writes a-z characters in the file. Code: #include <stdio.h> ...

  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
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    997
    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, 02: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, 05:14 AM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21