473,406 Members | 2,259 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,406 software developers and data experts.

how to read 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.

Expand|Select|Wrap|Line Numbers
  1. /* record.h */
  2. typedef struct conf_record {
  3.     char *source_IP;
  4.     char *dest_IP;
  5.     char *s_port;
  6.     char *d_port;
  7. //char** range_allow;//[2];
  8.     char *range_disallow[2];
  9. } Record;
  10.  
  11. /* main.c */
  12. #include <stdio.h>
  13. #include <errno.h>
  14. #include "record.h"
  15. int main()
  16. {
  17.     FILE *f1, *f2;
  18.     Record record, record1;
  19.     int i;
  20.     void *buff;
  21.  
  22.     f1 = fopen("./conf.txt", "a");
  23.     if (f1 != NULL) {
  24.         do {
  25.             printf("\nenter source_IP and dest_IP \n");
  26.             scanf("%s%s", &(record.source_IP), &(record.dest_IP));
  27.             printf("\nenter source_port and dest_port\n");
  28.             scanf("%s%s", &(record.s_port), &(record.d_port));
  29.             printf("\nenter start and end time of restricted period\n");
  30.             scanf("%s%s", &(record.range_disallow[0]),
  31.                   &(record.range_disallow[1]));
  32.  
  33.  
  34.             buff = &record;
  35.             fwrite(buff, sizeof(Record), 1, f1);
  36.             fflush(f1);
  37.             printf("\n more records? say 1 or 0 for yes /no:");
  38.             scanf("%d", &i);
  39.         } while (i == 1);
  40.         fclose(f1);
  41.     }
  42.  
  43.     f2 = fopen("conf.txt", "r");
  44.     if (f2 != NULL) {           //reading not taking place!!!!!!
  45.         while (fread(buff, sizeof(Record), 1, f2) > 0) {
  46.  
  47.             record1 = *((Record *) buff);
  48.             printf("%s %s %s %s", record1.source_IP, record1.dest_IP,
  49.                    record1.s_port, record1.d_port);
  50.         }
  51.         fclose(f2);
  52.     }
  53.     return 0;
  54. }
May 27 '07 #1
7 2946
Savage
1,764 Expert 1GB
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
May 27 '07 #2
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 .
May 29 '07 #3
AdrianH
1,251 Expert 1GB
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
May 29 '07 #4
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.
May 29 '07 #5
AdrianH
1,251 Expert 1GB
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
May 29 '07 #6
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.


Expand|Select|Wrap|Line Numbers
  1. #include "record.h"
  2. int main()
  3.     {
  4.       FILE * f1,*f2;
  5.       Record record,record1;
  6.        int i;
  7.       void *buff;
  8.  
  9.         record.source_IP=(char*)calloc(16,sizeof(char));
  10.         record.dest_IP=(char*)calloc(16,sizeof(char));
  11.         record.s_port=(char*)calloc(4,sizeof(char));
  12.         record.d_port=(char*)calloc(4,sizeof(char));
  13.         record.range_disallow[0]=(char*)calloc(10,sizeof(char));
  14.         record.range_disallow[1]=(char*)calloc(10,sizeof(char));
  15.  
  16.         record1.source_IP=(char*)calloc(16,sizeof(char));
  17.         record1.dest_IP=(char*)calloc(16,sizeof(char));
  18.         record1.s_port=(char*)calloc(4,sizeof(char));
  19.         record1.d_port=(char*)calloc(4,sizeof(char));
  20.         record1.range_disallow[0]=(char*)calloc(10,sizeof(char));
  21.         record1.range_disallow[1]=(char*)calloc(10,sizeof(char));
  22.  
  23.              f1=fopen("./conftemp.txt","wb");
  24.                if(f1!=NULL)
  25.                {
  26.                         do{
  27.                         printf("\nenter source_IP and dest_IP  \n");
  28.                                 scanf("%s",(record.source_IP));
  29.                             scanf("%s",(record.dest_IP));
  30.                             printf("\nenter source_port and dest_port\n");    
  31.                                 scanf("%s",(record.s_port));
  32.                             scanf("%s",(record.d_port));
  33.                                 printf("\nenter start and end time of restricted period\n");
  34.                                 scanf("%s",(record.range_disallow[0]));
  35.                                 scanf("%s",(record.range_disallow[1]));
  36.  
  37.                                buff=&record;
  38.                                fwrite(buff,sizeof(Record),1,f1);
  39.                                fflush(f1);
  40.  
  41.                                printf("\n more records? say 1 or 0 for yes /no:");
  42.                               scanf("%d",&i);
  43.                           }  while(i==1);
  44.                        fclose(f1);  
  45.                }
  46.  
  47.               f2=fopen("./conftemp.txt","rb"); 
  48.               if(f2!=NULL)
  49.                    {
  50.                          while(fread(buff,sizeof(Record),1,f2)>0)    
  51.                                {
  52.                                  record1=*((Record*)buff);    //reads but repeats the last   
  53.                      //  record input to file instead of showing from the initial input???
  54.  
  55.                                 printf("\n");
  56.                                 printf("%s ",(record1.source_IP));
  57.                                 printf("%s ",(record1.dest_IP));
  58.                                 printf("%s ",(record1.s_port));
  59.                                 printf("%s ",(record1.d_port));
  60.                                 printf("%s ",(record1.range_disallow[0]));
  61.                                 printf("%s ",(record1.range_disallow[1]));
  62.                                 fflush(stdout);
  63.                                 //buff=malloc(sizeof(Record));
  64.                          }
  65.                     fclose(f2);
  66.                   }
  67.            else      
  68.                        printf("\ncould not open%s",errmsg);
  69.           return 0;
  70.          }
May 29 '07 #7
AdrianH
1,251 Expert 1GB
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
May 29 '07 #8

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...
1
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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...
0
tracyyun
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...

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.