473,386 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

how to read a fields of a struct from file

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;
}
May 25 '07 #1
1 1017
bartonc
6,596 Expert 4TB
Hi liyankka. I see that you have found the x Forum. There is also a C++/C Forum here on TheScripts.com. I'm hopeful that they will be able to help you with this question if you post again, there.

Thanks for joining.
May 26 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

7
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...
5
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...
19
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...
6
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...
8
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
2
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...
9
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...
7
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 */...
9
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 ..... ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
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...
0
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...
0
marktang
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.