I have no idea how to do this. Here is the simplified version of my code. I am supposed to read off a binary file to a 2D array.
void readf(const char *fname, int a[50][50]){
int i=0;
FILE *f;
f = fopen(fname,"rb");
fread(a[i], sizeof(int), 1, f);
}
I tried using for loops and &a and a[i][j] and I ended up with this.
The file I am trying to read is .bin file and it only contains 0s and 1s
Related
I need to write function which will write elements of array into binary file, and after that find the mean value of elements of binary file.
#include <stdio.h>
double enterArray(){
int i=0,n;
double a[101];
FILE* fp=fopen("niz.bin", "w");
while(1){
scanf("%lf", &a[i]);
if(a[i]==-1)break;
i++;
if(i==100)break;
}
n=i;
for (i=0; i<n; i++)
fwrite(a,4, n, fp);
fclose(fp);
return 0;
}
double meanValue(){
return 1;
}
int main() {
enterArray();
printf("%g\n", meanValue());
return 0;
}
I get some random characters in niz.bin file. Could you help me fix my code?
There are a few problems with the code.
You take an arbitrary number of doubles, and then write the first n * 4 bytes in the array "repeatedly" to the file.
You don't know for sure that sizeof (double) is equal to 4 bytes.
On every iteration, you write (n * 4) bytes from the array a to the file.
What you want is, starting from i * sizeof (double), write sizeof (double) bytes to the file (if you want to use a loop. You could simply just write (n * sizeof (double) bytes to the file at once)
You use the "w" mode in fopen, which is not the "write binary" mode. Use "wb" instead.
Then, in meanValue(), you don't do anything. Writing/reading a 10-element double array to/from a binary file can be done in the following way:
/* write to the file */
double write_arr[10] = { 0.0, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9 };
FILE *fp = fopen("niz.bin", "wb");
fwrite(&write_arr, sizeof(double), 10, fp);
fclose(fp);
/* read from the file */
fp = fopen("niz.bin", "rb");
double read_arr[10];
fread(&read_arr, sizeof (double), 10, fp);
Also, note that you must identify how many elements you're going to write to the file, as you write the bytes in the file without knowing how many to write in advance. As you don't write the elements immediately, you could serialize the number of elements in the first 4 bytes in the file. And when you read from it, read the first 4 bytes to know how many doubles lying ahead.
I have a file in csv format look like this:
0.0060862,0.31869
0.025889,0.21183
0.064364,0.094135
0.10712,-0.0081176
0.15062,-0.073904
I would like to load the first column to array a and second column to array b. This is what the code looks like:
double a[5];
double b[5];
int i=0;
FILE* fileHandle = NULL;
fopen_s(&fileHandle, fileName.csv, "r+");
for(i=0;i<5;i++)
{
fscanf_s(fileHandle,"%lf,%lf",a[i],b[i]);
}
fclose(fileHandle);
Now I am converting the csv file to a binary file; the data is represented in 2's complement in unsigned int. How should I change my code?
I changed the code to
unsigned x[5];
unsigned y[5];
double a[5];
double b[5];
int i=0;
FILE* fileHandle = NULL;
fopen_s(&fileHandle, fileName.csv, "rb+");
for(i=0;i<5;i++)
{
fscanf_s(fileHandle,"%u,%u",x[i],y[i]);
a[i] = x[i]/(2^15);
b[i] = y[i]/(2^15);
}
fclose(fileHandle);
But x[i] and y[i] read from the binary is always 3435973836. How should I change my code to make it work?
When your data is binary, you don't need to convert it with fprintf and fscanf.
You can just read and write you array with fread and fwrite.
If your data alternates a and b records, you will better organize your variables the same way :
struct ab {
int a, b;
} ab[5];
and read it all in one shot with
fread(ab, sizeof (int), 10, fileHandle);
Then process it the way you like.
(see man fread, and man fwrite for details)
Can some one explain me how to read content from a binary file?
I have done this:
FILE *fp;
int a[50];
fp=fopen("x.exe","rb");
while(fread(&a,sizeof(a),1,fp)==1)
{
printf("%d",a);
}
suppose that file only contain integer values.
Also I tired writing into a file using structures, and I can easily read&write contents of it, but when I try reading some other binary file I just got garbage value.
Or is it impossible to read a .exe file compliled and linked?
the correct way is:
int myreadfile(void)
{
FILE *fp;
int i, n;
unsigned char a[50];
if (!(fp=fopen("x.exe","rb"))) return(0);
while(n=fread(a,1,sizeof(a), fp))
{
for (i=0; i<n; i++)
printf("%02x ",a[i]);
printf("\n");
}
fclose(fp);
return 1;
}
Note that the buffer is of type unsigned char. That is because
a) you don't know if the file is a complete number of ints (but it is of char, i.e. bytes) and
b) in the printf call, the char will be converted to intand would the high bit of the char be a 1, it would be sign-extended, which we don't want.
Also, fread does not try to read a whole buffer but just any number of bytes still in the file (to a maximum of the buffer).
this may seem basic to some of you, but please try to help me!
I have created a BMP file that stores a 4 x 3 image. The content of this file (FILEHEADER, FILEINFOHEADER, color bytes, and padding, are represented in hex).
I'm trying to write a loadFile function to load the BMP file and store it into a 2D array of integers.
I'm first trying to figure out how to store the content of the file into a character array, then i would try and do a strcpy of the char array and convert that into int values (i'm not converting yet).
But for now i've attempted to load the file and put into a char array... (it doesn't work, but i was hoping to get some feedback on whether or not i'm in the right direction), and how would i go about approaching this problem!
#include <stdio.h>
#include <stdlib.h>
#define length 30
#define rows 7
int main()
{
FILE *myBmp;
myBmp = fopen("myBmp.bmp", "r"); //for now i'm not sure what the r is for
//read file into array
char hexArray[rows][length];
int c;
for (c = 0; c < rows; c++) {
fscanf(myBmp, "%c", &hexArray[c]);
}
for (c = 0; c < rows; c++) {
printf("Number is: %c\n", hexArray[c]);
}
fclose(myBmp);
}
Im having some trouble figuring out how to properly format fread statements. The below code is just some randomn stuff Im practicing with. Basically it fills information into the first array (s), writes 's' to a file, and then reads the file into the second array (s2). However I can't seem to get the fread statement formated in a way that doesnt give an error or return garbage. The arrays are in char datatype because, if my understanding is correct, char uses less memory than other datatypes. The eventual application of this practice code is for a data compression project.
#include<stdio.h>
#include<string.h>
FILE *fp;
//file pointer
char s[56];
//first string
char s2[56];
//target string for the fread
int n=0;
//counting variable
int m=0;
int main (void)
{
fp=fopen("test.bin", "w+");
//open a file for reading and writing
strcpy(s, "101010001101010");
//input for the string
for(n=0;n<56;n++)
{
if(s[n]==1)
m=n;
else if(s[n]==0)
m=n;
}
printf("%d\n", m);
//the above for loop finds how many elements in 's' are filled with 1's and 0's
for(n=0;n<m;n++)
{
printf("%c", s[n]);
}
//for loop to print 's'
fwrite(s, m, 1, fp);
//writes 's' to the first file
s2=fread(&s2, m, 1, fp);
//an attempt to use fread...
printf("\n\ns2\n\n");
for(n=0;n<m;n++)
{
printf("%c", s2[n]);
}
printf("\n");
//for loop to print 's2'
fclose(fp);
printf("\n\n");
printf("press any number to close program\n");
scanf("%d", &m);
}
A FILE structure has an implicit seek position within the file. You read and write from that seek position. If you want to read what you have written, you need to change the seek position back to the beginning of the file with a call to fseek(). In fact, for a file open for reading and writing, you must call fseek() when switching between reading and writing.
The return value of the fread function is of type size_t. It is the number of elements successfully read. (reference: http://www.cplusplus.com/reference/cstdio/fread/)
Don't assign it to s2. Simply use fread(&s2, m, 1, fp);