Help please, storing my FILE

This is a discussion on Help please, storing my FILE within the C Programming forums, part of the General Programming Boards category; i have got a program which loads a file and reads it in char buy char and then print it. ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    20

    Help please, storing my FILE

    i have got a program which loads a file and reads it in char buy char and then print it.

    does anyone no how to change this program so it will store the char's into an array as they are being read.

    Code:
    #include <stdio.h>
    
    int main ()
    {
        
            
            FILE *file = fopen( "key.txt", "r" );
    
            
            if ( file == 0 )
            {
                printf( "Could not open file\n" );
            }
            else 
            {
                int x;
                
                while  ( ( x = fgetc( file ) ) != EOF )
                {
                    printf( "%c", x );
    				
                }
            }
    		printf("\n");
            
    		if 
    		
    		
    		
    		fclose( file );
        
    }
    Any help welcome

  2. #2
    Registered User
    Join Date
    Jun 2007
    Location
    Rome, NY
    Posts
    24
    Just to get you started, you can do this. However, be careful not to overflow your buffers, as this below isn't the best way to do this, but it gives you and idea. It uses a fixed array, but I didn't think your question concerned memory allocation.

    Anyway, this simply saves everything into a buffer, then prints the buffer.

    Code:
    #include <stdio.h>
    #define MAX_BUF 2048
    
    int main ()
    {
            FILE *file = fopen( "test.c", "r" );
    
            char array[ MAX_BUF ];  //a buffer to store up to 2048 characters
    
            if ( file == 0 )
            {
                printf( "Could not open file\n" );
            }
            else 
            {
                int x;
    
    
    	    int num = 0;
                while  ( ( x = fgetc( file ) ) != EOF )
                {
                    // printf( "%c", x );
    
                     // if there is rat least room for an ending NULL byte, then store the character
    		if( num < (MAX_BUF-1) ) {
    		  array[num++] = x;
    		}
    				
                }
            }
             //now dump the buffer to the stdout
    	printf("other buffer: \n%s", array );
    	printf("\n");
            
    
    		
    	fclose( file );
        
    }
    Hope this gets you going. Then from the looks of it your next task is to read that buffer back into another file. I'll leave you to it.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Be warned that that code will probably segfault if the file could not be opened. Moving the last three lines into the else block would solve that problem.

    Also, I don't see a NULL being stored in array anywhere.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 09:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

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