首页 > 解决方案 > 为什么打印不止一次?

问题描述

在它读取的我的文本文件中是hello world。我需要这个文件来将文本转换为二进制十六进制、密码、旋转。

当我这样做时./main -b txt.txt,它打印出来的次数比需要的多。它对十六进制输出也是如此。

hello world 的 haxe 十进制输出将输出

48656c6c6f20576f726c640a6c6c6f20576f726c640a6c6c6f20576f726cz1882855 binary output is:1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100 1010 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1 100100 1010 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100

如果我执行 1 个 Ceaser 密码,Ceaser 密码也不起作用,它会打印出来:

IfmmpUXpseYmmpUXpseYmmpUXps

为你好世界

我能做些什么来解决这个问题,因为它不会打印出比需要更多的次数。

 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <iostream>
 #include <getopt.h>
 #include <string>
 #include "more.h"

using namespace std;

int main(int argc, char*argv[]){
int op = 0;
int opt;
int fd = 1;
int buff = 10;
char buffer[buff];
string s;
int n = 0;
int bytesread= 10;
int i = 2;
ssize_t nr = 0;
string v;
while((op = getopt(argc, argv, "xbs:n:c:r:")) != -1){
    switch(op)
    {
    case 's':
    buff = atoi(optarg);
    i += 2;
    break;
    case 'n':
    bytesread = atoi(optarg);
    n =1;
    i += 2;
    break;
    case 'c':
    i++;
        v = argv[i];
    fd = open(v.c_str(),O_RDONLY);
    if (n == 1){
        nr = read(fd,buffer,bytesread);
        if (nr == -1){
            perror("reading file");
            exit(1);
        }
        opt = atoi(optarg);
        s = cipher(buffer,opt,buff);
        write(STDOUT_FILENO,s.c_str(),s.size());

    }
    else{
    do {
    nr = read(fd,buffer,bytesread);
    if (nr == -1){
        perror("reading file");
        exit(1);
    }
    opt = atoi(optarg);
    s  = cipher(buffer,opt,buff);
    write(STDOUT_FILENO,s.c_str(),s.size());
    }while(nr != 0);
    }
    break;
    case 'r':
    i++;
    v = argv[i];
    fd = open(v.c_str(),O_RDONLY);
    if (n == 1){
    nr = read(fd,buffer,bytesread);
    opt = atoi(optarg);
    s = rotation(buffer,opt);
    write(STDOUT_FILENO,s.c_str(),s.size());
    }
    else{
    do{
    nr = read(fd,buffer,bytesread);
    if (nr == -1){
        perror("reading file");
        exit(1);
    }
    opt = atoi(optarg);
    s  = rotation(buffer,opt);
    write(STDOUT_FILENO,s.c_str(),s.size());
    }while(nr != 0);
    }
    break;
    case 'x':
        v = argv[i];
    fd = open(v.c_str(),O_RDONLY);
    if (n == 1){
        nr = read(fd,buffer,bytesread);
        s = hexa(buffer,buff);
        write(STDOUT_FILENO,s.c_str(),s.size());
    }
    else{
    do {
    nr = read(fd,buffer,bytesread);
    if (nr == -1){
        perror("reading file");
        exit(1);
    }
    s  = hexa(buffer,buff);
    write(STDOUT_FILENO,s.c_str(),s.size());
    }while(nr != 0);
    }
    break;
    case 'b':
    v = argv[i];
    fd = open(v.c_str(),O_RDONLY);
    if (n == 1){
    nr = read(fd,buffer,bytesread);
    if (nr == -1){
        perror("reading file");
        exit(1);
    }
    s  = binary(buffer);
    write(STDOUT_FILENO,s.c_str(),s.size());
    }
    else{
    do{
        nr = read(fd,buffer,bytesread);
        if (nr == -1){
        perror("reading file");
        exit(1);
        }
    s  = binary(buffer);
    write(STDOUT_FILENO,s.c_str(),s.size());
    }while(nr != 0);
    }
    break;
    default:
        int  buf[10];
    fd = open(argv[1],O_RDONLY);
        if (fd == -1){
        perror("opening file");
        exit(1);
        }
    do {
    nr = read(fd,buf,bytesread);
    if (nr == -1){
        perror("reading file");
        exit(1);                            
    }
    write(1,buf,nr);
    }while(nr != 0);
    close(fd);
    break;      
    }   

}

return 0;
}

 #include <iostream>
 #include <unistd.h>
 #include <stdlib.h>
 #include <fcntl.h>
 #include <string>
 #include <bits/stdc++.h> 
 #include "more.h"

using namespace std;

constexpr char hexmap[] = {'0', '1', '2', '3', '4', '5', '6', '7',
                           '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

string cipher(char buffer[],int opt,int size){
string s = "";
for (int i = 0;i < size-1;i++){
    if (isupper(buffer[i]))
        s += char(int(buffer[i]+opt-65)%26+65);
    else 
        s += char(int(buffer[i]+opt-97)%26+97);
    }

  return s;

}
string rotation(char buffer[], int opt){
string s = buffer;
reverse(s.begin(), s.begin()+opt); 
    reverse(s.begin()+opt, s.end()); 
    reverse(s.begin(), s.end());    
return s;
}

string hexa(char buffer[],int size){
string str;
string n = buffer;
int s = n.size();
for( int i = 0; i < s; i++)
{
    char const byte = buffer[i];
    str  += hexmap[ ( byte & 0xF0 ) >> 4 ];
    str  += hexmap[ ( byte & 0x0F ) >> 0 ];
}

return str;
}

string binary(char buffer[]){
string n = buffer;
int s = n.size();
string t = "";
    for (int i = 0; i <= s+1; i++)  { 
     int val = int(n[i]); 
     string bin = ""; 
     while (val > 0){
        (val % 2)? bin.push_back('1'):
            bin.push_back('0');
        val /= 2;
     }
     reverse(bin.begin(),bin.end());
         t += bin + " " ;
    }
   return t;              

}

标签: c++

解决方案


推荐阅读