Miguel J.
Miguel J.

Reputation: 337

When reading from a file I get special characters, that are not in my text file

For starters, I don't know if it has to do with my OS by I am on OSx. When running my program and printing out what I read from the text file I get the following:

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 
a b c d e f g h i j k l m n o p q r s t u v w x y z\353`\232\377

I am reading from a new text document I created using text wrangler, I even turned on invisibles to make sure there wasn't anything at the end of the file. The code I am using to read from the text file is here:

// Parses text from file, then passes parsed text to writeAndEncode for writing          and encryption. 
int encode(char *fileRead, char *fileWrite) {

    // Get the file
    FILE *fileToRead = fopen(fileRead, "r+"); // Open file for reading
    char *textWithinFile;

    // Check if the file can be read
    if (fileRead != NULL) {
        // File can be read.

        // Get length of file
        fseek(fileToRead, 0L, SEEK_END);
        size_t size = (size_t)ftell(fileToRead);
        rewind(fileToRead);
        // Make sure no error in finding size of file.
        if (size != -1) {
            long charSize = sizeof(char);
            textWithinFile = malloc(sizeof(char) + size);
            // Add text from fileToRead to char array
            fread(textWithinFile, charSize, size, fileToRead);
            // Add null at end to make file string
            textWithinFile[size] = '\0';
            printf("%s\n", &textWithinFile[0]);
            fclose(fileToRead); // Debugging to find out what is being read

            writeAndEncode(size, textWithinFile, fileWrite);
            free(textWithinFile);
        } else {
            //File Can't be read
            printf("***ERROR_FILE_TO_READ_SIZE_CAN_NOT_BE_FOUND***\n");
        }
    } else {
        printf("***ERROR_FILE_TO_READ_CAN_NOT_BE_READ***\n");
    }

    return 0;
}

Would I have this same problem if I was on Windows?

Anyhow thank you for any help!

Upvotes: 0

Views: 1827

Answers (3)

Andrew
Andrew

Reputation: 56

I've fixed up your code a little and added some debugging statements. It'll save the contents of the input file into an malloc'ed buffer of size=(charSize*filesize+1), with the +1 bit to hold the null terminating character. It works on my machine with reasonably sized binary files

You can uncomment the printf(buffer_copy) statement to get what you were doing before. Otherwise, it'll now loop through each byte in the buffer and output as its hexadecimal equivalent. If you're still getting that 'junk' then it's just part of your input file and not a bug.

//get the file
FILE *infp=fopen(infile,"r");
if(infp!=NULL){
    //get length of file
    fseek(infp,0L,SEEK_END);
    size_t filesize=(size_t)ftell(infp);
    printf("file length = %d\n",(int)filesize); //debug statement
    rewind(infp);
    if(filesize>0){ 
        char * buffer;
        buffer=(char*)malloc((sizeof(char)*filesize)+1); // +1 for null char at the end
        size_t chars_read=fread(buffer,sizeof(char),filesize,infp);
        printf("chars read = %d\n",(int)chars_read); // debug statement (chars_read should equal filesize)
        buffer[filesize]='\0'; // properly terminate the char array
        fclose(infp);
        //output what you read (method 1, print string)
        //char *buffer_copy=buffer; 
        //printf("the file=\"%s\"",buffer_copy); //uncomment these two statement to do what you did before */
        //output what you read (method 2, byte-by-byte)
        if(chars_read>0){
            int i;
            for(i=0;i<chars_read;i++){
                char the_char = *buffer;
                printf("char%d=%02x\n",i,the_char); //output each byte as hexadecimal equivalent
                buffer++;
            }
        } else { printf "problem with fread"; }
    } else { printf("problem with filesize"); }
else { printf("problem opening the file"); }

The while loop would stop reading at the first null terminating char. The for loop will now read every single byte inside the file (in case you're trying to peek inside something that isn't necessarily .txt, like .jpg)


Have you tried checking the file from the command line to make sure it only has the characters you expect?

For example, by running the command od -c to view each byte as its ASCII equivalent (or octal, if non-printable).

Upvotes: 2

Miguel J.
Miguel J.

Reputation: 337

Turns out Xcode was acting up for me. I tried compiling this with terminal and it worked out fine. Thank you for you help everyone.

Upvotes: 0

pm100
pm100

Reputation: 50180

You need to un coment that trailing '\0' append

//textWithinFile[size] = '\0';

Upvotes: 1

Related Questions