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

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?

Related

I can't read and display txt file [duplicate]

This question already has answers here:
Why is “while( !feof(file) )” always wrong?
(5 answers)
Closed last month.
The community is reviewing whether to reopen this question as of 22 days ago.
I'm trying to read a file from txt docs and display the data, but when I run the code, it displays nothing and just shows like this:
enter image description here
I'm not sure where the problem is with my code, but perhaps the problem is because I need to display 4000 columns of data so the program cannot read and display it. I tried to use another sample txt with 10 columns of data and the code worked but not with 4000 columns of data.
This is my code :
char str [101][101];
FILE *fin = fopen ("test.txt", "r");
int count = 0;
while (!feof(fin)){
fscanf (fin, "%s", str[count]);
count++;
}
fclose(fin);
for (int i = 0 ; i < count ; i++){
printf("%s\n", str[i]);
}
This is data I want to read and display (4000 columns data)
Bali
Paris
Japan
Nepal
India
Rusia
Malaysia
Thailand
England
etc....
Thank you in advance!!
I haven't tried to run the code.
You read a file line by line and put every line within str. However, str can only contain a maximum of 101 lines, with each iteration not exceeding 100 chars.
Increase the value of str to at least 4000.
str[4500][101]
EDIT:
The question has been flagged as duplicate; however, the problem here is linked to how the C language works.
In this example, we have a static two-dimensional array for which C reserved some memory on the stack. As C tries to use more memory than allocated, the O.S. will inform the process that it does not have access to the location. Which makes C raise an exception at runtime.
I think bringing this precision to those who learn C is essential to avoid confusion.

Bufferoverflow, snprintf instead char resizes? [duplicate]

This question already has answers here:
Undefined, unspecified and implementation-defined behavior
(9 answers)
No out of bounds error
(7 answers)
Closed 3 years ago.
I have a hard time to understand why the below code is not resulting in a bufferoverflow and instead some how seems to resize the char example from 1 to 16.
I checked the snprintf documentation but nothing to be found about this.
//set char example size 1
char example[1];
//set example to args->arg2 which is a 15 character + 1 null byte.
//trying to put something to big into something too small, in my mind causing not a resize but a bof.
snprintf(example, 16, "%s", args->arg2);
fprintf(stdout,"[%s],example);
The fprintf in the end does not display 1 character nor does char example overflows but instead it seems to be resized and displays the full string of 16.
What am i misunderstanding here ?
Your array is not resized. Instead what happens is that there is some memory following it (in fact it's your call stack, which is why overruns like this are dangerous), and snprintf 'trusts' you and writes into that memory. fprintf after that happily reads whatever snprintf wrote there.
It works for you now, but it is undefined behavior, which means that, sooner or later, it will break.

Strange behaviour of fread() [duplicate]

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.

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()

Sprintf function + order arguments [duplicate]

This question already has answers here:
Can the input and output strings to sprintf() be the same?
(2 answers)
Closed 7 years ago.
I have two string ta and tb with certain value, then I use the function sprintf to concatenate the two both in the variable ta, when I write
sprintf(ta,"%s+%s",ta,tb);
I get the string 1+2. but I need store in ta the string 2+1 then I trying
sprintf(ta,"%s+%s",tb,ta);
but I get the string 2+2+2+2+. I don't understand why happens that, Could you help me please?. Below the complete code
int main() {
char ta[5];
char tb[5];
sprintf(ta,"%d",1);
sprintf(tb,"%d",2);
sprintf(ta,"%s+%s",ta,tb);
//sprintf(ta,"%s+%s",tb,ta); uncomment for the second case
printf("taid:%s",ta);
}
sprintf(ta,"%s+%s",ta,tb);
sprintf(ta,"%s+%s",tb,ta);
Both lines of calling sprintf have undefined behavior. You are trying to copy ta to ta itself.
C11 §7.21.6.6 The sprintf function
The sprintf function is equivalent to fprintf, except that the output is written into an array (specified by the argument s) rather than to a stream. A null character is written at the end of the characters written; it is not counted as part of the returned value. If copying takes place between objects that overlap, the behavior is undefined.

Resources