[英]Getting segmentation fault(core dumped)
My program has to encrypt/decrypt the textfile but I'm getting segmentation fault(core dumped)
when I do this: 我的程序必须加密/解密文本文件,但是这样做时我遇到了
segmentation fault(core dumped)
:
./program 9999 input.txt output.txt
The program takes every character from the input
file and converts it based on the passed key
. 该程序从
input
文件中获取每个字符,并根据传递的key
对其进行转换。 It compiles fine when I compile in CodeBlocks and does not give any errors. 当我在CodeBlocks中编译时,它可以正常编译,并且不给出任何错误。 Could smb tell me what's wrong with the code?
smb可以告诉我代码有什么问题吗? Thanks!
谢谢!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//Checks if the input arguments are less than 2 (starting from 0)
//A user should enter 3 arguments so as not to reach this method
int badProgram(const char *const program){
printf("The program is missing some of the files!");
return -1;
}
//Encrypts the passed inputFile and
//Produces its output to the passed outputFile
//Key is also passed as the seeding number
int encryptFile(FILE *input, FILE *output){
char c;
char p;
int r = 0;
char p1 = 0;
char c1 = 0;
while((p = fgetc(input)) != EOF){
r = rand() % 97;
//change all displayable characters [0...96]
if(p == 't'){
p1 = 0;
}
else if(p == '\n'){
p1 = 1;
}
else{
p1 = p - 30;
}
c1 = p1 ^ r;//bitwise xor
if(c1 == 0){
c = 't';
}
else if(c1 == 1){
c = '\n';
}
else{
c = c1 + 30;
}
//Write
fprintf(output, "%c", c);
}
}
int main(int argc, char *argv[])
{
//Check the number of the entered arguments
if(argc < 2){
return badProgram(argv[0]);
}
else{
FILE *input;
FILE *output;
//Seed a number into Random Generator
int key = *argv[0];
srand(key);
input = fopen(argv[1], "r");
output = fopen(argv[2], "w");
encryptFile(input, output);
}
return 0;
}
The **input.txt**
looks like this: **input.txt**
看起来像这样:
Hello, World!
Bye!
Couple of things that are wrong with your code: 您的代码有两点错误:
int key = *argv[0];
is most likely not doing what you think it does. 很可能没有按照您的想法去做。 What it actually does is the following:
它的实际作用如下:
assign an ASCII value of the first character of the
[0]
argument (program name) to an int variable将
[0]
参数(程序名称)的第一个字符的ASCII值分配给int变量
It is likely that what you intended to do there is:您打算在那里做的可能是:
int key = atoi(argv[1]); // this converts "9999" into an int 9999
input = fopen(argv[1], "r");
opens a file named (in your case) "9999" for reading and fails. 打开一个名为(以您为例)“ 9999”的文件进行读取,但失败。 You never check for the error so this is causing a crash the moment you are trying to use the
input
FILE
pointer. 您从不检查错误,因此在尝试使用
input
FILE
指针时会导致崩溃。 The fix is to use the argv[2]
解决方法是使用
argv[2]
argv[3]
for the output file argv[3]
encryptFile
function must return a value as it is declared int
(don't know why you want to return a value from it as you never use it) encryptFile
函数必须返回一个声明为int
的值(不知道为什么您不希望使用它从中返回值) Once you fix the above issues your program no longer crashes 解决上述问题后,您的程序不再崩溃
Update 更新
A bit of explanation for the above issues and general info: 有关上述问题和一般信息的一些解释:
argv
lists all the input parameters as strings ( char*
) where the first ( [0]
) argument is the executable name and is not your first argument "after" the program name argv
将所有输入参数都列为字符串( char*
),其中第一个( [0]
)参数是可执行文件名称 ,而不是程序名称之后的第一个参数 int
(or a double
, for that matter) but provides a whole set of functions to deal with numbers' parsing. int
(或者是double
),但是提供了一组完整的函数来处理数字的解析。 Some examples of those functions are: 'atoi', 'atol', 'atof'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.