Strange behaviour of fread() [duplicate] - c

This question already has answers here:
Read and write to binary files in C?
(7 answers)
what's the differences between r and rb in fopen
(5 answers)
Closed 4 years ago.
This is my code:
FILE* fLeftResult = fopen("C:/Users/Vincenzo/Desktop/unina/SOC/progetto esame/elaborazione fir/ResultLowLeft.bin","r+");
short output;
short matlabIdeal[SAMPLES+1] = {0};
size_t returnValue= fread(matlabIdeal, sizeof(short), SAMPLES, fLeftResult);
When SAMPLES is 6077, the array matlabIdeal is filled until the 4095th value. The successive values are 0. And returnValue is 1433.
When SAMPLES is 60772, the array matlabIdeal is filled until the 59391th value. The successive values are 0. And returnValue is 1433.
When SAMPLES is 30772, the array matlabIdeal is filled until the 30719th value. The successive values are 0. And returnValue is 1433.
The values that fread() fills are correct, but suddenly they became 0.
This is the binary file fread reads: https://ufile.io/sf85m
Can you help me with this problem? Or reproduce the code on your computer to see what will happen?

This is because you did not open the file for reading binary data.
fread is treating the stream as though it is text.
You should open the file using the "rb" mode.

Related

Why extra 'ÿ' added when writing into a .txt file in C? [duplicate]

This question already has answers here:
Why is “while( !feof(file) )” always wrong?
(5 answers)
Closed 11 months ago.
I searched about this problem everywhere, but none of the suggested solutions worked for me.
char currentChar;
FILE *fp_read = fopen("../input.txt", "r");
FILE *fp_write = fopen("../textArranged.txt", "w");
while (!feof(fp_read)){
currentChar = fgetc(fp_read);
...
}
I tried to change the while condition (using getc()), but it didn't work.
feof() seems to return 0 after reading the last byte of the file. It returns 1 after fgetc() already made the attempt to read one more byte after the end of the file.
When fgetc() makes the attempt to read data after the end of the file, fgetc() returns -1.
If you perform fputc(x, ...) and x is not in the range 0...255, fputc() will actually write the byte (x & 0xFF).
On nearly all modern computers, (-1 & 0xFF) is 0xFF which equals the character 'ÿ'.
So the following happens:
...
Your program reads the last byte of the file using fgetc()
It writes that character using fputc()
Although there are no more bytes left in the file, feof() returns 0 because you did not make the attempt to read bytes after the end of the file, yet.
Your program calls fgetc() and because there are no more bytes left, fgetc() returns -1.
Your program calls fputc(-1, ...) which writes the character 'ÿ'.
feof() returns 1 because fgetc() already tried to read bytes after the end of the file.

C - fseek in combination with fwrite doesn't overwrite data [duplicate]

This question already has answers here:
What is the difference between "rb+" and "ab" in fopen()?
(1 answer)
Using fseek() with append
(1 answer)
Closed 5 years ago.
I have an array called dicevalues which contains 100 random int values from 1 to 6. It is newly randomly generated everytime I run the program.
I make each value into a char, so I can write it properly into a file. I want to set the pointer of the file to the beginning, so if I run the program again it overwrites the old data. However this doesn't seem to work. Everytime I run the code it only appends. Heck, no matter what values I set fseek to it only appends without overwriting anything.
fseek(fd,0,SEEK_SET);
for(i=0;i<100;i++){
dicechar = (char) (dicevalues[i]+48);//makes a char out of the int
fwrite(&dicechar,sizeof(char),sizeof(augenchar),fd);
};
What am I doing wrong?

Reading specific lines using read function [duplicate]

This question already has answers here:
C Programming - Read specific line from text file
(2 answers)
Closed 5 years ago.
Assume I have a binary file data.bin of 1000 lines that was written using fwrite. For reading it, I just have to do something like this(where data is a double buffer of size 1000):
FILE *fp = fopen("data.bin", "rb");
fread(data, sizeof(double), (1000*sizeof(double),fp);
This will read the entire file but I am looking for reading only the last 500 lines!! This means I have to jump to the line 499 in data.bin and start reading from there until the end. How to do modify the previous fread function to read the last 500 lines?
Thank you.
Are you aware of the fseek() function. You need to set the pointer to begin at the last 500 lines then read until eof.
Look up fseek() and you will be able to solve this problem.
Here is a link for further information on fseek()

Unknown length of input [duplicate]

This question already has answers here:
How can I read an input string of unknown length?
(11 answers)
Closed 7 years ago.
There are several ways how to retrieve string input, e.g getline() , or fgets() but all of them require size of the string as an argument. But what if i want to retrieve string of unknown size? How is it possible using getline() or fgets() in C?
The answer is no. You can't read a string of indeterminate length. You can, however, read one character at a time until you reach the size of the storage space you have allocated in your program. Use fgetc in a loop.
int fgetc(FILE *stream)
Open the stream , read one character at a time, and stop reading when you see your sentinel character, which is probably a newline.

Reading from a binary file using fread() displays additional characters [duplicate]

This question already has answers here:
Why is “while( !feof(file) )” always wrong?
(5 answers)
Closed 8 years ago.
My question is about this fread() function that seems to be confusing for the time being. I create a binary file and put inside of it the values 1,2 and 3. And then I try to read the file and when I do using fread() it shows it like 1233 not 123.
#include <stdio.h>
#include <stdlib.h>
main ()
{
int x=1,y=2,z=3,i,j;
FILE *f;
f=fopen("Werid.bin","wb");
fwrite(&x,sizeof(int),1,f);
fwrite(&y,sizeof(int),1,f);
fwrite(&z,sizeof(int),1,f);
fclose(f);
f=fopen("Werid.bin","rb");
if (!f) perror("X");
while(!feof(f))
{
fread(&j,sizeof(int),1,f);
printf("%d",j);
}
fclose(f);
}
Why?
Change this
while(!feof(f))
to
while(fread(&j,sizeof(int),1,f) == 1)
From linux feof() manual
The function feof() tests the end-of-file indicator for the stream pointed to by stream, returning nonzero if it is set. The end-of-file indicator can only be
cleared by the function clearerr().
The feof() will return true after you try to call fread() at the end of file i.e. after you read the last number, you will need to call fread() again to set the end-of-file indicator.
So the loop will be executed one more time after the last read, and since it does not read anything but rather returns an error, it does not change the value of j either, so the previous value 3 is printed again.

Resources