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

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

Related

Is it possible to just replace a particular line in a file without rewriting the whole file [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 3 years ago.
Improve this question
I was trying to construct a function which would say given a line number replace that line with another string. Currently, I'm achieving this by reading the whole file into my RAM, modifying the line in RAM and rewriting the whole file back. I was wondering how databases managed to achieve this since this is a rather frequent operation over there.
line implies a text file, and since those generally have varying line lengths, there is no way to replace a line in the file on disk unless the new one has the same length (the operating system does not provide a way to open or close gaps in a file).
Databases, on the other hand, generally operate on fixed-size entities (per table), so there, the database can replace the content of a 'record' with new data and also keep a list of previously used records that are now deleted and can be recycled when new data is inserted.

Best practice for C program with large number of input files [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 3 years ago.
Improve this question
I am writing a C program that takes 7 input files. I could pass these to my program as command line arguments but it is not exactly succinct and could lead to errors with the order they are provided.
I had thought of creating 1 input file containing the 7 required file names and just passing this to my program.
What is best practice for providing a large number of input files to a C program?
There is a variety of mechanisms in common use for designating input files to programs, prominent among them (in no particular order):
separate command-line arguments, possibly with built-in default values (more on this below)
a common filename stem passed as an argument, with different extensions for the different individual files; the specific filenames are computed by the program from the one stem
a standard or user-specified file containing the names of the files to operate upon
Combinations of those are possible, and there are other alternatives. There is no single best practice, and which method or method(s) to choose is to some extent a matter of opinion and personal preference, likely with a dose of the specific practicalities of the particular program.
I could pass these to my program as command line argument but it is not exactly succinct and could lead to errors with the order they are provided.
I agree.
I had thought of creating 1 input file containing the 7 required file names and just passing this to my program. Is this good practice?
That is one of the common methods. Note, however, that although it makes the command line more succinct, it doesn't by itself do much for the ordering issue.
Personally, I wouldn't be too keen on a program that has seven required arguments, yet I also wouldn't like being forced to write an auxiliary file just to convey filenames to the program. To the extent that you do use command-line arguments, I urge you to define them as options, in the getopt() sense of the term, because mixing up argument order will otherwise be a significant practical problem for your users. Similarly, if you provide a control file as a way to convey the working file names, then I suggest going to just a bit more effort to use an order-insensitive key / value format. To the extent that you can provide sensible defaults, that would improve ease-of-use, too.

Is there a standard spreaded way to parse config files [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 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.

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.

Is an oracle data block similar to a txt file [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 8 years ago.
Improve this question
At this link here it says that the basic unit of oracle storage is a data block.
"One data block corresponds to a specific number of bytes of physical database space on disk."
Is it wrong to say that a data block is a like a .txt file?
A database block is a unit of organisation. So it really isn't very like a .txt file. In fact, it is more like processed cheese.
Perhaps a museum specimen cabinet is a better metaphor. It is a storage device, with a specific location (a table) and broken up into smaller units (rows).
Although, as the data block is actually a unit of I/O we can think of it as a train carriage.
Ultimately, it is best to read the Concepts Guide. Metaphors make for great poetry, but a poor one can confuse more than it enlightens. Find out more.
Off hand, I suspect that a data block has more in common with a file system block. Database files on disk are binary; they can hold anything in whatever representation the database wants. They could even hold parts of things and the rest be scattered across the file. One would almost never try to interact with their content directly. We let the database worry about what is in those files and stay out of its way, much like you would stay out of your OS's way when it's handling your disk drives.
So in short, you shouldn't care. Don't touch the content in those files unless you really, really, really, really know what you're doing. Based on the fact you're even asking this question, you don't know enough about what you're doing to mess with them.

Resources