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.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have an assignment on memory blocks. We have a struct Record that I need to save in these memory blocks. Each block holds 5 Records, which i memcpy one after another in a list of blocks and an int CountOfRecords.
We also have a DeleteRecord function that deletes (duh) a particular record from the memory block. Now, other than reducing the Count and shifting all next Records forward as to practically delete that Record, is there any way to ACTUALLY delete what is written with memcpy? Like writing something like a NULL as a struct instance? Memmove does not seem to offer such an application.
EDIT: I write the records as such
//block is the pointer to block,int is for the Count, and record is placed
memcpy(block+sizeof(int)+sizeof(Record),&record,sizeof(Record));
You basically don't want to move data around but instead set the memory to zero, how about memset?
memset(block+sizeof(int)+sizeof(Record), 0, sizeof(Record));
But you somehow have to remember, that the record at this point is zeroed out (not used), best by some property.
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
Schoolwork — No code please. Pseudo code ok.
We are only allowed to use 3 libc functions: read, malloc, and free. However, during the course of school we have implemented and written many of our own versions of libc functions, and those ones we are allowed to use. I would list them, but there are about 50 of them, so instead I will be sure to mention which ones I cannot use.
My question...What are the steps I must take to read a line from a file and store that line as a string, without the \n? Is anyone able to walk me through the process? Because right now, I don't even know where to begin. I know how to use read, and I would consider myself somewhere between beginner and intermediate skill level with C, but after spending an entire day searching Google, every time this same question has been asked, the accepted answer always involves using fgets or getline, which we are not allowed to use, for obvious reasons. I have implemented my own versions of many libc functions that could potentially help in this project, but anything that would make this project quick and easy is obviously not allowed.
Again, please no code, though pseudo code is fine, but I would much prefer it if somebody could help me better understand what I need to 'tell the computer' to do, and from there I should be fine to write the code myself.
Pseudo-code to read 1 line
buffer to 0, size to 0, size_used to 0
loop
read 1 character
no success? - break loop
size_used >= size
make buffer bigger (maybe 2x, at least 1)
[This involves allocating a new buffer, copy existing data, freeing old buffer]
add character to buffer
was character a \n? - break loop
Nothing read?
return NULL
right-size buffer to size_used+1
append \0
return buffer (calling code needs to eventual free it.)
Lots of efficiency improvement are possible. I suggest starting with a basic version, get functionality correct and then consider improvements such as
Performance: read from the file, maybe 4k bytes at a time.
Performance: re-using the returned buffer.
Make robust and check for allocation failures.
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 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 8 years ago.
Improve this question
I'm trying to take user input using the getline() function. I store the input and point to it with a char *pointer.
Now I want to split the string at the white space, if there is any, but I can't change a string literal. So my idea was to transfer a copy of the input to a char array so I could then play around with it. The only issue is I don't know the size of the users input yet so I can't specify the size of the array I want.
Any ideas how I can get around this, I'm probably missing something, I'm new to C from a Java background.
Many Thanks!
You read the line, figure out its size, then make a copy of that size.
If you store a user input with some function getline (there is no such a function in C Standard) then you can split it into tokens by using standard C function strtok declared in header <string.h> If you do not want to change the original string then you can write the required function yourself by means of searching blank and non-blank characters in the string.
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
Consider the following function that is supposed to validate passwords:
char *systemkey = ...... ;
int validate(char* key) {
char* k = malloc(16);
char* sk = malloc(16);
strcpy(sk,systemkey);
strcpy(k,key);
return (strncmp(k,sk,16) ==0);
}
If k and sk are allocated consecutively, that it's easy to break the function by supplying 2 identical blocks of 16 bytes each.
If I'm the one writing the compiler/malloc/free/OS, is there any way I can identify MOST of these type of hacks and prevent them?
EDIT
One possible solution is to put some sort of canary word between each two different allocations. Is there another way?
The best you can hope in use by and operating system is an implementation of malloc that randomizes the memory it returns. It doesn't prevent overflows, but makes exploiting them much harder. For large allocations a technique being used is to return the allocation aligned to the end of the page and leaving the next page explicitly unmapped as a guard page.
You can read a little bit on this page and a the links from it to see how OpenBSD implements malloc protection. As far as I'm aware this is the best you can get from a malloc in an operating system in wide use.
If I'm the one writing the compiler/malloc/free/OS, is there any way I can identify MOST of these type of hacks and prevent them?
One possibility is to use a “sound” static analyzer that, if used properly, can guarantee that your program does not access any invalid pointer for any execution. Here is one.
Another is to use dynamic instrumentation. Valgrind is an example of this approach.