'expected parameter declarator'. working with files in c

126 Views Asked by At

More less it's my first time working with files in C, having a hard time understanding fread, fwrite and all that jazz. So I was trying to copy the header of the file saved in input folder to another file, stored with the output pointer. I keep getting an error on line 47 about & read; "expected parameter declarator". Overall whatever I do to read the compiler screams at me. And Im not sure if fseek was necessary. Any advice?

  fseek(input, HEADER_SIZE, SEEK_SET);
    int read;
    fread(&read, HEADER_SIZE, 1, input);
    fseek(input, HEADER_SIZE, SEEK_SET);

    for (int i = 0; i < HEADER_SIZE; i++)
    {
        uint8_t fwrite(read[i], sizeof(int), 1, output);
        fseek(input, i++ ,SEEK_CUR);

    }

3

There are 3 best solutions below

1
Janith  Nanayakkara On

The error "expected parameter declarator" on line 47 is because you're missing the address of the variable you want to write to. The fread function expects a pointer to the memory location where the data should be read into. In your case, you should pass the address of the read variable to read:

fread(&read, HEADER_SIZE, 1, input);

This will read HEADER_SIZE bytes from the input file into the read variable.

The fseek function is used to seek to a specific position in a file. In your code, you're using it to seek to the beginning of the header after reading it into the read variable. This is necessary because you want to start writing the header to the output file at the beginning.

fseek(input, HEADER_SIZE, SEEK_SET);
int read[HEADER_SIZE]; // Declare an array to store the header data
fread(read, HEADER_SIZE, 1, input); // Read the header data into the array
fseek(input, HEADER_SIZE, SEEK_SET); // Seek to the beginning of the header again

for (int i = 0; i < HEADER_SIZE; i++) {
    fwrite(read[i], sizeof(uint8_t), 1, output); // Write each byte of the header to the output file
}
1
n. m. could be an AI On

There are some problems with your code. (That was an understatement).

Here is more or less how it could have looked:

char buffer[HEADER_SIZE]; // or perhaps int buffer[HEADER_SIZE];

if (fread(buffer, sizeof(buffer), 1, input) != 1)
{
   // do something about the error
}
else if (fwrite(buffer, sizeof(buffer), 1, output) != 1)
{
   // do something about the error
}

Notes.

  1. You only need fseek if you read or write the file out of order.

  2. Always check the return value of every library function you call.

  3. Pay close attention to types and sizes. The compiler won't stop you from doing nonsensical thing like trying to read 1000 bytes into a single int. Prefer sizeof over hardcoded values, and sizeof(variable) over sieof(type). Assuming buf is an array (not a pointer), here are some safe forms:

      fread(buf, sizeof(buf), 1, input); 
      fread(buf, 1, sizeof(buf), input);
      fread(buf, sizeof(buf[0]), sizeof(buf)/sizeof(buf[0]), input);
    
0
Francesco On

I don't really understand, i will try to clarify some point but you can find better and more detailed answers:

For example:

  1. you declare int read, but right after you use it as an array: read[i];

  2. fseek(), if input is a FILE *, will sets your "position" inside the file from SEEK_SET to a certain offset: HEADER_SIZE; Is that what you want?

  3. after you perform fread the "position" is already "updated" you don't need to move it with fseek, you should also check the return value of that call. It doesn't distinguish if an error occurred or the eof is reached so you should check it after you perform the fread() call, so if for example you read a buffer of 100 char but in the file you reach the eof that file return as if an error occurred and you lose the last line;

  4. uint8_t fwrite(read[i], sizeof(int), 1, output) <--- here you are trying to store the value that fwrite returns, but you miss something after uint8_t;