struct

fread() a struct in c

 ̄綄美尐妖づ 提交于 2021-02-07 06:44:52
问题 For my assignment, I'm required to use fread/fwrite. I wrote #include <stdio.h> #include <string.h> struct rec{ int account; char name[100]; double balance; }; int main() { struct rec rec1; int c; FILE *fptr; fptr = fopen("clients.txt", "r"); if (fptr == NULL) printf("File could not be opened, exiting program.\n"); else { printf("%-10s%-13s%s\n", "Account", "Name", "Balance"); while (!feof(fptr)) { //fscanf(fptr, "%d%s%lf", &rec.account, rec.name, &rec.balance); fread(&rec1, sizeof(rec1),1,

Can someone explain Python struct unpacking?

人走茶凉 提交于 2021-02-07 06:27:06
问题 I have a binary file made from C structs that I want to parse in Python. I know the exact format and layout of the binary but I am confused on how to use Python Struct unpacking to read this data. Would I have to traverse the whole binary unpacking a certain number of bytes at a time based on what the members of the struct are? C File Format: typedef struct { int data1; int data2; int data4; } datanums; typedef struct { datanums numbers; char *name; } personal_data; Lets say the binary file

Allowing struct field to overflow to the next field

送分小仙女□ 提交于 2021-02-07 06:16:48
问题 Consider the following simple example: struct __attribute__ ((__packed__)) { int code[1]; int place_holder[100]; } s; void test(int n) { int i; for (i = 0; i < n; i++) { s.code[i] = 1; } } The for-loop is writing to the field code , which is of size 1. The next field after code is place_holder . I would expect that in case of n > 1 , the write to code array would overflow and 1 would be written to place_holder . However, when compiling with -O2 (on gcc 4.9.4 but probably on other versions as

Allowing struct field to overflow to the next field

前提是你 提交于 2021-02-07 06:15:20
问题 Consider the following simple example: struct __attribute__ ((__packed__)) { int code[1]; int place_holder[100]; } s; void test(int n) { int i; for (i = 0; i < n; i++) { s.code[i] = 1; } } The for-loop is writing to the field code , which is of size 1. The next field after code is place_holder . I would expect that in case of n > 1 , the write to code array would overflow and 1 would be written to place_holder . However, when compiling with -O2 (on gcc 4.9.4 but probably on other versions as

Is there UI-independent Point struct in .NET?

耗尽温柔 提交于 2021-02-07 04:42:24
问题 I know several Point structs in .NET: System.Drawing.Point , System.Windows.Point , Sys.UI.Point , but all of them are in high-level UI libraries (GDI+, WPF, AJAX). I need a Point struct for calculations in my class library which I don't want to tie to any specific UI technology. Is there any UI-independent Point struct in .NET? Or I will need to create it myself? That is simple, I know, but sounds like reinventing the wheel. 回答1: Reinvent the wheel. It will run smoother! Really, if it's just

Self-mutate Swift struct in background thread

荒凉一梦 提交于 2021-02-07 03:19:00
问题 Assume we have a struct capable of self-mutation that has to happen as part of a background operation: struct Thing { var something = 0 mutating func operation(block: () -> Void) { // Start some background operation dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)) { // Mutate self upon background task completion self.something += 1 block() } } } Now, when I use such a struct in context: var myThing = Thing() myThing.operation { println(myThing.something) } The

Self-mutate Swift struct in background thread

此生再无相见时 提交于 2021-02-07 03:13:59
问题 Assume we have a struct capable of self-mutation that has to happen as part of a background operation: struct Thing { var something = 0 mutating func operation(block: () -> Void) { // Start some background operation dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)) { // Mutate self upon background task completion self.something += 1 block() } } } Now, when I use such a struct in context: var myThing = Thing() myThing.operation { println(myThing.something) } The

Self-mutate Swift struct in background thread

雨燕双飞 提交于 2021-02-07 03:10:28
问题 Assume we have a struct capable of self-mutation that has to happen as part of a background operation: struct Thing { var something = 0 mutating func operation(block: () -> Void) { // Start some background operation dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)) { // Mutate self upon background task completion self.something += 1 block() } } } Now, when I use such a struct in context: var myThing = Thing() myThing.operation { println(myThing.something) } The

Empty struct in C vs empty struct in C++

风流意气都作罢 提交于 2021-02-07 02:49:29
问题 Why is empty struct in C a constraint violation? Why does this rule get changed in C++? Are there any historical reasons? 回答1: since you don't have inheritance in C you don't need them. If you just want to have a distinguishable pointer type you can use pointers to incomplete types. struct opaque; struct opaque* stranger = 0; should work fine. 回答2: My guess is this: In C, there isn't inheritance, templates, and function overloading - three major reasons we use empty structs in C++ - as a base

Why doesn't a struct in an array have to be initialized?

一曲冷凌霜 提交于 2021-02-06 15:21:56
问题 I researched this subject but I couldn't find any duplicate. I am wondering why you can use a struct in an array without creating an instance of it. For example, I have a class and a struct : public class ClassAPI { public Mesh mesh { get; set; } } public struct StructAPI { public Mesh mesh { get; set; } } When ClassAPI is used in an array, it has to be initialized with the new keyword before being able to use its properties and methods: ClassAPI[] cAPI = new ClassAPI[1]; cAPI[0] = new