Click to See Complete Forum and Search --> : bug [incorrect incrementation of variable]
hypronix
March 10th, 2004, 09:02 AM
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:
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!
nebulus200
March 10th, 2004, 05:51 PM
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
hypronix
March 10th, 2004, 08:27 PM
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?
nebulus200
March 10th, 2004, 10:42 PM
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
hypronix
March 10th, 2004, 10:48 PM
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.
jdenny
March 11th, 2004, 08:40 AM
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>
hypronix
March 11th, 2004, 10:09 AM
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.
kryptonite0110
March 16th, 2004, 02:59 AM
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;
}