1

Had an idea to test that if the given char * doesn't pass the assigned malloc size, however I'm not sure if this a great way to check.

Note: sorry for the messy code, having problems indenting.

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

bool start_or_reset(int allocated_memory_lenght_size){

printf("allocated_memory_lenght_size: %d\n\n", allocated_memory_lenght_size);
int allocated_memory_lenght_reached = 0;
int line_count = 0, i = 0;

char line[255];
char * file_contents = malloc(allocated_memory_lenght_size);
file_contents[0] = 0;

if (!file_contents){
    perror("Malloc failed. Exiting.\n");
    exit(1);
}

FILE * fp1 = fopen("read_from_this_file.txt", "r");
FILE * fp2 = fopen("read_from_this_file.txt", "r");

if (fp1 == NULL || fp2 == NULL){
    perror("The file doesn't exist. Exiting.\n");
    exit(1);
}

while (fgets(line, sizeof(line), fp1) != NULL){
    line_count++;
}
printf("line count: %d\n", line_count);

while (fgets(line, sizeof(line), fp2) != NULL){
    
    allocated_memory_lenght_reached += strlen(line);
    i++;

    // Add +1 for allocation if \0 is met:
    if (i == line_count)
        allocated_memory_lenght_reached += 1;

    printf("allocated_memory_lenght_reached: %d\n", allocated_memory_lenght_reached);

    if (allocated_memory_lenght_reached > allocated_memory_lenght_size)
        return 1;
    
    strcat(file_contents, line);
}

printf("\nfile_contents:\n\n%s\n", file_contents);

free(file_contents);
fclose(fp1);
fclose(fp2);
return 0;
}

int main(){

bool malloc_is_valid;
int allocated_memory_lenght_size = 56;

// Step 1
// Check if malloc is enough or not, then return 0 or 1
malloc_is_valid = start_or_reset(allocated_memory_lenght_size);

printf("\n0: Malloc successful, 1: Malloc unsuccessful\n");
printf("malloc_is_valid returns: %d\n", malloc_is_valid);

return 0;
}

New output of console: enter image description here

What I've changed: I made sure it checks for \0 last line in the last line of char * file_contents and adds +1 to required allocation size in allocated_memory_lenght_reached. I've also updated the image to the new output.

I hope this better and closer to the end goal of checking that if it's okay to allocate memory to or not.

Appreciate all the help. Thanks!

HamzaKerem
  • 121
  • 1
  • 8
  • You have to set the first byte of `file_contents` to `0` before the loop to make it usable by `strcat`. – mch Jul 10 '20 at 11:40
  • Does your code work if you pass more than 59 instead of exactly 59 characters? – Thomas Jager Jul 10 '20 at 11:42
  • 1
    You need the +1 for the `0` terminator in `file_contents` only once, not at each iteration. There is only 1 at the end, not at the end of each line. – mch Jul 10 '20 at 11:42
  • To check that the file fits into the provided space, you can use `fseek()` and `ftell()` to obtain the file's size. It is much simpler than your algorithm. -- Oh, that's not quite true on Windows, as line terminators are composed of two characters on *that system*. – the busybee Jul 10 '20 at 11:47
  • @mch Ah I knew it! Edited just now and no differences has occurred when I run it. Added: file_contents[0] = 0; – HamzaKerem Jul 10 '20 at 11:49
  • `// +1 due to \0` - you are adding a `\0` _for each line_. You should be adding a single `\0` on the end of whole file. There is only one single `\0` - on the end of the string. I do not understand. What does it mean "malloced size is reached or not"? If you want to know the size of a file in bytes, it's typical to just `fseek` the file to the end and `ftell` the position. Is this a duplicate of https://stackoverflow.com/questions/8236/how-do-you-determine-the-size-of-a-file-in-c ? – KamilCuk Jul 10 '20 at 11:50
  • @ThomasJager Yup works perfect with any number equal or greater to 59 or whatever else the allocated_memory_lenght_reached must be for it to work properly. – HamzaKerem Jul 10 '20 at 11:50
  • @mch oh good point with the \0, missed out on that let me change it 1 sec. – HamzaKerem Jul 10 '20 at 11:51
  • Why `return 1;` if your allocation limit is reached instead of calling `realloc()`? – David C. Rankin Jul 10 '20 at 11:53
  • 1
    Why not: `fread(file_contents, 1, allocated_memory_lenght_size, fp)` and then `feof(fp)` to check that eof was reached? – Paul Hankin Jul 10 '20 at 11:54
  • @DavidC.Rankin just curios + I tried it with realloc first and messed up. – HamzaKerem Jul 10 '20 at 11:56
  • 1
    I ask because when reading with `fgets()`, all you need to is read until the sum of the times you fill line +1 is greater than your `allocated_memory_lenght_size` (allocation too small) or you read a `'\n'` before that point (allocation okay). – David C. Rankin Jul 10 '20 at 11:58
  • Also: repeated `strcat` causes quadratic runtime (although if your buffer is only 59 bytes long, this is not likely to be a problem). – Paul Hankin Jul 10 '20 at 12:02
  • Please recheck the code, I've updated it to be more error-free (hopefully). – HamzaKerem Jul 10 '20 at 12:19
  • @mch Finally updated. please lmk what you think. – HamzaKerem Jul 10 '20 at 12:23
  • @mumcuhkm34 I thought about `allocated_memory_lenght_reached ` starting at `1` instead of `0` instead of reading the file twice. – mch Jul 10 '20 at 13:05

0 Answers0