Results 1 to 8 of 8

Thread: bug [incorrect incrementation of variable]

  1. #1
    Senior Member
    Join Date
    Jul 2003
    Posts
    813

    bug [incorrect incrementation of variable]

    K so I was having fun writing a little steganographic application, but one of the member functions is giving me a hard time.

    In the code below:

    Code:
    unsigned long count(char file[])
    	{
    	char c;
    	unsigned long size=0;
    	fstream test(file,ios::in||ios::binary);
    	while (!test.eof())
    		{
    		test.read(&c,1);
    		size++;
    		}
    	test.close();
    	return size;
    	}
    size comes up with the value of 107, although I've used to different files to test it with [as it is called under main()]. Now if not anything else, the two files were very different in length, so even in the case of an overflow the two numbers should have been different I think. Any ideas at all? I've looked and looked at the code but I'm blanked out for ideas. Any help is infinitely appreaciated.

    Thanx!

  2. #2
    Jaded Network Admin nebulus200's Avatar
    Join Date
    Jun 2002
    Posts
    1,356
    Did a quick look through some docs trying to figure out where the .read function is inherited in fstream and wasn't able to find it (although I am severely limited for free time, so I probably just didn't look far enough). Regardless, since you defined 'c' as a char, you are essentially reading a byte at a time through the file.

    Since you aren't doing anything with the actual information out of the file, wouldn't it be easier to just get the filesize? Since you are reading a byte at a time from the filestream, it should be the same value...

    /nebulus
    There is only one constant, one universal, it is the only real truth: causality. Action. Reaction. Cause and effect...There is no escape from it, we are forever slaves to it. Our only hope, our only peace is to understand it, to understand the 'why'. 'Why' is what separates us from them, you from me. 'Why' is the only real social power, without it you are powerless.

    (Merovingian - Matrix Reloaded)

  3. #3
    Senior Member
    Join Date
    Jul 2003
    Posts
    813
    Thanks for your answer.

    Okay so using stat of sys/stat.h the file sizes are now 111. Both of them, still. But one of them is a txt file of 1 kb [that's the disk size, I actually need the real size] and the other one is a jpg of 164 kb.

    Anybody any more ideas? Maybe a different function?
    /\\

  4. #4
    Jaded Network Admin nebulus200's Avatar
    Join Date
    Jun 2002
    Posts
    1,356
    Yeah you are going to have to be careful to get the actual size and not the size calculated by the number of clusters (i think it is clusters, maybe sectors, hell I don't remember right now, sorry) allocated per sector by the disk (I forget what the default is in Windows, but this affects the minimum file size by the minimum number of clusters that the file can occupy, so you might have a 1B file but it shows up as 1Kb).

    If I have some free time this week, I will try to do something similar and mention if I find anything...

    /nebulus
    There is only one constant, one universal, it is the only real truth: causality. Action. Reaction. Cause and effect...There is no escape from it, we are forever slaves to it. Our only hope, our only peace is to understand it, to understand the 'why'. 'Why' is what separates us from them, you from me. 'Why' is the only real social power, without it you are powerless.

    (Merovingian - Matrix Reloaded)

  5. #5
    Senior Member
    Join Date
    Jul 2003
    Posts
    813
    Yeah that's partly why I was using the counter with the files. I guess I will try to paste it into /i]another program and just test the function above as part of main. I can watch for size and see how it changes.
    /\\

  6. #6
    Senior Member
    Join Date
    Jul 2002
    Posts
    339
    hypronix, have a look at this example to get length of a file using seekg() and tellg():
    http://www.cplusplus.com/ref/iostream/istream/read.html

    Note that read() take "char *" as argument, not "char".

    Peace always,
    <jdenny>
    Always listen to experts. They\'ll tell you what can\'t be done and why. Then go and do it. -- Robert Heinlein
    I\'m basically a very lazy person who likes to get credit for things other people actually do. -- Linus Torvalds


  7. #7
    Senior Member
    Join Date
    Jul 2003
    Posts
    813
    jdenny thanx a lot for the link, this should work much better [but unfortunately I cannot test right now, will post back my results tomorrow]

    However I know that read takes in a char*, that is why my character is reference as &c. Anyway, otherwise the compiler would return an error for type mismatch. But thanx for the observation.
    /\\

  8. #8
    ummm... are you checking the length of a string, or a file??? i have code for both of these maybe..
    i didn't test this because i'm not at a computer with a compiler, and this may, of course, be completely incompilable, but it is woth a try...
    code:
    //for a string argument//
    //include string.h for this to work//
    unsigned long count(char file[ ])
    {
    unsigned long a;
    a = strlen(file);
    return a;
    }


    //and for the file//
    //include stdio.h//
    unsigned long count(FILE * file)
    {
    unsigned long a;
    char c;
    while(!(feof(file))
    {
    if (file)
    {
    c = fgetc(file);
    a++;
    }
    }
    return a;
    }
    You laugh because im different, i laugh because your all the same.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •