0

One of my files should be different than the others and this program is supposed to tell me which. I have a feeling that the "_setmode..." could be wrong (actually almost sure, it doesn't seem to have any effect - it's supposed to set input mode to binary at the start of the program). To test, I'm resetting the program after each use and renaming the text files so they are all tested with the same name. I'm using C:/Users/User/Desktop/file.txt as my DOS command.

#include <stdio.h>
#include <fcntl.h>
#include <io.h>

int main(void){
    int s1=0, s2=0, s3=0, s4=0;
    int m1=3, m2=7, m3=13, m4=23;
    int B;
    _setmode(_fileno(stdin), _O_BINARY);

while((B=getchar()) != EOF)
{

   s1 = (s1 + B*m1) % 256;
   s2 = (s1+s2 + B*m2) % 256;
   s3 = (s1+s2+s3 + B*m3) % 256;
   s4 = (s1+s2+s3+s4 + B*m4) % 256;
   B = getchar();
   printf("%02x%02x%02x%02x\n", s1, s2, s3, s4 );
}
return 0;
}

Files I'm using (pastebin with pastebin links to files since I need more rep to post more than 2 links):

File Links: http://pastebin.com/wLF9NRNu

I also just found out that I'm apparently only supposed to be checking the midpoint value, so I was given the following command, which seems more or less useless to me:

C:\ > mdPoint < file.txt

Also after playing with it some more, it seems my commands aren't opening the files at all and is just giving me output based on the command text I enter. Not sure what's up with that.

Link2999
  • 5
  • 4
  • I don't think this code does what you think it does. It iterates over stdin, and calculates a hash value of every-other byte read from the input stream. It is not clear what your intention is. – user590028 Feb 07 '15 at 21:11
  • It's supposed to calculate the hash, but from what's in the files, not from the input stream. This is what the output is supposed to look like, and some extra information about the output (sorry, not returns in comments): For us, the output will be 8 hexadecimal digits, 0-9, a-f, using ascii. The file will be read from standard input using file redirection. The program is used like this: C:\> mdPoint < file.txt MD value is: 31965eca Edit: How would I have it use stdin? – Link2999 Feb 07 '15 at 21:15
  • Ah. I thought you were feeding it the list of files -- I hadn't realized the stdin redirection was of the content. In any event, one thing you'll want to to do is remove the extra call to `getchar()` from within your while loop. It's causing you to skip every other byte in your calculation. – user590028 Feb 07 '15 at 21:18
  • Ya, doesn't work at all if I do that. If I still wanted it to check the characters (but not skip any) from each text file, how would I do that without getchar()? – Link2999 Feb 07 '15 at 21:25
  • I think you have some basic code errors. I've included the revisions as an answer. Try that version. – user590028 Feb 07 '15 at 21:39

2 Answers2

0

Not an answer but too much for a comment. I tested your program on three files. The first file test1.txt contains the text "one".

yourprogram < test1.txt
4d5646e2
7c95787e

An identical file test11.txt has the same content "one" and the same result.

yourprogram < test11.txt
4d5646e2
7c95787e

The next file test2.txt contains the text "two".

yourprogram < test2.txt
5c88c818
a93a4e42

So your comments about the result being "based on the command text" are incorrect, it works off the content. Perhaps you need a better thought out and presented question. And your pastebin does not contain anything easily accessible.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
  • Wouldn't an identical file with the same name provide the same result regardless of it's content if it were based on the text from the input stream? Where are you putting your test files? In the same folder as the program? Is that all there is to the command you're using? Sorry, I'm unfamiliar with DOS commands. Edit: and I apologize about the pastebins, as a new user I'm only allowed to post 2 links, there were 4 text files. The text files that I'm using are pretty long too. – Link2999 Feb 07 '15 at 21:26
  • Oh, I see you renamed the file to test11.txt, not test1.txt again. Maybe I am just using the wrong commands then? – Link2999 Feb 07 '15 at 21:34
0

You had some errors in your code. I believe you mean the following:

#include <stdio.h>
#include <fcntl.h>
#include <io.h>

int main(void){
    int s1=0, s2=0, s3=0, s4=0;
    int m1=3, m2=7, m3=13, m4=23;
    int B;
    _setmode(_fileno(stdin), _O_BINARY);

    while((B=getchar()) != EOF)
    {

       s1 = (s1 + B*m1) % 256;
       s2 = (s1+s2 + B*m2) % 256;
       s3 = (s1+s2+s3 + B*m3) % 256;
       s4 = (s1+s2+s3+s4 + B*m4) % 256;

    }
    printf("%02x%02x%02x%02x\n", s1, s2, s3, s4 );
    return 0;
}
user590028
  • 11,364
  • 3
  • 40
  • 57
  • That's actually what I originally wrote up believe it or not, but the print wouldn't return anything. What would the DOS command be to use a text file with this code? I have a feeling I might just be entering the wrong commands. And is this a valid command? C:\ > mdPoint < file.txt I'm apparently only supposed to get one hash per file. – Link2999 Feb 07 '15 at 21:43
  • What do you mean by "...but the print wouldn't return anything...". You mean there is no output on the screen? – user590028 Feb 07 '15 at 21:45
  • There's no output at all for any command I put into the DOS window. – Link2999 Feb 07 '15 at 21:46
  • What happens when you run your program? It just ends? – user590028 Feb 07 '15 at 21:48
  • DOS window opens (blank), I type my command, then when I press enter, it goes to a new blank line. So still running, just not giving any output. – Link2999 Feb 07 '15 at 21:50
  • Try hitting -D to send EOF. Also, I'm not familiar with `_setmode()`. What happens if you get rid of it? – user590028 Feb 07 '15 at 21:52
  • Still not doing anything. Getting rid of the set mode doesn't have any noticeable effect either. I provided the text files that I'm trying to use in my original post, try testing it for yourself. Someone below said he got it to work as I had it, but I'm not sure what commands he used to open up his files, it may just be I'm using the wrong commands to open my text files. – Link2999 Feb 07 '15 at 21:57