![]() |
|
#1
|
|||
|
|||
Cut -> CHey everyone
As a good excercice for my exam next thursday, i want to make the CUT command in C. But it's a bit out of my league... Can someone here give me the solution? (i think i will lean a lot from that) I want the program to read in a .txt file, just as CUT does |
|
#2
|
|||
|
|||
Re: Cut -> CQuote:
When I say "CUT" to my barber (or my pet's groomer, my pet's veterinarian), it's a lot different than reading (or piping) a file to cut on my Linux system. I don't have a "CUT" program anywhere on any of my systems. Maybe you can clue us in to what you mean. And in general: Before even thinking about a solution, I believe it would be appropriate for you to define the problem fairly explicitly (make a Program Specification). You have indicated exactly one thing: reads a text file. I think there should be a complete description of your goal before starting to consider approaches to creating a solution: 1. What is the input (Input from file, input from console or what? What is the data type? What is the acceptable range of input values, etc.) If it is from a file, where does the file name come from (command line or what?) 2. What is the output? (Print to file, print to screen, save in another variable, return a value to calling function, change value of input variable, or what?) 3. What processing is required to transform an input variable to the output? Give an example or two of what your function is supposed to accomplish. Then show how you might write a function to accomplish it, and why it didn't work, or whatever. Regards, Dave |
|
#3
|
|||
|
|||
Re: Cut -> Cwell,
the function is supposed to be like the cut command in unix. You should input a text file and then show it on the screen. I started with fopen and fclose but i can't get it to work good |
|
#4
|
|||
|
|||
Re: Cut -> CQuote:
So, the command is "cut", not "CUT". Well, for GNU cut (on my Linux systems) there are something like 10 options to the program "cut". It does a lot more than just inputting a text file and showing it on the screen. (Maybe you were thinking of "cat"? That's the one that just takes one or more files, concatenates them ---thus the name "cat" -- and puts the results to stdout.) Anyhow, I will continue with general considerations. The reason that I suggested that you make a formal Program Specification is because in your first post you said, "it's a bit out of my league." OK, fine and dandy. Then don't start trying to implement everything all at once in this fine program (and I really like it--- I use it a lot). This is a learning experience, right? (After all, no one was born knowing this stuff!) I think the way to learn something complicated is to start with a stripped-down version that does something simple and then adds complexity a step at a time (after throughly testing the previous step(s)). In order to make a Program Specification, you ask yourself (and answer in writing) questions about Input, Processing, and Output before starting to code the program. First of all, "cut" is a typical "filter" program: If you don't give it a file name or if the file name on the command line is -, it takes input from stdin. Is this the behavior that you require? Maybe for starters, just assume that the input is from stdin and the output is to stdout, so you would invoke the program by something like the following to redirect input from a file and output to a file: Code:
Its use as a filter might be illustrated by something like Code:
The idea is that inside the program itself (in this example) cut is using stdin and stdout. The operating system is supplying the first cut with output from grep. The output from the first cut serves as the input to the second cut and the second cut has its output redirected (by the operating system) to a file. That's the power of a filter: the program itself is just dealing with stdin and stdout. Now, let's leave aside the options for now (the -d and -f stuff). Here's the first pass of a program implementation that reads and writes lines from/to text files (Its functionality isn't any way related to cut, but it can be a way to test basic I/O before getting to the "good stuff".) Code:
We've come this far together, so I'll spot you the code for starters: CPP / C++ / C Code:
Now, if you save this file as, say, z.c, and if you compile it then invoke it with Code:
Code:
This is an important first step (and I have done all of the work, so far). Now if you are convinced that you can read exactly every line in the file, one at a time, you can start to work on the options for "cut" (I don't think that printing line numbers is one of the options, so the stuff inside the loop will probably look nothing like mine.) Read the man page for cut (or texinfo cut, if your system has the texinfo program and texinfo files for cut installed). Pick an option, say -f. Decide what it does. Practice with the version of cut that you have to work with so that you can see exactly what it does. Write down a description of what your program has to do in order to implement this functionality. Then, and only then, is it time to put the processing stuff in the middle of the loop. Now, there are lots of other ways to implement a loop that reads every line in a file into a buffer so that a program can process them. I should be used to it by now, but I am surprised time and time again at how many wrong ways make their way into classrooms, textbooks and online tutorials. (Sometimes the instructor gets it right, but one student copies it down wrong and everyone else copies it, not from the instructor, but from the misguided student. That is what surprises me, but I should know better). My point is: you don't have to do it the way that I showed, but the way that I showed is a way to get started with it. Don't take my word for it (please). Try it. If you find a way that you like better, go for it!. But I can't tell you how important I feel it is for you to make absolutely certain that you can do something as simple as reading and writing lines from a text file before you get into the "good stuff" of cut or any other program. (If you come back with a loop that goes while(!feof(infile)){...} I won't necessarily grill you over the coals, but I won't hesitate to castigate you in other ways. See footnote number 2.) Just kidding of course, I would never turn mean and nasty on you. Regards, Dave Footnote number 1: For C++, I would probably use getline() to read into a std::string. Footnote number 2: "Youth will be served --- frequently stuffed with chestnuts." (I read it in a work by James Thurber a looong time ago, but I don't think he originated it.) |
Recent GIDBlog
NARMY by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The