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.
Related
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
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 6 years ago.
Improve this question
What is the best way to create a function that reads each part of a file like described in the following picture and saves it to arrays and integers, it must read and save the second part (above word_count): word; orientation; row; col; points; jogador until a number(Turn) is read.
Start by creating 3 struct's. One for people. One for words and one to aggregate the full structure. For the third, you will need to decide which arrays can be sized at compile time and which need to use malloc or calloc to allocate space for the people or word structures.
Next write a function to populate a person from a one line string and one to populate a word from a one line string. You could use strchr to find the semicolons or for less error durability you might look at sscanf.
Finally write your loading function to read the file line by line detecting 'mode' changes by (strchr(line, ';') == -1), and calling the appropriate convert function. You can then return the aggregate structure as a pointer to a malloc'ed struct.
Don't forget to write a function, that takes that pointer, to dispose of everything you malloc'ed so that a caller does not need to know your allocation details and can just say "get me one from that file" followed by "throw this away".
Unfortunately, C is unlike Java or C# in that the heavy lifting is not built in or covered by copious included libraries. You need to find libraries or write low level code yourself.
Good luck with you project.
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
I want to do the following:
#define ConfigureCAN(index,value) CAN.MBOX##index=value
for (i=0;i<15;i++) ConfigureCAN (i,0);
This doesn't work. Is there an other way then:
ConfigureCAN (0,0);
ConfigureCAN (1,0);
...
Thanks.
Edit: Wil this work Or better is the indexing of the stuct correct?
#define ConfigureCAN(index,value) {struct MBOX *Mailbox;Mailbox = &can.MBOX0 + (index * sizeof(struct MBOX)); MBOX=value;}
No, you can't use a for loop index variable (which only gets its value at run-time) to build a compile-time symbol name like that. The symbol names are not available when the program runs, i.e. when the for loop executes, so it doesn't make any sense.
You can probably set up a (pointer) array to make accesses easier to loop, but then you're going to have initialize the array which will be similiar-looking (but enable better run-time performance, if that's your goal).
There is no easy way to do so in C.
Depending on the framework and hardware you use, there might be an alternative way to address your CAN structs:
If you need the MBOX### values more often in a loop, you can create an array with pointers at an appropriate place:
volatile int * MBOXPOINTERS[] = { &CAN.MBOX0, &CAN.MBOX1, /* go on on your own */ };
(replace volatile int by whatever is used in the respective header file)
If you have the device data sheet and you see that the fields are in a successive way, you mght eveb ne able to define an array situated at the appropriate address. But that is highly hardware dependent.
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 is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Next semester, I'll be making a game in C (C89 specifically). Coming from higher-level languages such as C#, one of the first things I would do is make a List of Entities (game objects), and every frame, loop through each Entity and run their Update and Draw methods. Converting this ideology to C, my idea was to develop a "List" structure that contains a pointer to an array of void pointers, and making functions to add, get, and remove entries from the list. If an object is added to the List but the array is too small, realloc the array to a twice-as-large array. With this system in place, when I, say, spawn an enemy, I can add a pointer to its Entity struct to the List, and then each frame, loop through the List, cast each void pointer element as a pointer to an Entity, and then pass it to entity_update(Entity *) and entity_draw(Entity *) methods accordingly.
In my mind, as long as I'm 100% sure to only put pointers to Entity structs (cast as void pointers) in the List array, there wouldn't be any problem... but when I mentioned this to a classmate, he said that he thought there were performance drawbacks associated with casting void pointers to pointers to other things. Looking around on the Internet, from what I can tell, the real problem is that compilers can't properly optimize your code as they would if the compiler knew in advance what type the pointer pointed to. Would this end up being a problem in my situation, where I want to loop through a potentially large-ish array of void pointers, sixty times a second? It wouldn't be a huge deal to just store pointers to Entities in an array of Entity pointers, with a large, predefined maximum size, and then do bound checking to make sure i.e. an enemy isn't spawned if there's no room left in the array... but I just wanted to make sure before I started working on the basic underlying engine stuff for the game.
In C casting a pointer doesn't actually do anything except tell the compiler to treat it as a different type. There's no runtime overhead.
So no.