arrays - C中文件范围错误的可变修改“数据”
问题描述
我在这里有以下 C 代码来创建一个带有名为 ccipher 的函数的命令。它允许用户执行命令然后输入一个字母(如果输入 -s 作为密钥,则输入一个数字),就像用户输入 ./ccipher -s 3 file.txt 它会将文件中的内容转换为 Caesar移位为 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 ARGS
if(argc==1)
{
printf("Usage: %s -EnTsr file.txt\n",argv[0]);
exit(0);
}
//PARSE ARGS 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 中,是read-onlyconst
的误称。
要修复,请使用宏:
#define SIZE 1024
推荐阅读
- javascript - 如何使此滚动内容高于内容 120 像素?
- c# - 将数据添加到实体框架中的动态表
- python - 吉他音符的Python FFT频率
- python - selenium add_cookie 需要两个请求
- android - 检测 RecyclerView 内 Recyclerview 的点击
- ios - iOS UICollectionView 推送刷新时的意外行为
- c# - 从辅助监视器获取像素颜色的更快方法是什么?
- python - 我应该如何将 base.css 与 Django 中的 base.html 文件链接?
- php - 避免 PHP double To: header
- r - 速度-加速度热图