Hi,
I have the following structure
struct Format
{
char x[12];
unsigned char a;
unsigned char b;
unsigned char c;
char y[30];
} __attribute__(( __aligned__));
I have a csv file which I am reading the data from which has fields
which match the structure and I tokenize the data read from the file
using strtok. the problem is when I do a strcpy to the the string x
and print the value from the structure for x,
I get the whole string and an extra char at the end of the string. it
is a special char and I am not able to get rid of that special char.
It only happens when I have a token that is exactly 12 chars wide. if
the token being copied into x is smaller than 12 it is fine. But if I
try to copy a token which is 12 bytes long I get an extra char at the
end in x.
I tried all sorts of stuff even memcpy etc and still I end up with
that extra char in x. If I just print the token there is no extra char
but when I copy it into that member of structure and print it I get a
extra token.
sample out put :
Original token
Awsche-tonol
1
9
47
Fiery Legion
Error output from displaying members of the Struct:
Awsche-tonol,1,9,47,F iery Legion
Any help regarding this please.
Regards. 4 2370
I have the following structure
>
struct Format
{
char x[12];
unsigned char a;
unsigned char b;
unsigned char c;
char y[30];
} __attribute__(( __aligned__));
I have a csv file which I am reading the data from which has fields which match the structure and I tokenize the data read from the file using strtok. the problem is when I do a strcpy to the the string x and print the value from the structure for x,
Show me the code. Particularly, show me where you modify format.a
and how that relates to the point where you put something in x.
>I get the whole string and an extra char at the end of the string. it is a special char and I am not able to get rid of that special char. It only happens when I have a token that is exactly 12 chars wide. if
If you put 12 characters (not including the \0 terminator) into x,
then it is NOT A String and you should not use the %s format to
attempt to print NOT A Strings. If you used strcpy() (as opposed
to, say, strncpy()) to do it, you also committed a buffer overflow
and invoked the wrath of undefined behaviour by doing so.
After you did that copy, did you perhaps assign something to format.a,
clobbering the string terminator character ?
>the token being copied into x is smaller than 12 it is fine. But if I try to copy a token which is 12 bytes long I get an extra char at the end in x.
On Apr 19, 12:53 pm, gordonb.2y...@b urditt.org (Gordon Burditt) wrote:
I have the following structure
struct Format
{
char x[12];
unsigned char a;
unsigned char b;
unsigned char c;
char y[30];
} __attribute__(( __aligned__));
I have a csv file which I am reading the data from which has fields
which match the structure and I tokenize the data read from the file
using strtok. the problem is when I do a strcpy to the the string x
and print the value from the structure for x,
Show me the code. Particularly, show me where you modify format.a
and how that relates to the point where you put something in x.
I get the whole string and an extra char at the end of the string. it
is a special char and I am not able to get rid of that special char.
It only happens when I have a token that is exactly 12 chars wide. if
If you put 12 characters (not including the \0 terminator) into x,
then it is NOT A String and you should not use the %s format to
attempt to print NOT A Strings. If you used strcpy() (as opposed
to, say, strncpy()) to do it, you also committed a buffer overflow
and invoked the wrath of undefined behaviour by doing so.
After you did that copy, did you perhaps assign something to format.a,
clobbering the string terminator character ?
the token being copied into x is smaller than 12 it is fine. But if I
try to copy a token which is 12 bytes long I get an extra char at the
end in x.
the thing is I have to get the structure size to be 45 bytes and so I
cannot fit in \0 into the string coz then I will have to make the size
of the x as 13 as opposed to 12.
Code to tokenize
split = strtok(buffer," ,");
no_tok =0;
j=0;
while(split != NULL)
{
strcpy(temp[j],split);
strcat(temp[j],"\0");
//printf("%s",tem p[j]);
j++;
split = strtok(NULL,"," );
++no_tok;
}
Code to store into the Structure
if(no_tok != 0)
{
rec++;
strcpy(F.x,temp[0]);
F.a = atoi(temp[1]);
F.b = atoi(temp[2]);
F.c = atoi(temp[3]);
if(no_tok == 5)
strcpy(F.y,temp[4]);
else
if(no_tok == 4)
{
strcpy(F.Guild, "\0");
}
}
and another problem is even if I am using #pragma(1) I am not able to
get the size of the struct to 45 and always ends up as 48.
....vagrahb wrote:
Hi,
I have the following structure
struct Format
{
char x[12];
unsigned char a;
unsigned char b;
unsigned char c;
char y[30];
} __attribute__(( __aligned__));
FYI, the __attribute__(( __aligned__)) stuff is not C, but
C-with-lipstick.
[...] if
the token being copied into x is smaller than 12 it is fine. But if I
try to copy a token which is 12 bytes long I get an extra char at the
end in x.
You have forgotten the '\0' character that marks the end of
a string in C. The x[] array has room for twelve characters, so
it can hold *eleven* characters of string "payload" in addition
to the '\0' that marks the end. When you try to stuff thirteen
characters -- twelve payload plus the '\0' marker -- into an
array that only has room for twelve of them, your program starts
behaving like Jack Nicholson in "The Shining."
--
Eric Sosman es*****@ieee-dot-org.invalid
struct Format
{
char x[12];
unsigned char a;
unsigned char b;
unsigned char c;
char y[30];
} __attribute__(( __aligned__));
Most of the stuff on the above line isn't actual C, and doesn't it
*DEMAND* padding?
>If you put 12 characters (not including the \0 terminator) into x, then it is NOT A String and you should not use the %s format to attempt to print NOT A Strings. If you used strcpy() (as opposed to, say, strncpy()) to do it, you also committed a buffer overflow and invoked the wrath of undefined behaviour by doing so.
>the thing is I have to get the structure size to be 45 bytes and so I cannot fit in \0 into the string coz then I will have to make the size of the x as 13 as opposed to 12.
If you leave it as size 12 then when you copy a 12-character something
into it, you'd darn well better NOT use strcpy(), and you'd darn
well better NOT use %s to print it. It is acceptable (but very bad
style because things that look like strings but aren't are confusing
to handle) to use fixed-size arrays like this (UNIX v6 used directories
with a 14-character array for the name and a short for an inode
number), but DON'T PRETEND IT'S A STRING. You get the extra character
because you don't stop outputting at 12 characters. If you printed
it properly, you'd never go over 12 characters.
>Code to store into the Structure if(no_tok != 0) {
rec++;
strcpy(F.x,temp[0]);
***ERROR*** If temp[0] contains a 12-character string, you are committing
a buffer overflow here. Try:
strncpy(F.x, temp[0], sizeof(F.x));
AND REALIZE THAT F.x DOES NOT CONTAIN A STRING, SO YOU CAN'T PRINT IT LIKE ONE.
F.a = atoi(temp[1]);
F.b = atoi(temp[2]);
F.c = atoi(temp[3]);
if(no_tok == 5)
strcpy(F.y,temp[4]);
Likely you have the same problem here if you need to cram 30 characters
into y.
else if(no_tok == 4) { strcpy(F.Guild, "\0"); } }
and another problem is even if I am using #pragma(1) I am not able to get the size of the struct to 45 and always ends up as 48.
C lets the compiler make the structure any size it wants to make
it. And doesn't that extra non-C crap on the structure declaration
REQUIRE alignment? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Chris Ritchey |
last post by:
Hmmm I might scare people away from this one just by the title, or
draw people in with a chalange :)
I'm writting this program in c++, however I'm using char* instead of
the string class, I am ordered by my instructor and she does have her
reasons so I have to use char*. So there is alot of c in the code as
well
Anyways, I have a linked list of linked lists of a class we defined, I
need to make all this into a char*, I know that I...
|
by: jagmeena |
last post by:
Hello,
I am sure this problem has been addressed before, however, I could'nt get a
suitable solution to my problem. Hence I am posting here. Thanks a lot for
all your help.
The code I have is
typedef struct Rec {
unsigned char msg;
unsigned long len;
|
by: ishmael4 |
last post by:
hello everyone!
i have a problem with reading from binary file. i was googling and
searching, but i just cant understand, why isnt this code working. i could
use any help. here's the source code:
--cut here--
typedef struct pkg_ {
short int info;
char* data;
|
by: derek.google |
last post by:
I have an application that's crashing because of an alignment problem,
and this is the smallest program that demonstrates what's happening:
int main() {
struct Message {
unsigned short size;
};
const int START_INDEX = 1;
char* buffer = new char;
Message* msg = (Message*)&buffer;
|
by: Roy Hills |
last post by:
When I'm reading from or writing to a network socket, I want to use a
struct to represent the structured data, but must use an unsigned char
buffer for the call to sendto() or recvfrom().
I have two questions:
1. Is it generally safe to "overlay" the structure on the buffer,
e.g.:
unsigned char buffer;
| |
by: Juergen Wohnich |
last post by:
Hello,
i want to do store int variablen into a char Buffer.
Like this:
char Buffer;
int b = 1447;
int *pb; // pb deklariert als pointer auf int
pb = &b; // & ist Adress operator, liefert Adresse von b
memcpy( Buffer, pb, 4 );
|
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: nass |
last post by:
hello everyone, i have a bit of problem reading char * strings from a
buffer (a shared memory, pointed to by 'file_memory').
basically i have a structure in memory 'ShMem' that can be accessed by
2 applications (or will be at least when it is done).
the structure is declared in the procedure under the pointer infoVar.
members tXXL are integer lengths of the strings that as all saved
(concatenated) at address of oAndS_str, which is of type...
|
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= |
last post by:
Hi Willy,
Thank you very much for your work.
C++ code doesnot make any serialization.
So at runtime C# code gives an serialization error at
"msg_file_s sa = (msg_file_s) bf.Deserialize(ms);"
I thought that it is very hard to memory map structure array.
I need both read and write memory mapped file at both side of C# and C++.
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |