Click here to Skip to main content
15,887,342 members
Please Sign up or sign in to vote.
4.25/5 (4 votes)
See more:
Don't know if I'm looking at this tired (since I don't see what's wrong) but I have some code dumping data to a file for analysis:

//open file
ofstream file;
CString name;
name.Format("C:\\%s", title);
file.open(name, ofstream::out | ofstream::app);

//write characters (as append)
for(int i=0; i<size; i++)
{
  file << (int) data[i];
  if((i!=0) && ((i%100) == 0)) 
    file << std::endl;
}

//close file
file.close();


As you can see, I'm inserting line breaks every one hundred characters to make it easier to read, but the first line is always 101 characters, what am I not seeing here?... :doh:
Posted

It because you're printing the 100th character and then the endline. Try switching the print statements:

C#
for(int i=0; i<size; i++)
{
    if((i!=0) && ((i%100) == 0))
    {
        file << std::endl;
    }
    file << (int) data[i];
}
 
Share this answer
 
v2
Comments
Manfred Rudolf Bihy 19-Apr-11 16:03pm    
Correct! 5+
Albert Holguin 19-Apr-11 16:04pm    
mbue's solution popped up first on my screen, thanks though! my 5 as well!
mbue 19-Apr-11 16:34pm    
If you want to have fast code: reduce the amount of comparisations. my code is most fast as possible. cause im possessed by speed.
Regards.
Albert Holguin 19-Apr-11 16:40pm    
lol, this is just debug code, it'll be removed when i'm done anyway... you are right though
Rick Shaub 19-Apr-11 16:40pm    
If you were worried about speed, you'd count by 100. Or not. I don't think it matters for this kind of code (test).
The increment happens at the end of the for loop, so you just have put the IF statement before the statement where you are writing into the file.
That's all there is to it! :)

Happy coding,

-MRB
 
Share this answer
 
Comments
Albert Holguin 19-Apr-11 16:05pm    
thanks, i must be tired... looking at code all day...
Nish Nishant 19-Apr-11 16:13pm    
Voted 5!
Manfred Rudolf Bihy 19-Apr-11 16:15pm    
Thanks Nishant!
Use:
if(99==(i%100)) // code

Regards.
 
Share this answer
 
Comments
Albert Holguin 19-Apr-11 16:02pm    
oh, of course! otherwise you end up with 0-100 (101) on the first line... damn, i must be tired.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900