Question:
Cut program in C language?
2009-03-16 19:12:50 UTC
I need to develop a utility program named cut that prints certain "columns" of one of more files. For example, we can use it to print characters 5 through 10 of each line of a file. As another example, if we have
a file where each line consists of strings or numbers (that we’ll call fields) delimited by commas (as
in a CSV file), we can use cut to print fields 5 though 10.

There is a similar program in UNIX called cut but I need to make it in a windows system.
Three answers:
2009-03-17 00:34:12 UTC
Here is my solution: http://pastie.org/419470



It should work in both Windows and Linux. (i set it to handle the different line endings, and it worked on my tests for both systems) If you give it bad data, it will act up, I didn't code for exceptions (ie giving chars instead of numbers), I did have it check that the correct number of argments exist, though.



Compile it and run it from the command line or terminal like this:



cut input.txt output.txt characters 10 15



This will call the program (cut) have it open the file "input.txt" read characters 10 through 15 (inclusive) and print them out to the file "output.txt"



Or you could do like this



cut input.txt output.txt fileds 2 3



This will print fields 2 through 3 inclusive to the output file.



In reality, it tells the difference between characters and fields by just checking the first character of the third input. If it begins with a 'c' then it assumes characters. Otherwise, it assumes fields. So you could shorten these to



cut input.txt output.txt c 10 15

cut input.txt output.txt f 2 3



To print a single character or field, use the same begin and ending index.



All indexes begin at 1. So the first character is character 1, and the first field is field 1.
bobette
2017-01-16 22:51:13 UTC
The languages delivers you some geared up-in issues like key words and purposes that carry out some predefined initiatives. You strengthen a software making use of this prebuilts. This prebuilts are defined via the language developer interior the computing device language bits and bytes. whenever you run a software that's compiled (in case of C,C++) or interpreted then compiled (in case of Java). This step makes particular of no syntatical blunders interior this technique and produces a compiled code (.obj document (C,C++) or byte code (Java)). This compiled code is now carried out i.e., training are performed as reported. This technique of compilation is executed via compiler. The compiler is a gadget, which interprets severe-point language to gadget-point and vice-versa. those training are given to the processor chips making use of ssome command words. The processor chips are additionally programmed. we could first see how a chip works. each processor has a assembly language linked with it. This language instructions are like writing words particularly of sequence of bits or bytes. The command words are the sequence of bits whose each and each bit or team of bits defines a particular function. The processor recieves the command word reads each and each bit and produces the wished output interior the variety of sequence of bits and sends to the thinking gadget. certainly this processor chip works with the voltages; 0 bit representing decrease volt or 0 volts and a million-bit representing bigger voltage or any constructive voltage (relies upon on the chip). On recieving the end result from the chip, the compiler converts it into the wished style and provides the output on the reveal. i attempted to describe with my finished ability, wish it particularly is going to sparkling your doubts.
2009-03-17 07:38:26 UTC
Assuming you can use the cstring header, you can use strtok to seperate a CSV file into fields:



http://www.cplusplus.com/reference/clibrary/cstring/strtok.html



In the first case, just read in the line as a character array. Then, use a for loop to output:



int i;

for(i = 4; i < 10; i++) {

cout << input[i];

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...