我这里有以下 C 代码来创建一个带有名为 ccipher 的函数的命令。它允许用户执行命令,然后输入一个字母(如果输入
-s
作为密钥,则后面输入数字),就像用户输入 ./ccipher -s 3 file.txt
一样,它会将文件中的内容转换为 Caesar cipher移位 3。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
const int SIZE = 1024;
char data[SIZE], temp;
int key, count;
void getmessage() {
/*
printf("Enter a String: \t");
scanf("%[^\n]s", data);
*/
strncpy(data, "Hello World\n", sizeof("Hello World\n"));
}
void key_input() {
/*
printf("Enter a Key: \t");
scanf("%d", &key);
*/
key = 5;
}
void caesar_cipher_encryption() {
for (count = 0; data[count] != '\0'; count++) {
temp = data[count];
if (temp >= 'a' && temp <= 'z') {
temp = temp + key;
if (temp > 'z') {
temp = temp - 'z' + 'a' - 1;
}
data[count] = temp;
} else if (temp >= 'A' && temp <= 'Z') {
temp = temp + key;
if (temp > 'Z') {
temp = temp - 'Z' + 'A' - 1;
}
data[count] = temp;
}
}
/*
printf("%s\n", data);
*/
}
void caesar_cipher_decryption() {
for (count = 0; data[count] != '\0'; count++) {
temp = data[count];
if (temp >= 'a' && temp <= 'z') {
temp = temp - key;
if (temp < 'a') {
temp = temp + 'z' - 'a' + 1;
}
data[count] = temp;
} else if (temp >= 'A' && temp <= 'Z') {
temp = temp - key;
if (temp < 'A') {
temp = temp + 'Z' - 'A' + 1;
}
data[count] = temp;
}
}
/*
printf("\nDecrypted Message:\t%s\n", data);
*/
}
int main (int argc, char *argv[])
{
int lineNum = 0, endLine = 0, tabReplace = 0, args = argc, num = 1, eolFlag=0, numRead=0, j=0, fd;
int Encrypt = 0, Decrypt = 0;
char buf[SIZE];
char c;
int shift = 0;
// Check for no arguments
if(argc == 1)
{
printf("Usage: %s -EnTsr file.txt\n", argv[0]);
exit(0);
}
// Parse arguments for switches
while(args--)
{
if(argv[args][0] == '-')
{
int switchLen = (int)strlen(argv[args]);
while(switchLen--)
{
if(argv[args][switchLen] == 'n')
lineNum = 1;
if(argv[args][switchLen] == 'E')
endLine = 1;
if(argv[args][switchLen] == 'T')
tabReplace = 1;
if(argv[args][switchLen] == 'r')
Decrypt = 1;
if(argv[args][switchLen] == 's')
{
Encrypt = 1;
key = strtol(argv[args+1], NULL, 10);
}
}
}
}
if (Encrypt != 1)
{
printf("Nothing to encrypt, bye\n");
return 0;
}
// Open file
fd = open(argv[argc-1], O_RDONLY);
if (fd == -1)
{
perror(argv[argc-1]);
exit(1);
}
// Read file
//while((numRead = read(fd, buf, SIZE)) > 0)
while((numRead = read(fd, data, SIZE)) > 0)
{
caesar_cipher_encryption();
if (Decrypt == 1)
caesar_cipher_decryption();
strcpy(buf, data);
// Parse buffer
for(j=0; j<numRead; j++)
{
c = buf[j];
if(lineNum && (eolFlag || num == 1))
{
printf(" %d ", num++);
eolFlag = 0;
}
if(tabReplace && c == '\t')
{
printf("^I");
}
else if(endLine && c == '\n')
{
printf("$\n");
}
else
{
printf("%c", c);
}
if(lineNum && c == '\n')
{
eolFlag = 1;
}
}
}
// Close file
close(fd);
return 0;
}
当我今天在 puTTY 编译此代码时,我收到此错误:
ccipher.c:9:6: error: variably modified 'data' at file scope
9 | char data[SIZE], temp
|
我对 C 语言相当陌生,我正在努力学习它并避免错误,但对于这个问题来说,解决起来有点复杂。有谁对如何避免此错误有任何想法?我将不胜感激!
您不能使用
const
限定文件范围变量作为数组维度。在 C 语言中,const
是 只读 的用词不当。
要修复,请使用宏:
#define SIZE 1024