1

I have the following task: I have a file (card) with 5 strings:

U98_25984nhdrwedb \n
U98_5647BGFREdand \n
U98_30984bgtjfYTs \n
U77_76498375nnnnn \n
U98_83645bscdrTRF \n

I need to extract to another file image.txt those strings starting with "U9". The below code without the memory assignment (malloc, calloc) print out the codes correctly to the screen, but it does not print the correct data to the image.txt, where I only get "98_25984nhdrwedb@". I think I am applying the memory allocation incorrectly, but when I use malloc or calloc (before the while loop) it gets worse and print out garbage and I cannot figure out how to set this correctly.

#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <stdint.h>
    
typedef uint8_t  BYTE;
    
int main()
{
    FILE *input_card = fopen("card","r");    //open the file for reding
    BYTE data[18];
    int i, n = 5;

    FILE* output = fopen("image.txt","w");    //open the output file for writing

    output = malloc(sizeof(data)*18);         //assign memory
    while (!feof(input_card))
    {
        for (i = 1; i <= n; i++)
        {
            fread(data,sizeof(BYTE),18,input_card);
            if(data[i] != 0)
            {
                if (data[0] == 'U' && data[1] == '9')
                {   
                    printf("data: %s",data);
                    fwrite(&data[i],sizeof(BYTE),18,output);
                }
                fclose(output);
            }
        }
    }
    fclose(input_card);
    free(output);
    return 0;
}
3
  • 2
    Aside from the memory allocation problem you should take a look at Why is “while ( !feof (file) )” always wrong? Commented Nov 18, 2020 at 12:21
  • 1
    You close the output file after the first write access. You do not check how many bytes were read at all. Your usage of if(data[i]!=0) is very weird. This checks the second character in first line, third characater in second line, fourth character in third line... Also for each iteration of your while loop you read 5 times from the file. Commented Nov 18, 2020 at 12:36
  • OT: if this is a text file you should read it line by line with fgets. Commented Nov 18, 2020 at 12:40

2 Answers 2

2

In the following 2 lines from your code, the second line is incorrect:

FILE* output = fopen("image.txt","w");    //open the output file for writing
output = malloc(sizeof(data)*18);         //assign memory <= This is WRONG

The variable output is a FILE pointer. You should not allocate it using malloc. You should only use it if fopen returns it successfully, which means it has already been allocated by fopen.

This means you don't need this:

free(output); // This is also WRONG

Because this already freed the pointer's allocated data:

fclose(output);
Sign up to request clarification or add additional context in comments.

Comments

2

fopen() returns a FILE pointer so when you try to allocate memory (using malloc, that also returns a pointer) you're replacing the pointer to the FILE with something that points to memory instead of the file. Removing

output = malloc(sizeof(data)*18);         //assign memory

should make it just work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.