Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I need to reed a csv file in C.
I made a struct like this:
typedef struct Node{
void** elem;
List* next;
}List;
If I dont'know what's in csv file, how can I read the elements? They can be different in the same record.
A sample of a record is this:
12,ciao,45,2.4
Where i have a different type in the same record.
I saw fget function yet, but it return a char* or char... I would like something more generic because I don't know what there's in the record.
I will use this struct with sorting algorithm.
In sorting algorithm i have a compare function like this
typedef int (*CompareFunction)(void*, void*);`
so if i can read void* on csv file and than put the element on the struct this wuold make easy the compare function.
you can use a lot of function
read function
fgets function as you said
of course you don't know the content of your file but using a char * is the best solution
in both cases you will use a char * because it's the best way to read your file; and can convert your data to an other type really easily
for example if you read a number in your file, then you can convert it to an integer using atoi for example
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Writing a program in C that is similar to ls. My program needs to implement the -S flag, meaning it sorts whatever directory is specified (or the current one if not) by the size of its contents in bytes.
Obviously you can easily do an alphabetical sorting with scandir and alphasort (see https://www.cs.cf.ac.uk/Dave/C/node20.html). My question is whether you can use scandir with a comparator function that compares two entries on the size of their contents.
Alternatively, I could just specify NULL for the comparator function, have the unsorted array, and then sort it myself with the qsort() function.
Any advice much appreciated. Any clarifications needed, just say in the comments.
Converting comments to an answer.
Since the POSIX declaration of scandir() is
int scandir(const char *dir, struct dirent ***namelist,
int (*sel)(const struct dirent *),
int (*compar)(const struct dirent **, const struct dirent **));
you can write a comparator function that uses stat() on each of the two pointed at directory entries and compares the sizes in any way you see fit. The difficulty is caching the stat data; there isn't an obvious way to do that, but if you have thousands of files, you probably don't want to be running two stat() system calls each time you make a comparison — that would be pretty much the ultimate in 'slow compare vs fast swap'.
I was thinking of that, but then I thought that a lot of work would be done for even a few hundred files. Any suggestions?
I'd probably go with a a simple comparator (alphasort() perhaps), and then when the data's returned by scandir(), create an array of a data structure that can cache the file names and sizes (and any other data that you want for your comparisons), and then use qsort() on the array of the data structure.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am currently studying the crazyflie 2.0 drone Firmware. For those who do not know the drone, here is a link to the Website:https://www.bitcraze.io/crazyflie-2/
It is an open source Project.
Anyway, I have toruble understanding some part of the Firmware Code. Actually it might be very simple, but I am very new to programming in C.
struct CommanderCrtpValues
{
float roll;
float pitch;
float yaw;
uint16_t thrust;
} __attribute__((packed));
static struct CommanderCrtpValues targetVal[2];
You can find this Piece of Code at: https://github.com/bitcraze
crazyflie-firmware/modules/src/commander.c
I do not understand the last line. I believe that it is an assignment of a struct to an Array, named targetVal, but I am not sure. Could you explain what's really going on ?
This is creating a static array of 2 CommanderCrtpValues structures.
Since this is declared in the global scope its memory is going to be initialized as 0 (i.e. the all the fields will have the value 0) when the program starts, see Why are global variables always initialized to '0', but not local variables?. I do not think this detail is important in this particular case but it is used in other places of the firmware.
(Disclaimer: I am the author of this code.)
The code defines a structure named CommanderCrtpValues that has four fields (roll, pitch, yaw, thrust).
The last line defines a variable named targetVal that is an array of 2 elements. Each array element is a structure of CommanderCrtpValues.
The variable targetVal has not been initialized yet so the actual content of each element is undefined. Even though you know that they are CommanderCrtpValues structures, you don't know what values the fields contain.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am trying to write a C program in Linux system which the main function is read a data file (csv format 200MB file) in struct array and searching condition file (few lines) then output the matching result.
The read data function takes around 1 second to run and the matching part is pretty quick. I am thinking is that possible I can pre read the data file in memory by some methods then run the searching function for many time as I want.
It maybe similar to R. Read a csv file first then do some calculate from it.
Create a tokenizer using read system call to read until you hit the comma and then update up to that part to your struct using memcpy or strncpy. After that it would be easy for searching and validation.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am curious about struct that we declare in C. What does it actually do ?
I know we can make nodes & pointers to those using struct but how does it function ? Like a while loop checks the condition & branches accordingly if equal or not equal. What does struct do under the hood ?
A struct type is a user-defined composite type. It is composed of fields or members that can have different types.
From struct-wiki:
A struct in the C programming language is a declaration that defines a list of variables to be placed under one name in a block of memory, allowing the different variables to be accessed via a single pointer.
For memory allocation of a struct: check out How are C struct members allocated?
For why to use struct, check out Why should we typedef a struct so often in C?
Its nothing more than multiple variables treated as one entity. There is not much magic behind this, the values simply appear behind each other in the order of declaration in the memory.
A C struct simply represents data in a specified way and doesn't do anything at all. It is used to represent a more complex data-type, such as a linked-list node.
Using struct a user can define its own required data type to handle complex data.
just like array where all elements in array are of same type but in struct each element can be defined in user's desired way.
so struct is used to define ""user-defined data types".
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
My question is a conceptual one(just for my knowledge).
Is it possible that we get a loop in memory addresses while using an array of pointers to point to multiple linked lists?
Suppose I have some code like this:
struct linkedList{
int data;
char name[20];
struct linkedList *next;
};
struct linkedList *head[10];
Is it possible that 2 or more pointers in above declaration may end up pointing to same addresses?
If so, how to prevent this situation?
Yes, of course it's possible. That would be called a "circular list", and is sometimes very useful.
It can be prevented by writing code to detect such a situation.