I have no where really else to turn. I have been working on an assignment for over a week, and still confused. I made a cat file and have to upgrade the file. I felt as though I am on the right path, but once again I'm stuck.

• The ability to specify a command line parameter (-s x ) to change the size of the buffer used
with read and write to be x bytes.
• The ability to specify a command line parameter (-n x ) to change the number of bytes read
• The ability to specify a command line parameter (-c x ) to change the shift to x when applying
a simple Caesar cipher to text within the file (described below).
• The ability to specify a command line parameter (-r x ) to specify the shift to x when applying
a similar rotation to binary data within the file.
• The ability to specify a command line parameter (-x ) to output the data in the file as hexadecimal
numbers.
• The ability to specify a command line parameter (-b ) to output the data in the file in binary
notation, most significant bit first.

These are the line parameters I am trying to include, and feel like I am close to having them all work. The Caesar shift I feel like the loop I wrote will work, but when it comes to binary and hexadecimal representation NO IDEA.

Here is what it states:

Binary rotation
While the Caesar cipher above was interpreting the bytes that you read as text, changing only the
alphabetical characters, this option should apply to any binary value. The principle is the same, but
instead of applying it to A-Z and a-z you apply it to any one-byte binary value from 0-255.
All numbers are stored as binary bits on the computer. Each char is one byte, which is 8 bits. When
dealing with files that are not meant to be read by a human (binary as opposed to ASCII), it can become convenient to encode the data in a format called hexadecimal. Hexadecimal, the base-16
number system, is useful precisely because it is based on a power of 2 (24 = 16), and so each hexadecimal
digit represents 4 bits. Numbers represented as decimal (base-10) do not line up in the same
way. Each byte is 8 bits, so it can be written as 2 hexadecimal digits. Your program should be able to
output the data in this format (2 hex digits per input byte) whenever the -x command line parameter
is enabled. Keep in mind that there will be two characters of output for every character of input in
this mode.
For the caesarshift I figured this loop would work.

caesarShift(char *buffer)
{
int count = 0;
int length;
length = (int)buffer.length();

for(count = 0; count < length: count++ )
{
if(isalpha(buffer[count]))
{
for(int i = 0; i < 13; i++)
{
if(buffer[count] == 'z')
{
buffer[count] == 'a';

}
else
buffer[count]++;
}
}
}
}
This is main, and I have some of it commented out, it was working, but now it isn't.\

#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

int main(int argc, char* argv[]){

int fd, count, opt;
int i = 2;
int sizeOfBuffer = 1024;
int numBytes = 1024;
char *buffer;
struct stat fileInfo;

while((opt = getopt(argc, argv, "s:n:c:r:x:b")) != EOF)
{
switch(opt)
{
case 's':
cout << "Whatis the size of buffer you want to use?" << endl;
cin >> sizeOfBuffer;
break;
case 'n':
cout << "How many bytes of the file do you want to read in?" << endl;
cin >> numBytes;
break;
/* case 'c':
caesarShift();
break;
case 'r':
rotation();
break;
case 'x':
break;
case'b':
binaryOutput();
break;

case 'r''c'|| 'c' 'r':
cout << "error: cannot pass both options in at the same time." << endl;
break;
case 'x' && 'b':
cout << " Cannot represent data as both binary and hexadecimal." << endl;
exit(-1);*/
default:
cout << "Unknown option." << endl;
return 1;
}
}
for( i; i < argc; i++)
{

fd = open(argv[i],O_RDONLY);
if (fd == -1)
{

perror("open failed");
continue;
}

while((count = read(fd, buffer, sizeOfBuffer) > 0))
{

if (fd == -1)
{

continue;

}

while(count = read(fd, buffer, numBytes) > 0)
{
write(fd, buffer, numBytes);
cout << buffer;
}

}

close (fd);

}
return 0;

}
I'm not clear on what you're on, but you do have some problems.

First for all, does the basic cat work? I think not. My comments are against the lines of code.
 ``123456789101112131415161718192021222324252627282930313233343536`` `````` int fd, count, opt; int i = 2; //kbw: logically, it doesn't make sense to initialise this here int sizeOfBuffer = 1024; int numBytes = 1024; char *buffer; //kbw: you've just declared a pointer, it should either be an array or you allocate the memory struct stat fileInfo; //kbw: ... for (i; i < argc; i++) //kbw: is i initialized to the right value? { fd = open(argv[i],O_RDONLY); //kbw: you really ought to declare fd locally, even though this is C if (fd == -1) { perror("open failed"); continue; } while ((count = read(fd, buffer, sizeOfBuffer) > 0)) //kbw: you read 1K { count = read(fd, buffer, numBytes); //kbw: then you read 1K again, overwriting the previous content if (fd == -1) //kbw: feof(f) checks for end of file { perror("read failed"); continue; } while (count = read(fd, buffer, numBytes) > 0) //kbw: then you read 1K again, overwriting the previous content { write(fd, buffer, numBytes); //kbw: you're writing into your input file cout << buffer; //kbw: I thought this was a C program } } close (fd); }``````

I recomed you approach it in this way.
1. Decide whether you want to use C or C++.
2. Make your cat work with two fixed files. This will make you get the basic algorithm right.
3. Convert that to take the file names from the command line. This will force you to contend with arguments.
4. Add the -s option. This will force you to revise the buffer you use.
5. Add each of the other options in turn. They all require manipulation of the output.

BTW, this is the implementation of Plan9's cat. It won't compile as is anywhere else, but you can use the algorithm as a guide.
https://9fans.github.io/usr/local/plan9/src/cmd/cat.c
Last edited on
Topic archived. No new replies allowed.