Hello
I'm still kinda new at C and I have this buffer filled with data that I want to to 'parse' into a an array of structs. How do I do this?
The only thing I can think of is for-loops, kinda 'bytewise', but there must be a better way?
It's something like this: - char buf[BUFSIZE];
-
-
struct table_entry{
-
int length;
-
int offset;
-
char *name;
-
}
-
-
struct table_entry *tablearray = NULL;
-
I allocate memory for the tablearray, data is read into the buf and then, how do I 'fill' the structs? The data in the buffer is of course 'structdata', I mean of the kind 'length,offset,name,length,offset,name...."
Hope someone knows what I mean and can help me, that would be so very kind of you!
Cheers!
9 10325
You can use memcpy function in your code. -
Code: ( text )
-
char buf[BUFSIZE];
-
struct table_entry{
-
int length;
-
int offset;
-
char *name;
-
}
-
struct table_entry *tablearray = NULL;
-
-
I am assuming that the buffer has valid data that is aligned properly with the struct table_entry. As you mentioned you will need a for loop to copy the number the entries which you should maintain a count of. I am supposing the number of entries to be variable n. -
int i = 0;
-
/* Allocate the memory for tablearray */
-
tablearray = malloc(...);
-
for(i=0;i < n;i++) {
-
memcpy(tablearray, buf, sizeof(table_entry));
-
}
-
I hope this helps.
Ambrish Kinariwala
Hello
I'm still kinda new at C and I have this buffer filled with data that I want to to 'parse' into a an array of structs. How do I do this?
The only thing I can think of is for-loops, kinda 'bytewise', but there must be a better way?
It's something like this: - char buf[BUFSIZE];
-
-
struct table_entry{
-
int length;
-
int offset;
-
char *name;
-
}
-
-
struct table_entry *tablearray = NULL;
-
I allocate memory for the tablearray, data is read into the buf and then, how do I 'fill' the structs? The data in the buffer is of course 'structdata', I mean of the kind 'length,offset,name,length,offset,name...."
Hope someone knows what I mean and can help me, that would be so very kind of you!
Cheers!
I want to help but I don't want to write a bunch of stuff that you might already know. Help me understand how far along in the process you are.
Do you have code to open the input file and read it into your parse buffer?
Have you figured out the high level algorithm for parsing a struct worth of data?
Do you have any parse code?
--
Ambrish's code is likely not to work because sizeof( struct table_entry) only gives you enough memory to hold 'name' address, not space to actually hold the name (which may be 1024 bytes for all anybody knows - you have to read 'length' to know that).
Do you have code to open the input file and read it into your parse buffer?
Have you figured out the high level algorithm for parsing a struct worth of data?
Do you have any parse code?
Thank you both for taking the time trying to help me!
I have the code to open the input file and read it into my parse buffer yes, so I guess my problem is figuring out the high level parsing a struct worth of data algorithm!
No, I don't have any parse code yet (if I understand that last question correctly), that's kinda the problem..
Thanks again!
Forget the array if structs. You have not mentioned your file format. It's not so much that you parse into an array of structs but more imprtant that you know the data you are reading.
If you can read the data an populate one struct variable, then an array is simple.
So, what does your data look like?
Forget the array if structs. You have not mentioned your file format. It's not so much that you parse into an array of structs but more imprtant that you know the data you are reading.
If you can read the data an populate one struct variable, then an array is simple.
So, what does your data look like?
That's the thing, that's my problem: populating that one struct variable!
Maybe it's just that my problem is too simple, that's why you all have a hard time understanding what I mean? (And of course, I'm not too great at describing my problem clearly either I guess..)
Let's put it this way: If I have a buffer filled with bytes, and I want to populate a struct out of it, how do I do that?
It's something like this - struct table_entry{
-
int length;
-
int offset;
-
char *name;
-
}
-
char buf[BUFSIZE];
-
-
struct table_entry entry;
-
-
entry.length = //The first 4 bytes of buf
-
entry.offset = //The next 4 bytes of buf
-
entry.name = //The next 16 bytes of buf
-
My problem is the comments, what should the code look like there?
Hope I'm being a little bit clearer, and sorry for being such a newbie..
- struct table_entry{
-
int length;
-
int offset;
-
char *name;
-
}
-
char buf[BUFSIZE];
-
-
struct table_entry entry;
-
-
entry.length = //The first 4 bytes of buf
-
entry.offset = //The next 4 bytes of buf
-
entry.name = //The next 16 bytes of buf
-
My problem is the comments, what should the code look like there?
Maybe you can do it like this? - memcpy(entry.length,buf,4);
-
memcpy(entry.offset,buf+4,4);
-
memcpy(entry.name,buf+8,16);
Cheers!
Maybe you can do it like this? - memcpy(entry.length,buf,4);
-
memcpy(entry.offset,buf+4,4);
-
memcpy(entry.name,buf+8,16);
Cheers!
Hi Mimimi while trying out your query, i coded in this way but its not working out. Can you please share your code snippet. Anybody can help out me regarding this. -
#include <stdio.h>
-
-
#define BUFSIZE 100
-
-
char buf[BUFSIZE][BUFSIZE]={ 1,3,"Trying",2,4,"out",4,5,"something"};
-
-
struct table_entry{
-
int length;
-
int offset;
-
char *name;
-
}
-
-
struct table_entry *tablearray = NULL;
-
memset(tablearray,0,BUFSIZE);
-
tablearray = (table_entry*)malloc(sizeof(BUFSIZE));
-
-
int main()
-
{
-
int i;
-
for(i=0;i<BUFSIZE;i++)
-
{
-
memcpy(tablearray.length,buf,4);
-
memcpy(tablearray.offset,buf+4,4);
-
memcpy(tablearray.offset,buf+16,16);
-
}
-
return 0;
-
}
-
Thanks in advance,
mmk
Thank you all for taking the time to try and help me with this!
The problem is now solved!
memcpy was no good idea!
And also, the struct member 'name' worked much better looking like this - struct table_entry {
-
int length;
-
int offset;
-
char name[NAMESIZE];
-
};
instead of 'char *name'. And I didn't need an 'intermediate' buffer but could get the struct-data 'straight away'. Kinda like this (pseudocode): -
struct table_entry *tablearray;
-
int TABLESIZE = 64;
-
int i,offset = 0;
-
struct table_entry entry;
-
tablearray = mem_alloc(sizeof (struct table_entry) * TABLESIZE);
-
read_from_file(file,(unsigned char*)tablearray,offset,TABLESIZE);
Then I could access the data like this - for(i=0; i<TABLESIZE;i++)
-
{
-
entry.len = tablearray[i].len;
-
entry.offset = tablearray[i].offset;
-
strncpy(entry.name,tablearray[i].name,NAMESIZE);
-
}
Again, thank you all, and take care!
Cheers!
And also, the struct member 'name' worked much better looking like this
Code: ( text )
struct table_entry {
int length;
int offset;
char name[NAMESIZE];
};
instead of 'char *name'. And I didn't need an 'intermediate' buffer but could get the struct-data 'straight away'.
Yes, but you also built a limit into your code: NAMESIZE. That means if NAMESIZE is 80, a name like MimiMi will take 80 bytes instead of 6. Now your program is larger and slower.
Stick with the pointer.
If you have a buffer with a C-string in it, all you need to do si allocate memory and then copy the string to your struct variable: -
struct data
-
{
-
char* name;
-
};
-
-
char buffer[SOMESIZE];
-
struct data var;
-
var.data = (char*)malloc((strlen(buffer) + 1) * sizeof(char));
-
strcpy(var.data, buffer);
-
var now has a copy of the data.
I aksed about your file format since some of your struct members were ints and you cannot have ints in a text file. Text file have only chars. However, I never saw yout file layout or your read code so I really can't see what you are doing.
char buf[BUFSIZE][BUFSIZE]={ 1,3,"Trying",2,4,"out",4,5,"something"};
An array has to have elements that are the same type. This one has int and char*. I expect the compiler will gag on it.
Again, this comes down to data format. You cannot read data unles you know the format.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Johnny Google |
last post by:
Here is an example of the type of data from a file I will have:
Apple,4322,3435,4653,6543,4652
Banana,6934,5423,6753,6531
Carrot,3454,4534,3434,1111,9120,5453
Cheese,4411,5522,6622,6641
The...
|
by: SD |
last post by:
I am thinking about writing a text editor in C for unix sometime soon.
I am just doing this to learn more about C. I want to write something
like ed.c, a simple line editor. What types of data...
|
by: Eric |
last post by:
Hi,
I need to send data from a file into a structure. In Vb6 this was done like
so.
public Type Struct1
v1 as string
v2 as string
end type
|
by: Zeke Zinzul |
last post by:
Hi Guys & Geeks,
What's the most elegant way of dealing with binary data and structures?
Say I have this (which I actually do, a woo-hoo):
struct Struct_IconHeader
{
byte width;
byte...
|
by: aarklon |
last post by:
Hi all,
this is a question which i saw in a book
typedef struct mall_li_header_ {
int refcnt;
uchar pool;
uchar flag;
ushort magic_no;
char data;
|
by: AdrianH |
last post by:
Assumptions
I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C++ programming.
FYI
Although I have called...
|
by: AdrianH |
last post by:
Assumptions
I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C programming.
FYI
Although I have called this...
|
by: JRough |
last post by:
I'm trying to use output buffering to cheat so i can print to excel
which is called later than this header().
header("Content-type: application/xmsdownload");
header("Content-Disposition:...
|
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: 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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: 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: 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...
|
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: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |