Reputation:
got some code here that won't compile correctly because it is saying that my pointer is already null when i am testing for a not null expression in my main function. here is the code :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXCODE 53
#define MAXMESSAGE 256
void getCode(char *codeIn, char *filename) {
FILE *codeFile;
/* Open the file with the code */
codeFile = fopen(filename, "r");
if (codeFile == NULL) {
printf("Error opening the code file - program terminated\n");
exit(1);
}
/* Read the first (and assumed only) line from the file */
fgets(codeIn, MAXCODE, codeFile);
/* Terminate the string with /0 */
codeIn[MAXCODE] = '\0';
/* Close the file */
fclose(codeFile);
return;
}
int getMessage(int *message, char *filename) {
FILE *messageFile;
int counter = 0;
/* Open the file with the message */
messageFile = fopen(filename, "r");
if (messageFile == NULL) {
printf("Error opening the message file - program terminated\n");
exit(1);
}
/* Read one number at a time from the file and store it */
while (!feof (messageFile))
{
fscanf (messageFile, "%d", (message+counter));
counter++;
}
/* Close the file */
fclose(messageFile);
return (counter);
}
void sortMessage(int *message, int size) {
int i, j, temp;
for (i=0; i<size-1; i++) {
for (j=i; j<size; j++) {
if (message[i]>message[j]) {
temp = message[i];
message[i] = message[j];
message[j] = temp;
}
}
}
return;
}
void decodeMessage(char *codeIn, int *message, int size) {
FILE *outputFile;
int i = 0;
/* Open the output file */
outputFile = fopen("csis.txt", "w");
if (outputFile == NULL) {
printf("Error opening the output file - program terminated\n");
exit(1);
}
for (i=0; i< size; i++) {
fprintf(outputFile, "%c", codeIn[message[i]%100]);
printf("%c", codeIn[message[i]%100]);
}
printf("\n");
/* Close the file */
fclose(outputFile);
return;
}
int main(int argc, char *argv[])
{
char code[MAXCODE];
int msg[MAXMESSAGE];
int msgSize;
if (argc != 3) {
printf("This program takes two arguments: the name of the file with the code, and the name of the file with the encoded message\n");
}
getCode(code, argv[1]);
msgSize = getMessage(msg, argv[2]);
sortMessage(msg, msgSize);
decodeMessage(code, msg, msgSize);
return;
}
So basically my code is using two files called codefile.txt and msgfile.txt to decode the secret message and write the decoded sequence to a new text file called csis.
Upvotes: 0
Views: 563
Reputation: 15642
As woolstar pointed out in the comments, you don't need to NUL terminate your codeIn
array following fgets
, because fgets
will do that for you. In fact, this constitutes an overflow which we can best see by considering what happens when MAXCODE
is 1: codeIn
contains only one element: codeIn[0]
, and accessing codeIn[1]
is an error.
Similarly, since MAXCODE
is 53
and that's how many elements pointed to by codeIn
, codeIn[message[i]%100]
is suspicious because there's a potential for message[i]%100
to be an invalid index. While we're on this note, it might be wise to make message[i]
an unsigned int
so that it can't be negative. The format specifier (for printf
and scanf
) corresponding to unsigned int
is %u
.
while ( !feof(messageFile) )
is wrong because the EOF
flag isn't set until an attempt is made at reading. Between attempting to read and your EOF
test, however, you've incremented counter
which means you've counted one too many items. Perhaps your loop should look like this:
while (fscanf(messageFile, "%d", (message+counter)) == 1)
{
counter++;
}
Note that this code assumes you've chosen to keep message[i]
as an int
. If you've chosen to use unsigned int
instead, of course you'll want to use the %u
format specifier.
You can probably see that feof
is mostly superfluous... You can usually test for erroneous reads by checking the return value. Try to avoid feof
in the future.
Your main
function has a return type of int
, yet at the end of it you have a return;
statement which doesn't return an int
value. Remove that. It's probably causing errors during compilation.
Presumably, when argv != 3
you want to return from main
so you don't end up processing invalid arguments... Make sure you return an int
value, e.g.
if (argc != 3) {
printf("This program takes two arguments: the name of the file with the code, and the name of the file with the encoded message\n");
return 0;
}
Upvotes: 1