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.
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 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 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.
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.
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.
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 have been given a task to write a C language analyser using an AFD. I can choose whichever language I want so I think I will go for Ruby. However this task is a little overwhelming to grasp at the beginning.
The problem I stumble across is : How do I even represent the AFD of the entire C language?.
I have been doing a little bit of digging and I ended up reading this on lexical analysis. In this paper the author defines every token of the language as a transition between 2 states (which is very logical). I find it almost impossible for me not to miss a few or build such a big AFD by hand without many mistakes. Any tips ?
The task you have is a similar one posed to many undergraduate students in compiler courses every year in thousands of universities, and the notes you cite are good sample of the many sets of course notes available on the topic.
The solution is the same as any software engineering problem: testing against the specification.
Although the intellectual problem of the analysis and creation of AFDs for a whole language by hand might seem overwhelming error prone, don't forget you are tasked with also implementing this (in your chosen language of Ruby).
This implementation can be tested by feeding it carefully graded and selected samples of C language input. When it does not deliver the expected result there error will either be in the coding of the AFD or a fault in the AFD you constructed. You make the necessary change and go around the testing loop again.
You will eventually end up with a valid AFD for the entire C language and an analyser for it written in Ruby.
It is often a good idea to start small and implement a subset of the C language and get that working first and then add more to it using stepwise refinement. This is a less risky strategy than attempting to do the whole thing in one go.
You need to apply all those techniques you should have learned about building specifications, designs, programs and testing and apply it to this problem. Just apply good computer science and software engineering to this problem.