Thread: File read in problem

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    10

    File read in problem

    Hi,

    I've made a function that reads in a file by first determining the file size and then reading in the contents, unfortunately for some reason when the contents are read in, additional unwanted and non existent characters are being read in to (characters that aren't in the files contents), I can't see where the problem is, can anyone spot what I've done incorrectly?

    Code:
    void encrypt(char *key,char *fileIn,char *fileOut)
    {
        FILE *pFile;
        long fileSize;
        if( (pFile = fopen ( fileIn, "r")) != NULL )
        {
            fseek (pFile,0,SEEK_END);
            fileSize= ftell (pFile);
        } else {
            printf( "File could not be opened\n" );
       }
        fclose(pFile);
        char message[fileSize];
        
        if( (pFile = fopen( fileIn, "r" )) != NULL )
        {
            fread( message, sizeof( char ), (fileSize), pFile );
            printf( "Number of characters: %d\n", fileSize );
            printf( "Message reads: %s\n", message );
            fclose( pFile );
        }
       else {
            printf( "File could not be opened\n" );
       }
    }
    The function itself is incomplete I'm just trying to get the read in part of it correct first, the idea is that it will read in the contents of a plain text file into an array, alter the data and then output it to separate file (cipher file).

    at the moment when I read in a file containing the word Test, I get:

    Test (which is the contents followed by a square thing then >>-u#E and a few others I can't make on this keyboard.

    Any help is appreciated.
    Last edited by DimensionX; 11-10-2009 at 08:24 AM.

  2. #2
    Registered User rpbear's Avatar
    Join Date
    Nov 2009
    Posts
    18
    this happened because you haven't init the array----message[fileSize]
    add one line after that:
    Code:
    memset(message,0,fileSize);
    second,you can't expect that the fread will read in char as much as fileSize,so you'd do this:
    Code:
    int n = fread( message, sizeof( char ), (fileSize), pFile );
            printf( "Number of characters: %d\n", n );
    don't forget add the header <memory.h>

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    You should allow room for one more character in the array - the terminating zero.

    I don't see why rpbear says you can not expect the number of chars requested.
    All you need to do is place a terminating nul - which would be in location...
    message[fileSize] = '\0';
    Then print it out.

  4. #4
    Registered User rpbear's Avatar
    Join Date
    Nov 2009
    Posts
    18

    Thumbs up

    Quote Originally Posted by nonoob View Post
    You should allow room for one more character in the array - the terminating zero.

    I don't see why rpbear says you can not expect the number of chars requested.
    All you need to do is place a terminating nul - which would be in location...
    message[fileSize] = '\0';
    Then print it out.
    agreed!

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    To clarify, yes, in general you can not depend on the read returning the number of characters requested. After all, in isolation, the count is a maximum only. The file may have less. But in the context above, the count was determined via the file's size.

    There may be another complication... Does the ftell() return the raw file size? I bet it does. Depending on whether it's opened "t" or "b" mode, reads may or may not compact/expand line breaks into one/two characters. This would cause the character count to be different than expected.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    10
    Thank you both very much, it's working perfectly!

    really appreciate all the help you've both given me.

    thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. Read File Problem
    By lonewolf367 in forum C Programming
    Replies: 11
    Last Post: 11-30-2005, 10:32 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM