The following code writes into the file but is unable to retrieve fields of the struct
conf_record in the printf (after fread within while loop) statement.Please give a solution. - /* record.h */
-
typedef struct conf_record {
-
char *source_IP;
-
char *dest_IP;
-
char *s_port;
-
char *d_port;
-
//char** range_allow;//[2];
-
char *range_disallow[2];
-
} Record;
-
-
/* main.c */
-
#include <stdio.h>
-
#include <errno.h>
-
#include "record.h"
-
int main()
-
{
-
FILE *f1, *f2;
-
Record record, record1;
-
int i;
-
void *buff;
-
-
f1 = fopen("./conf.txt", "a");
-
if (f1 != NULL) {
-
do {
-
printf("\nenter source_IP and dest_IP \n");
-
scanf("%s%s", &(record.source_IP), &(record.dest_IP));
-
printf("\nenter source_port and dest_port\n");
-
scanf("%s%s", &(record.s_port), &(record.d_port));
-
printf("\nenter start and end time of restricted period\n");
-
scanf("%s%s", &(record.range_disallow[0]),
-
&(record.range_disallow[1]));
-
-
-
buff = &record;
-
fwrite(buff, sizeof(Record), 1, f1);
-
fflush(f1);
-
printf("\n more records? say 1 or 0 for yes /no:");
-
scanf("%d", &i);
-
} while (i == 1);
-
fclose(f1);
-
}
-
-
f2 = fopen("conf.txt", "r");
-
if (f2 != NULL) { //reading not taking place!!!!!!
-
while (fread(buff, sizeof(Record), 1, f2) > 0) {
-
-
record1 = *((Record *) buff);
-
printf("%s %s %s %s", record1.source_IP, record1.dest_IP,
-
record1.s_port, record1.d_port);
-
}
-
fclose(f2);
-
}
-
return 0;
-
}
7 2946
The following code writes into the file but is unable to retrieve fields of the struct
conf_record in the printf (after fread within while loop) statement.Please give a solution.
/* record.h */
typedef struct conf_record{
char * source_IP;
char * dest_IP;
char* s_port;
char* d_port;
//char** range_allow;//[2];
char* range_disallow[2];
}Record;
/* main.c */
#include <stdio.h>
#include <errno.h>
#include "record.h"
int main()
{
FILE * f1,*f2;
Record record,record1;
int i;
void *buff;
f1=fopen("./conf.txt","a");
if(f1!=NULL)
{
do{
printf("\nenter source_IP and dest_IP \n");
scanf("%s%s",&(record.source_IP),&(record.dest_IP) );
printf("\nenter source_port and dest_port\n");
scanf("%s%s",&(record.s_port),&(record.d_port));
printf("\nenter start and end time of restricted period\n");
scanf("%s%s",&(record.range_disallow[0]),&(record.range_disallow[1]));
buff=&record;
fwrite(buff,sizeof(Record),1,f1);
fflush(f1);
printf("\n more records? say 1 or 0 for yes /no:");
scanf("%d",&i);
}while(i==1);
fclose(f1);
}
f2=fopen("conf.txt","r");
if(f2!=NULL)
{ //reading not taking place!!!!!!
while(fread(buff,sizeof(Record),1,f2)>0)
{
record1=*((Record*)buff);
printf("%s %s %s %s",record1.source_IP,record1.dest_IP,record1.s_po rt, record1.d_port);
}
fclose(f2);
}
return 0;
}
U can use scanf for that.
For more info check this article ;)
Savage
Thank you for help. I could read both using fread and fscanf in different copies of
the same proogram by allocating memory(malloc) char * fields of the struct or else by scanf/printf on each field separately .
Thank you for help. I could read both using fread and fscanf in different copies of
the same proogram by allocating memory(malloc) char * fields of the struct or else by scanf/printf on each field separately .
Is that a question?
Adrian
Is that a question?
Adrian
Though fread reads the written record, it only repeatedly shows the last record read , as many times as many records were written into the file.
I'm able to get all records using fscanf, but i also want to know how it could be done using fread and void pointers. So, Please tell.
Though fread reads the written record, it only repeatedly shows the last record read , as many times as many records were written into the file.
I'm able to get all records using fscanf, but i also want to know how it could be done using fread and void pointers. So, Please tell.
Well, you will need to read it to somewhere, so you will need an array which you may have to reallocate if you run out of space, an array of pointers which you must allocate each element for and reallocate if you run out of space or some other type of container (preferably one that will do the allocation for you ;)).
So, have you used malloc() and free() before? What about them do you not understand?
Adrian
Well, you will need to read it to somewhere, so you will need an array which you may have to reallocate if you run out of space, an array of pointers which you must allocate each element for and reallocate if you run out of space or some other type of container (preferably one that will do the allocation for you ;)).
So, have you used malloc() and free() before? What about them do you not understand?
Adrian
Yes I do understand the functions performed by malloc and free.Though I
use malloc(and calloc) often , I haven't worked much with free(may be because I wrote small programs ). For the allocations done in this program, i'm sending that section too. - #include "record.h"
-
int main()
-
{
-
FILE * f1,*f2;
-
Record record,record1;
-
int i;
-
void *buff;
-
-
record.source_IP=(char*)calloc(16,sizeof(char));
-
record.dest_IP=(char*)calloc(16,sizeof(char));
-
record.s_port=(char*)calloc(4,sizeof(char));
-
record.d_port=(char*)calloc(4,sizeof(char));
-
record.range_disallow[0]=(char*)calloc(10,sizeof(char));
-
record.range_disallow[1]=(char*)calloc(10,sizeof(char));
-
-
record1.source_IP=(char*)calloc(16,sizeof(char));
-
record1.dest_IP=(char*)calloc(16,sizeof(char));
-
record1.s_port=(char*)calloc(4,sizeof(char));
-
record1.d_port=(char*)calloc(4,sizeof(char));
-
record1.range_disallow[0]=(char*)calloc(10,sizeof(char));
-
record1.range_disallow[1]=(char*)calloc(10,sizeof(char));
-
-
f1=fopen("./conftemp.txt","wb");
-
if(f1!=NULL)
-
{
-
do{
-
printf("\nenter source_IP and dest_IP \n");
-
scanf("%s",(record.source_IP));
-
scanf("%s",(record.dest_IP));
-
printf("\nenter source_port and dest_port\n");
-
scanf("%s",(record.s_port));
-
scanf("%s",(record.d_port));
-
printf("\nenter start and end time of restricted period\n");
-
scanf("%s",(record.range_disallow[0]));
-
scanf("%s",(record.range_disallow[1]));
-
-
buff=&record;
-
fwrite(buff,sizeof(Record),1,f1);
-
fflush(f1);
-
-
printf("\n more records? say 1 or 0 for yes /no:");
-
scanf("%d",&i);
-
} while(i==1);
-
fclose(f1);
-
}
-
-
f2=fopen("./conftemp.txt","rb");
-
if(f2!=NULL)
-
{
-
while(fread(buff,sizeof(Record),1,f2)>0)
-
{
-
record1=*((Record*)buff); //reads but repeats the last
-
// record input to file instead of showing from the initial input???
-
-
printf("\n");
-
printf("%s ",(record1.source_IP));
-
printf("%s ",(record1.dest_IP));
-
printf("%s ",(record1.s_port));
-
printf("%s ",(record1.d_port));
-
printf("%s ",(record1.range_disallow[0]));
-
printf("%s ",(record1.range_disallow[1]));
-
fflush(stdout);
-
//buff=malloc(sizeof(Record));
-
}
-
fclose(f2);
-
}
-
else
-
printf("\ncould not open%s",errmsg);
-
return 0;
-
}
Don't write pointers to a file. It doesn't make sense. Tell me, what would happen if you read in a bunch of pointers using a different app? The pointers will not be valid in that app (if it is valid here it is barely valid).
Write the contents, not the pointer.
Anciant proverb: It is the fool who looks at the finger that is pointing at the moon. (or something like that ;)).
Adrian
Sign in to post your reply or Sign up for a free account.
Similar topics
by: KantKwitDansin1 |
last post by:
I have a file "a.dat" containing 10^4 32 bit binary numbers. I need to
read in these numbers and deterimine if they are prime. The primality
part I will have to figure out later, but I was...
|
by: Vasilis Serghi |
last post by:
Hi all, I am fairly new to C programming and I have come to a stand still. I
am trying to create a program that will accept a user input and give the
user an output depending on the contents of a...
|
by: Mark Richards |
last post by:
I've been programming for many years, but have only recently taken a
deep "C" dive (bad pun, i know) and need a lot of explanation from an
expert. My questions center around those mysterious...
|
by: G.Esmeijer |
last post by:
Friends,
I would like to read a text file (fixed length formaated) really fast and
store the data into an Access database (2003).
Using the streamreader and reading line by line, separating the...
|
by: a |
last post by:
I have a struct to write to a file
struct _structA{
long x;
int y;
float z;
}
struct _structA A;
//file open
write(fd,A,sizeof(_structA));
//file close
|
by: Tiger |
last post by:
I try to write a struct into a brut file but I can't write all var in
this struct
when I try to add user I have that :
> testmachine:/usr/share/my_passwd# ./my_passwd -a users.db
> Ajout d'une...
|
by: Adi |
last post by:
Hello eveyone,
I wanna ask a very simple question here (as it was quite disturbing
me for a long time.)
My problem is to read a file line by line. I've tried following
implementations but still...
|
by: liyankka |
last post by:
The following code writes into the file but is unable to retrieve fields of the struct
conf_record in the printf (after fread within while loop) statement.Please give a solution.
/* record.h */...
|
by: Hollywood |
last post by:
Hello members of the comp.lang.c++,
My log file is made of a set of 1000 following lines kind:
21/09/07 13:49:56,MW.SET.D_IGLS,2.000000
21/09/07 13:49:56,MW.SET.GNP_NT,7.000000
.....
...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |