GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 28-May-2006, 08:57
bulbulred bulbulred is offline
New Member
 
Join Date: May 2006
Posts: 2
bulbulred is on a distinguished road

Cut -> C


Hey 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  
Old 28-May-2006, 09:16
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,621
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Cut -> C


Quote:
Originally Posted by bulbulred
Hey everyone

i want to make the CUT command in C.

Can someone here give me the solution?

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  
Old 28-May-2006, 13:12
bulbulred bulbulred is offline
New Member
 
Join Date: May 2006
Posts: 2
bulbulred is on a distinguished road

Re: Cut -> C


well,

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  
Old 28-May-2006, 15:07
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,621
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Cut -> C


Quote:
Originally Posted by bulbulred
well,

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

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:
cut [various options here] <infilename >outfilename

Its use as a filter might be illustrated by something like
Code:
grep "#include" z.c | cut -d\< -f2 | cut -d\> -f1 > includefiles.txt

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:
Make a loop that reads (from stdin, or from any other text file) a line at a time. Since this is the C board, I respectfully suggest fgets() to put each input line into a char array. See footnote number 1. After successfully reading each line, just print it out (printf() is good enough for now, but you might want to use something else). Continue with this loop until there are no more lines to be read.

We've come this far together, so I'll spot you the code for starters:

CPP / C++ / C Code:
/* This can be used as a starting point for a filter program.
 * As shown here, it takes input from stdin and sends output
 * to stdout.
 */
#include <stdio.h>
#include <string.h>

int main(/* no arguments for now: later will use argc, argv */)
{
    FILE *infile;
    int line_number;
    char inbuf[BUFSIZ]; /* defined in stdio.h */
    char *chpt;        /* pointer used by strchr() */

    /* 
     * later we might want to use fopen() to let the program
     * use a file whose name was given to us by a user option.
     */
    infile = stdin; 


    line_number = 0;
    /*
     * The actual program will do lots of different things, depending
     * on command-line options from the user. Here is a way to make
     * a loop through the entire input file a line at a time.
     */
    while (fgets(inbuf, sizeof(inbuf), infile)) {
        ++line_number;
        chpt = strchr(inbuf, '\n'); /* look for newline                 */
        if (chpt) {                 /* if it's there, kill it           */
            *chpt = 0;              /* Now the string doesn't have '\n' */
        }
        /* 
         * Here's where you operate on the line. I'll just print it
         * out with its line number
         */
        printf("%5d: %s\n", line_number, inbuf);
    }

    printf("Total number of lines read was %d\n", line_number);

    return 0;
}

Now, if you save this file as, say, z.c, and if you compile it then invoke it with
Code:
./z <z.c >z_lineno.txt
It should read exactly the number of lines in the file (44 if you paste it directly into your text editor). The output file will look like this:

Code:
1: /* This can be used as a starting point for a filter program. 2: * As shown here, it takes input from stdin and sends output 3: * to stdout. 4: */ 5: #include <stdio.h> 6: #include <string.h> 7: 8: int main() 9: { 10: FILE *infile; . . . 41: printf("Total number of lines read was %d\n", line_number); 42: 43: return 0; 44: }

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 GIDBlogNARMY by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 03:07.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.