473,388 Members | 1,526 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,388 software developers and data experts.

Copy a database from a file to a structure

Platform: ANSI C, RTOS
Hi,
I am trying to fread a database from a text file, and assign the database directly to the public structure below. In the text file first 20 chars is for SKU next 35 chars for product and last 5 chars for quantity.


// Structure for Database
typedef struct _database
{
char SKU[20];
char product[35];
char quantity[5];
}
db;


My code to assign address of the first char of database to the begining address of structure.
...
db *database;
database = (char *)malloc(sizeof(db)); //or malloc(sizeof(char)*60);
handle = x_fopen("/flash/database.txt","r");
x_fread(handle,database,sizeof(db));
printf("SKU : %s\n", database->SKU);
printf("PRODUCT : %s\n", database->product);
printf("QUANTITY : %s\n", database->quantity);


What I get for database->SKU is the whole line with 60 chars strlen(?).
database->product starts from the right char (26) but goes to the end.
database->quantity start from right address as others goes till the end.

I need to split the first 25 char and next 35 chars and last 5 chars. Can I do it without 3 fread command (fread(first 25) fread(next 35) fread(last5))?
Thank you.
Apr 24 '07 #1
1 1944
All right I got it. It was because printf("%s") is posting till the null char. Structures are correctly saved with the code below. Have to use STRCMP to validate the SKU, product or quantity. Thanks.

Platform: ANSI C, RTOS
Hi,
I am trying to fread a database from a text file, and assign the database directly to the public structure below. In the text file first 20 chars is for SKU next 35 chars for product and last 5 chars for quantity.


// Structure for Database
typedef struct _database
{
char SKU[20];
char product[35];
char quantity[5];
}
db;


My code to assign address of the first char of database to the begining address of structure.
...
db *database;
database = (char *)malloc(sizeof(db)); //or malloc(sizeof(char)*60);
handle = x_fopen("/flash/database.txt","r");
x_fread(handle,database,sizeof(db));
printf("SKU : %s\n", database->SKU);
printf("PRODUCT : %s\n", database->product);
printf("QUANTITY : %s\n", database->quantity);


What I get for database->SKU is the whole line with 60 chars strlen(?).
database->product starts from the right char (26) but goes to the end.
database->quantity start from right address as others goes till the end.

I need to split the first 25 char and next 35 chars and last 5 chars. Can I do it without 3 fread command (fread(first 25) fread(next 35) fread(last5))?
Thank you.
Apr 24 '07 #2

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

Similar topics

2
by: Anna | last post by:
Hi all. I am trying to write a stylesheet that will structure the input HTML file in the following way: For each heading of level n it needs to enclose the heading and all its content until the...
3
by: Sandli Bygg AS, Kent Olsson | last post by:
Hi! How can I copy a Access-database and relations, but without data? Best regards Kent Olsson
5
by: Deano | last post by:
I'm experiencing bloat problems when I restore my backup files. Users can take a look at the reports of any previous backup without having to ensure they have a backup of their current data - this...
1
by: deko | last post by:
DoCmd.CopyObject copies data, but I only need structure. I'm trying to clone several tables in my Access 2003 mdb. The goal is to link to a series of Excel spreadsheets and then run various...
5
by: DraguVaso | last post by:
Hi, I'm looking for a way to Copy and Paste Files to the clipboard. I found a lot of articles to copy pieces of text and bitmaps etc, but nog whole files. Whay I need is like you have in...
2
by: Robert Fitzpatrick | last post by:
Does COPY require values for each column even though the database field is not set to NOT NULL? I did a COPY TO first to line up the data and then COPY FROM a tab delimited file. Here are the first...
5
by: kebuchan | last post by:
Hi all, I know this is possible in Oracle but has anyone done it in DB2 and could you maybe direct me to some documentation or let me know how I would go about doing it? In the oracle world...
12
by: alanchinese | last post by:
i am a db2 newbie. we have a server hosting a db2/6000 database that restricts the use of backup, generate ddl. i wonder if there is a simple way to transfer the database structure and data into my...
12
by: Robbo | last post by:
Hi, My database contains 45 tables the structure of which is evolving as I develop. I need a utility that will "read" the structure of this database and it's tables and then create an identical...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.