Why fwrite doesn't work in writing binary mode? - c

int main()
{
FILE* bfp = fopen("student.dat", "wb");
int n = 1452;
fseek(bfp, 0, SEEK_SET);
fwrite(&n, sizeof(int), 1, bfp);
FILE* rfp = fopen("student.dat", "rb");
int read = 0;
fread(&read, sizeof(read), 1, rfp);
printf("%d", read);
fclose(bfp);
fclose(rfp);
return 0;
}
Why do I get 0 when I print it out?
I tried "w" mode, but it didn't work. How can I enter it?

Related

I want to save quantized int8 weights from a convolutional neural network, are my code edits in c correct?

Here is a post I made on the GitHub page: https://github.com/AlexeyAB/darknet/issues/1405#issuecomment-927394677
Here is all the source code: https://github.com/AlexeyAB/yolo2_light/tree/781983eb4186d83e473c570818e17b0110a309da/src
The closing bracket mistake is easy to fix. I noticed that I got outputs when I added an & to the multipliers. Is & the correct pointer for structs? I put the function in additionally.c, referenced it in additionally.h, and officially referenced it in main.c.
void save_weights_int8(network net, char *filename, int cutoff)
{
fprintf(stderr, "Saving weights to %s\n", filename);
FILE *fp = fopen(filename, "wb");
if(!fp) file_error(filename);
int major = 0;
int minor = 1;
int revision = 0;
fwrite(&major, sizeof(int), 1, fp);
fwrite(&minor, sizeof(int), 1, fp);
fwrite(&revision, sizeof(int), 1, fp);
fwrite(net.seen, sizeof(int), 1, fp);
int i;
for(i = 0; i < net.n && i < cutoff; ++i){
layer l = net.layers[i];
if(l.type == CONVOLUTIONAL){
//save_convolutional_weights(l, fp);
int num = l.n*l.c*l.size*l.size;
fwrite(l.biases, sizeof(float), l.n, fp);
fwrite(l.weights_int8, sizeof(uint8_t), num, fp);
fwrite(&l.weights_quant_multipler, sizeof(float), 1, fp);
fwrite(&l.input_quant_multipler, sizeof(float), 1, fp);
}
}
fclose(fp);
}

I wrote an function which reads in an entire file into a string but it doesnt work

So, i need to read an entire file into a string in c, i dont know how big the file is gonna be. I wrote this function, but it doesnt work:
int slurp(char * filepath, char * outputfile) {
fp = fopen(filepath, "r");
int success = 0;
if (fp == NULL) {
success = 1;
}
if (success == 0) {
fseek(fp, 0, SEEK_END);
outputfile = (char *) calloc(ftell(fp) + 1, sizeof(char));
fread(outputfile, ftell(fp), sizeof(char), fp);
fseek(fp, 0, SEEK_SET);
outputfile[ftell(fp)] = '\0';
}
return success;
}
It doesnt get an error opening the file, but when i print out outputfile, i only get (null).
Why doesnt it work?
Thanks.
I tried your suggestions and it still doesnt work:
int slurp(char * filepath, char * outputfile) {
fp = fopen(filepath, "r");
int success = 0;
if (fp == NULL) {
success = 1;
}
if (success == 0) {
fseek(fp, 0, SEEK_END);
size_of_file = ftell(fp);
fseek(fp, 0, SEEK_SET);
outputfile = (char *) calloc(size_of_file + 1, sizeof(char));
fread(outputfile, size_of_file, sizeof(char), fp);
outputfile[size_of_file] = '\0';
}
return success;
}
Seek to the beginning before reading (reverse to this order):
fseek(fp, 0, SEEK_SET);
fread(outputfile, ftell(fp), sizeof(char), fp);

Read wrong values from binary file

