简体   繁体   中英

Allocating memory from a file, then using fread and fwrite C

I'm having a bit of trouble getting my code to work, which is to open a file, count the number of characters in it, and then allocating that using malloc() . And then I am supposed to read the characters in from one file (mine contained "Hello World!") using fread() , and write them to a blank .txt file using fwrite .

My code so far is printing corrupted characters. I couldn't find any questions that were specific enough to my problem. If anyone could tell me what I'm doing wrong I'd appreciate it. I think it is specific to my fread and fwrite calls, but nothing I've tried works.

The code in question (not commented yet, sorry!):

#include <stdio.h>
#include <stdlib.h>

//initialized using ./a.out in.txt out.txt

int main(int argc, char *argv[])
{
    FILE *fp;
    int count, end;
    char *memory;
    char c[64]; 

    fp = fopen(argv[1], "r");

    if((fp) == NULL)
    {
        printf("Error: cannot open file.\n");
    }

    else
    {
        while((fgetc(fp))!= EOF)
        {
            count++;    
        }

        memory = (char*)malloc(count);

        c[64] = fread(memory, sizeof(char), 1, fp);
        fclose(fp);

        fp = fopen(argv[2], "w");
        fwrite(c, sizeof(char), sizeof(c), fp); 

        fclose(fp);     
        free(memory);
    }       
    return 0;   
}

code have logical mistakes, as follow

  1. Initialise variables int count= 0 , char c[64]= {0};
  2. Type cast not required memory = malloc(count);
  3. First you have counted number of char in file so Before reading again file rewind it by fseek(fp,0,SEEK_SET);
  4. c[64] = fread(memory, sizeof(char), 1, fp); In this if you are reading single char , you should read complete file , To read complete file do fread(memory, 1, count, fp); and c[64] is out of bound and fread return the number of char successfully read .

  5. fwrite(c, sizeof(char), sizeof(c), fp); In this you are writing complete char array to file but you have read only single variable in array which number of char read . So you are writing uninitialised char array to file. so you are getting corrupted character in file.

  6. To write in file do fwrite(memory, 1, count, fp);

To solve problem ,avoid above error and read complete file in char array and then write.

Alright, there are a number of problems here, I'll start with the least bad:

memory = (char*)malloc(count);

Casting the return of malloc() is unnecessary and can potentially mask errors, for more info see here .

int count;

You never initialise count to anything. This is undefined behaviour and there is no guarantee it'll start at 0. It can start at random garbage left in memory. Same for end

    c[64] = fread(memory, sizeof(char), 1, fp);

2 Problems here. c[64] is out of bounds for the array c since indexes start at 0, so the last element in the array is c[63] . sizeof(char) is defined to be 1, so use 1 instead. Further, fread() returns the amount of characters read, so not sure what you are trying to do with that value even.

fwrite(c, sizeof(char), sizeof(c), fp);

You're writing a complete uninitialised array to the file (=garbage)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM