Hi im trying to read 6 bytes of data from a previously created file but wen i type in the file name it stops responding and crashes, its a binary file. i have included the code used to create the binary file(its commented out) and the code im using to read the binary file.Any tips???
Thank you
#include<stdio.h>
#include<string.h>
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
/* random record description - could be anything */
struct rec
{
int x,y,z;
};
//writes and then reades 6 arbitrary records from the file "sequence".
int main(void)
{
int i,TxData[6];
char word[100];
FILE *f;
struct rec r;
char fname[100];
//create the file of 6 bytes
// f=fopen("sequence","wb");
// for(i=0;i<6;i++){
// scanf("%s", &word);
// TxData[("%d",i)] = strtol(word,NULL,2);
//
// getchar();
// fwrite(&r,sizeof(struct rec),1,f);
// }
// fclose(f);
// printf("\n");
//read the 6 records back
printf("Enter file name\n");
scanf("%s",fname);
f=fopen("fname","r");
for(i=0;i<6;i++){
fread(&r,sizeof(struct rec),1,f);
printf("%d\n",TxData);
}
fclose(f);
printf("\n");
scanf("%d",&i);
return 0;
}
Thank you
#include<stdio.h>
#include<string.h>
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
/* random record description - could be anything */
struct rec
{
int x,y,z;
};
//writes and then reades 6 arbitrary records from the file "sequence".
int main(void)
{
int i,TxData[6];
char word[100];
FILE *f;
struct rec r;
char fname[100];
//create the file of 6 bytes
// f=fopen("sequence","wb");
// for(i=0;i<6;i++){
// scanf("%s", &word);
// TxData[("%d",i)] = strtol(word,NULL,2);
//
// getchar();
// fwrite(&r,sizeof(struct rec),1,f);
// }
// fclose(f);
// printf("\n");
//read the 6 records back
printf("Enter file name\n");
scanf("%s",fname);
f=fopen("fname","r");
for(i=0;i<6;i++){
fread(&r,sizeof(struct rec),1,f);
printf("%d\n",TxData);
}
fclose(f);
printf("\n");
scanf("%d",&i);
return 0;
}