Question

I'm trying to go through a file line by line (each line is no more than 50 characters), shift each character by 10 or -10 (to encrypt and decrypt) and then print the shifted string where the old string was. But I'm getting some really funny output.

heres the code:

#include <stdio.h>
int main(void){
    FILE *fp;
    fp=fopen("tester.csv","r+");
    Encrypt(fp);      // I call decrypt here when I test it.
    fclose(fp);
}

int Encrypt(FILE *fp){
    int offset=10;
    Shift(fp, offset);
}
int Decrypt(FILE *fp){
    int offset= -10;
    Shift(fp, offset);
}
int Shift(FILE *fp, int offset){
    char line[50],tmp[50], character;
    long position;
    int i;
    position = ftell(fp);
    while(fgets(line,50,fp) != NULL){
        for(i=0;i<50;i++){
            character = line[i];
            character = (character+offset)%256;
            tmp[i] = character;                 
        }
        fseek(fp,position,SEEK_SET);
        fputs(tmp, fp);
        position = ftell(fp);
    }
}

so if tester.csv originally reads

this, is, a, test

running the program produces

~rs}6*s}6*k6*~o}~
























êñv[    ‰

this, is, a, test
Was it helpful?

Solution

fputs(tmp, fp);

fputs writes the bytes until the terminating 0 byte.

while(fgets(line,50,fp) != NULL){
    for(i=0;i<50;i++){
        character = line[i];
        character += offset;
        tmp[i] = character;                 
    }

you shift 50 chars, regardless of how long the line that you read in was, and thus most of the time, there is no 0-byte in the tmp buffer, thus fputs often writes at least 50 bytes, some of which have nothing to do with what was in the file at that place, and beyond the buffer, which invokes undefined behaviour and might cause a crash.

You should check for the terminating 0-byte in the loop, probably even stopping at the newline is a good idea.

while(fgets(line,50,fp) != NULL){
    for(i = 0; i < 50 && line[i] != 0 && line[i] != '\n'; i++){
        character = line[i];
        character += offset;
        tmp[i] = character;                 
    }

Note: the loop body would simpler be line[i] += offset;.

OTHER TIPS

Try using GDB to Debug your program as it encrypts.

Compile with: gcc -g -Wall YOURPROGRAM.cxx

Run gdb: gdb YOURPROGRAM.cxx

Set a BreakPoint:

Break at line 3: break 3

Debug your Program: run

You can step through each line of code using step and next, and print out variables at each point using print VARIABLENAME. It's a powerful program, and really helpful in debugging.

You should probably not be using the line-oriented fgets() and fputs() since you could get NUL '\0' characters in the output data on encryption, and in the input data on decryption (and newlines will be . Use fread() and fwrite(). Make sure you handle the right number of characters too; fgets() might not return 49 characters and a NUL; the line might be shorter, and you don't want to process the NUL anyway.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top