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 7 years ago.
Improve this question
I need the most efficient (performance/speed wise) way for reading a configuration from a file in C under linux.
I have not decided on the config format yet, but I would prefer (I'm all ears for better formats) the following format:
buttons 3
size 100
etc.
(The option name then the option value delimited by whitespace)
How can I read the options, should I go for strcmp? Or should I go for character by character manual comparison? Is there a more efficient way?
I want the program to:
be as quick as possible (time)
use as little cpu time as possible
use as little memory as possible
This code will be written for Linux (generic)
So far I'm using: xtest, stdio, stdlib and string libraries
I would rather code everything myself than use external libraries (which are not part of most Linux distributions).
Write/Read binary data for performance.
struct config { /* whatever */ };
struct config config;
configread(&config, "configfile"); // fread() or read() or whatever()
configwrite(&config, "configfile"); // fwrite() or write() or whatever()
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 months ago.
Improve this question
I want to read a file using read() system call and copy all its contents to another file. As the input file can be large, I don't know what buffer size to use. How to change the buffer size dynamically? Or is there any other approach like reading a file part by part using a fixed buffer ? Can anyone tell how to do this.
Read the file part-by-part using a fixed buffer. To copy a file, there is no reason why you have to read the entire file in one call.
If I understood your question correctly, each time you want to read with a different BUFFER_SIZE, read all the file and copy it to an other one.
I think you can easily read the file using BUFFER_SIZE each time, and join the string to what you've read before until Read returns zero which means end of file(check [read(2) — Linux manual page][1]), than you can write the whole thing to the other file, I hope that this answered your question.
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 2 years ago.
Improve this question
File data example line:
Sicilia 005 frenkco mastro 202020kkk 3 11-23-2155 12-44-6666
I want to overwrite that line with another same line copied but with some changed values:
Calabria 006 frenkco mastro 202020kkk 3 11-23-2155 12-44-6666
That's the result i want, but if I change values and then use "fprintf", it prints a new line without deleting the first one.
You cannot overwrite a specific line, because lines are just conventions related to \n bytes. Please read more about C programming and consider reading the n1570 C standard.
You either want to copy the source file to a target one (this is how sed(1) works on Linux; study its source code since it is free software) or use higher-level approaches such as gdbm or sqlite or databases.
For small amount of data practically fitting in RAM (e.g. less than a gigabyte) or in your page cache, a common approach is to generate a new textual file entirely. It might be a temporary file that you'll rename (e.g. using atexit(3) or simply later on)
You could want to read a textbook on operating systems and/or learn to use databases (perhaps MongoDB or PostGreSQL, but there are many other approaches too) or indexed files.
In some cases, you might consider serialization and parsing techniques, perhaps using textual formats like JSON or YAML (for which many open source libraries are available, and worth studying).
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 8 years ago.
Improve this question
I have a c code file (let's say A). I wanted to redirect the output of that code to a text file by using another C program (say B). But the thing is I can't touch (edit) the A file. Is there a way to do so by using FILE operations, maybe?
You have two options here:
Probably the easiest, yet least flexible solution would using system function:
system("A.exe <someargs> > filename.txt");
If you want more flexibility, you should look into your platform APIs. On Windows, you can use CreateProcess specifying a handle to which redirect each of the streams (stdin, stdout and stderr).
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682499%28v=vs.85%29.aspx
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I want to write a program that formats a disc to a new format (not NTFS or FAT32) and can read from and write to the disc, In order to do this, I have to have a way to write single bytes to a disc (without creating files). How do I do that?
EDIT: I found this. But I'm not sure if using CreateFile() as Eli Bendersky said in the first answer (when organized in order of votes) let's you write one byte (or even one bit) at a time, or do you have to write full sectors at a time.
Answer: Since I can't post an answer the question because it was closed, I will answer it write here. You don't need need API functions to do this. All you need to do is open the disk like you open any other file. Like this:
int hFile;
hFile=open("/dev/sdb",0_RDWR);
You program has to directly talk to the driver as you will be by-passing the file-system.