I want to take an array of structs so I can sort it by name and write it on a txt file. But it takes wrong values like strange symbols or numbers. Anyone knows what is wrong?
typedef struct candidato Candidato;
struct candidato {
char inscr[10];
char nome[44];
int periodo;
int posicao;
char curso[30];
};
FILE *fp = fopen(filename, "rb");
if (fp == NULL)
{
fprintf(stderr, "Failed to open file %s for reading\n", filename);
return ;
}
fseek(fp, 0, SEEK_END);
size_t sz = ftell(fp);
int ncand = sz/sizeof(Candidato);
rewind(fp);
Candidato *arr = malloc(sz);
if (arr == 0)
{
fprintf(stderr, "Failed to allocate %zu bytes memory\n", sz);
return ;
}
printf("%d \n",ncand);
int i;
int cont;
for (i = 0; fread(&arr[i], sizeof(Candidato), 1, fp) == 1; i++){
printf("%s\n",arr[i].nome); //test if it got what I want
}
fclose(fp);
I solved my problem and here's the working code:
FILE *f = fopen (filename, "rb");
if(f==NULL){
printf("Erro na abertura do arquivo. \n");
system("pause");
return;
}
fseek(f, 0, SEEK_END);
int sz = ftell(f);
rewind(f);
Candidato arr[sz/sizeof(Candidato)];
int i;
for (i = 0; fread(&arr[i], sizeof(Candidato), 1, f) == 1; i++) {
printf("%s %i \n",arr[i].nome,arr[i].inscr);
}

Printing the code of the file

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
FILE *f;
f = fopen("my file.txt", "w");
int n = 5;
fprintf(f, "n equals to %d", n);
fclose(f);
f = fopen("my file2.txt", "w");
char *txt = "New file";
fwrite(txt, 1, strlen(txt), f);
fclose(f);
f = fopen("my file3.txt", "w");
char *txt2 = "Hello";
for(int i = 0; i < strlen(txt2); i++)
{
printf("Mouse cursor at %d.\n", ftell(f));
fputc(txt2[i], f);
}
fclose(f);
f = fopen(__FILE__, "r");
char str[1024];
while (!feof(f))
{
fscanf(f, "%s", str);
puts(str);
}
putchar('\n');
fclose(f);
f = fopen(__FILE__, "r");
long size = fseek(f, 0, SEEK_END);
char *buffer = malloc(sizeof(char) * size + 1);
fread(buffer, 1, size, f);
puts(buffer);
free(buffer);
fclose(f);
return 0;
}
Take a look in this part of the code :
f = fopen(__FILE__, "r");
long size = fseek(f, 0, SEEK_END);
char *buffer = malloc(sizeof(char) * size + 1);
fread(buffer, 1, size, f);
puts(buffer);
free(buffer);
fclose(f);
I've tried to print the code in my file and this is what I wrote to do it ^
When I try to print it with puts function it prints 3 characters :
http://i61.tinypic.com/ortdvl.png
The third character is changing in each execution.
Anyway to the question, I don't know why this happens can someone explain me what I did wrong ?
fseek(f, 0, SEEK_END);
long size = ftell(f);
char *buffer = calloc(size + 1, sizeof(char));
rewind(f);
fread(buffer, 1, size, f);

Changing one byte in a file in C

I have a file stream open and ready.
How do I access and change a single Byte in the stream such that the change is reflected on the file?
Any suggestions?
#include "stdio.h"
int main(void)
{
FILE* f = fopen("so-data.dat", "r+b"); // Error checking omitted
fseek(f, 5, SEEK_SET);
fwrite("x", 1, 1, f);
fclose(f);
}
FILE* fileHandle = fopen("filename", "r+b"); // r+ if you need char mode
fseek(fileHandle, position_of_byte, SEEK_SET);
fwrite("R" /* the value to replace with */, 1, 1, fileHandle);
#include <stdio.h> /* standard header, use the angle brackets */
int main(void)
{
char somechar = 'x'; /* one-byte data */
FILE* fp = fopen("so-data.txt", "r+");
if (fp) {
fseek(fp, 5, SEEK_SET);
fwrite(&somechar, 1, 1, fp);
fclose(fp);
}
return 0; /* if you are on non-C99 systems */
}

Resources