Is there a standard spreaded way to parse config files [closed] - c

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 5 years ago.
Improve this question
I want to create a configuration file for my program in C.
I know how I can do this and the problem is I have several ideas in my mind.
I was researching but I cannot find any generalized standard way to create and parse config files, so I am afraid that I will just create a file with some kind of structure like this:
# Section1
STRING_parameter1 Value1
STRING_parameter2 Value2
# Section2
STRING_other_parameter1 Value3
STRING_other_parameter2 Value4
...
etc
And then just parse the file from the different modules, every section in a normal way.
But I am afraid I can come out with some long term problems that only experience can teach and then to have to modify this structure.
That is why I would like to know if there is a better way to do this in order to avoid future issues.

Although you may pair the C language with other scripting and parsing languages, like xml, json, yaml, there is a simple standard way that is used by those who want to have C only programming environment. Matter of taste, I guess. You may simply have an ASCII .txt file with predetermined format, read it from C and update it either manually or by some other problem. This is the simplest solution that will be always C compatible.
However, if you want to go json, or yaml, go ahead, by all means. Just keep in mind, that maintenance may be a bit complicated, if your code gets into the hands of someone who does know C, and doesn't know jsom, yaml or xml.
I hope that this answer your question.

Related

How to overwrite a specific line in a file [C] [closed]

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).

Flex - A fast scanner generator. Why? [closed]

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 6 years ago.
Improve this question
I wonder, why is Flex used even till now as far as I know?
If it is not used now and was used earlier, then also what is the advantage it provided over writing C code directly?
This is what I read about Flex
It takes as its input a text file containing regular expressions, together with the action to be
taken when each expression is matched. It produces an output file that contains C source code
defining a function yylex that is a table-driven implementation of a DFA corresponding to the
regular expressions of the input file. The Flex output file is then compiled with a C compiler to
get an executable.
What is the need of Flex? Is it better than writing directly C programs?
better in terms of execution or speed of code writing?
I am referring this as my source
Compared with writing out a state machine by hand, it certainly takes less code to produce a lexical scanner with flex. It is also much easier to read a flex specification and understand what tokens are recognized by it.
While it is possible to hand-optimize a scanner and beat flex in terms of execution time, it is rarely a good use of programmer time. In most parsing problems, the lexical scan is not the bottleneck, and a small performance improvement will be invisible. Also, the naive use of tools like regular expression libraries is likely to produce code which is both much slower and much harder to maintain.
Nothing has changed in the C language over the last 20 years which would affect either of the above statements.
All of the above is contingent on the programmer having some understanding of how to use the tool and for which problems it is and is not appropriate. As with any toolset.

Getting started with coding unix commands [closed]

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
I have been learning c and data structures for quite some time now and I wanted to see whether I could apply what I have learnt. I searched a bit and found out that I could start with util linux but, before I could do so, I thought I'd check and perhaps dabble a bit with the code for basic unix commands like "cat". I was able to understand what a part of the code might have been trying to do, but I was not able to understand the entire code as a unit.
For example, in the "cat" code, a pointer to the output buffer and input buffer is declared and is appropriately used, which I could understand. What i could not understand, are parts of code like io_blksize (stat_buf) which has no description whatsoever, on what it does. Or how two pointers declared as pointers to the input and output buffers, actually correspond to the input and output buffers ?
So my question being, how do I approach these type of code, how can I understand something that has no description to what it does (in the example given above) and how can I make and implement changes in the code, so that I can see the changes when i run a command ?
(Would really appreciate references or topics I should start with, so that I can relate what I have learnt to how command code's can be modified. I also apologize if the question is to abstract.)
This is a bit of a subjective question so my answers will just be my opinion of course.
A good place to start when you run into something you don't recognise while reading source code is the manpages. Each function will generally have a manpage, e.g. man 2 read or man 3 printf. Beyond that, I feel perhaps you should get more of a foundation in Unix before attempting to read the straight source code, a good book is Advanced Programming in the Unix Environment. I've been working through it myself and am finding my Unix knowledge improving considerably.
Just my two cents.

How to create a Program/Command that will search files for every instance of "Real" and then replace them with "double." [closed]

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
I just started coding in C. I need to create a program/command that will search through the various program files and directories, locate any instances of the word "Real" and then replace them with the word "double". This program/command will need to be able to search though .c and .h files (and possibly more).
Basically, I would like to avoid having to search through thousands of lines of code, and dozens of files, just to retype the word "Real" as "double". As you are aware, these are variable types, and I need to change one to the other. Any ideas? Thoughts?
sed -e 's/Real/double/g'
On your input files...
I realize that this may require multiple programs (one for each file type).
Nope, that's completely false.
What about looking in to some intelligent text editors, like Notepad++ (https://notepad-plus-plus.org)? It has the capability to search across multiple files, multiple folders and multiple extensions, also using Regular Expressions.
In general, anyway, editing code with find-and-replace utilities would be not my first choice. When writing C code, it's better to use some typedef keywords, or some preprocessor related stuff, to write a code which is more editable and scalable in the future.
Create a typedef instead:
typedef double Real;
Now any instance of Real is an alias to double.
You don't want to use a simple text find/replace tool like sed since that could potentially change the string "Real" which appears in comments, string constants, or as part of another identifier such as getRealUser.

What do you mean by file handling? [closed]

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
What does a file handling mean? What is the difference between them in different languages?, for example perl or c. I'm unable to get a grasp on the concept.
I'm posting an example from perl suggested by #jeegar patel.
open(DATA, " <file.txt");
if you see in this piece of snippet, DATA acts as a file handle, so what exactly is the function/purpose of "DATA" here.
PS: I apologize if this is such a lame question, but I'm trying to get the basics right! Appreciate if anybody could acknowledge this.
In Any programming language, to perform any file operation in programming way it will have some own APIs.
Like C programming has.
http://www.w3schools.in/c/file-handling/
Like perl has
http://www.tutorialspoint.com/perl/perl_files.htm
What is the difference between them in different languages?
You can read file handlings APIs for different programing language and came to know what are different in them..
In fact your program does not get direct access to physical hardware and devices. Your program has to request these as services from the operating system. In each language there is a "run time model" of what a pralooks like, things like where the stack, heap and program memory are. The file system provided by the language is contains the routines needed to coordinate with the operating system, convert data types to the format required by your program and ways to find out the status of the file or even where to put things so the OS will actually write them out
This is complex stuff and each language and language design team has approached the problems somewhat differently.
Trust me they used to be a lot less standardized...
There are many functions on various level of file system hierarchy in the word "File handling".
An example of file system hierarchy and functions in each level:
Handle file systems:
Device/Disk/Directory(Folder)
create/delete/rename/move
Handle a file:
create/delete/rename
Handle a contents of a file:
open/close/read/write
 etc.
Each languages have corresponding functions and/or libraries/APIs for "file handling".
When you focused in certain level of hierarchy, "a contents of a file" for example, functions of that level may be comprehensible for you.
Don't to try to see broad functions in all levels at once.
That is not the way of human kind, but of god.

Resources