Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

structures and files.

Status
Not open for further replies.

Denster

Programmer
Apr 9, 2001
198
GB
need some help on the subject of file handling. I have a text file with some data and need to read it into a structure. The data reads as follows :-

C11401 FOYS HARDWARE 208 BRADKIRK PLACE, WALTON SUMMIT, PRESTON, PN2 3XL 0 1094866278

This is the program so far wich just doesnt work :-

#include <stdio.h>
struct rec_create{
char type;
char cust_code[6];
char cust_name[21];
char cust_address[61];
float cust_balance;
int credit_limit;
};
void main()
{
FILE *source;
struct rec_create type3;
source = fopen(&quot;DF73VF.DAT&quot;,&quot;rt&quot;);
fread(&type3,sizeof(struct rec_create),1,source);
printf(&quot;%c\n%s\n%s\n%s\n%f\n%d&quot;,
type3.type,type3.cust_code,
type3.cust_name,type3.cust_address,
type3.cust_balance,type3.credit_limit);
}

I'm sure there is an easy solution to this, but cant get my head round it. Thanx in advance for any guidance.



 
Your strings are not terminated by '\0'

Notice if you add the following lines after reading the file:

type3.cust_code[5] = '\0';
type3.cust_name[20] = '\0';
type3.cust_address[60] = '\0';

the program runs fine.
 
Well thats a lot better but it still is'nt reading the last numbers correctly. numbers produced are
0.0001680297 909522996
perhaps I havent got the spacing right in the text file.

:-I
 
Well, the last number is probably too large (10 digits) to fit into integer storage, and I suspect this is part of the problem.

Are you sure you want that data type to be an int?

Better a char[] or if it is monetary use float or double.
 
Actually, your problem IS with the spacing. Your record length is 101 bytes. Records should be fixed length for fread. You really shouldn't use integer or float types in your structures, make them all character of appropriate length and then convert back using atoi and atof, but remember that last number might not fit inside an int.

The code below runs fine:

#include <stdio.h>
struct rec_create{
char type;
char cust_code[6];
char cust_name[21];
char cust_address[60];
char cust_balance[2];
char credit_limit[10];
};

void main()
{
FILE *source;
struct rec_create type3;
source = fopen(&quot;DF73VF.DAT&quot;,&quot;rt&quot;);

if(!source)
{
printf(&quot;no data file found\n&quot;);
exit(0);
}

fread(&type3,sizeof(struct rec_create), 1, source);


printf(&quot;%c\n%s\n%s\n%s\n%s\n%s\n&quot;,
type3.type,
type3.cust_code,
type3.cust_name,
type3.cust_address,
type3.cust_balance,
type3.credit_limit);
}

 
You are mixing ASCII and binary file I/0. Since you are trying to read an ASCII file, you should not use fread() but use fscanf() or similar ASCII I/O facility. The difference lies in the fact that ASCII files are often organized around delimiters and tokens which do not lend themselves to the the strict byte-offset organization which is expected by fread.

Brudnakm
 
You should use fread to read data from a file only when the data within the file are written using fwrite. If you are not doing so, problems will come while reading non-character data from the file.

The binary eqivalnet of the data were written to file or read form the file ( so for int 4 or 2 bytes will be written or read).

So write the data using fwrite, Open file in binary mode for both read and write operation.

Maniraja S
 
thanx to everyone who has replied to this thread. The advice has been taken and eventualy decided to read the whole thing in as a string using the fgets function. Once the line has been read I can then break it down and put it in the relevant data structure.




s-)

much happier
Denster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top