|
Thank you, Ravadre!
I will take a look at the articles and the book you suggested.Thanks again!
|
|
|
|
|
hi dude ,
Can you help me in getting Equalizer Sample code in MFC pls , i want it as soon as possible.
thanks
ellappan
|
|
|
|
|
How would I fix this problem I am having?
I am reading datetime values from a database.
DateTime timeIn = DateTime.Parse(myReader["TimeIn"].ToString());
DateTime timeOut = DateTime.Parse(myReader["TimeOut"].ToString());
TimeSpan timeHere = timeOut.Subtract(timeIn);
Now this code works perfectly except for dealing with midnight.
If the person is here at 1:30PM and leaves at lets say 12:04AM, then how do I calulate that? It returns an hour of -13?
The time is entered at 00:04 when put in the datebase (It doesn't let you enter 24:04)
|
|
|
|
|
Is there an actual date associated with the value, or is it strictly time only?
If it is time only, this explains why you're getting a negative value... A simple workaround may be to simply add 24 hours to your result if timeHere is negative, as you can hopefully assume nobody's time traveling and clocking out before they clock in.
|
|
|
|
|
LOL. It is just time only.
So if timeOut.Hour == 0 then I use it to add 24 hours to it then do the subtraction right
|
|
|
|
|
Well, except then you'll get someone poor sod who works until 1am who blows that assumption up.
I think you can do something like
if (timeHere < TimeSpan.Zero)
timeHere += TimeSpan.FromDays(1);
I think this should work, as long as nobody is working longer than 24 hours shifts.
I don't have a compiler in front of me, so you may want to try a few values in that and make sure it works as expected, though.
|
|
|
|
|
Ok I did
timeOut = timeOut.AddHours(24);
then completed it.. It works, thanks!
|
|
|
|
|
Happy to be of assistance.
|
|
|
|
|
Well, until there's one guy who stayed for longer than 1 day there...
|
|
|
|
|
In military time, 00:04 is 12:04AM. There is no such thing as 24:04 in military time; military time starts at 0000 (midnight).
If you are subtracting 1:30PM from 12:04AM for the same date, you should get a timespan result of -13:26.
|
|
|
|
|
Thanks,
I knew there wasnt a 24 but it doesnt work if you subtract 00 from any other number to get the correct time..
So I added 24 to the time that held the midnight number to subtract the other time.
|
|
|
|
|
That would be why you should store the full date and time.
|
|
|
|
|
A true evangelist would add "according to ISO 8601"
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
modified on Friday, June 10, 2011 11:32 PM
|
|
|
|
|
Well, not in this case, as if you would store a complete date and time you should store it as a datetime value, not a string...
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
True evangelists don't care about circumstances, they spread the gospel no matter what.
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
modified on Friday, June 10, 2011 11:32 PM
|
|
|
|
|
Alas, no one listens. And at least he was already using 24-hour time, so I didn't feel I had to invoke the gospel according to ISO 8601.
|
|
|
|
|
Hi. I have to read large files and manipulate strings. The file size may vary and there can be two or more sentences in each line. I chose a char array to read the file in chunks with Streamreader Read().
Because the char array might end in the middle of a line, then this line gets split into two parts.
So instead of getting the following output:
1 This is a line.
2 This is a line.
3 This is a line.
4 This is a line.
5 This is a line.
I actually get…
1 This is a line.
2 This is a line.
3 This is
a line.
4 This is a line.
5 This is a line.
Line No. 3 (and other ones, depending on how large the file is) will be missing when I need to work on it later.
Is it possible to read a char array, BUT until the last new line in the array (in the example above, it would be until the end of line 2) and then start reading a second char array as from line 3?
I know there are other methods like ReadLine(), etc but I would be happy if you could help me with this one. Thank you.
C# Code (simplified):
string fileName1 = "fileName1.txt";
string fileName2 = "fileName2.txt";
FileStream myStream = new FileStream(fileName1, FileMode.Open, FileAccess.Read);
myStream.Seek(0, SeekOrigin.Begin);
StreamReader reader = new StreamReader(myStream);
reader.BaseStream.Seek(0, SeekOrigin.Begin);
StreamWriter writer = new StreamWriter(fileName2);
int blockToRead = 4096;
char[] buffer = new char[blockToRead];
int charsRead = 0;
string myLine = null;
while (reader.Peek() != -1)
{
charsRead = reader.Read(buffer, 0, blockToRead);
myLine = new string(buffer);
myLine = myLine.Substring(0, charsRead);
string[] lines = myLine.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
int i = 0;
foreach (string line in lines)
{
i++;
writer.WriteLine(i + " " + line);
}
}
myStream.Flush();
myStream.Close();
reader.Close();
writer.Close();
|
|
|
|
|
Hi,
this does not make sense whatsoever, your code is too complex and bound to be slow: you are reading a char array, then turning it into a huge string (that is copying all chars), then getting a substring (again copying all chars), then splitting it in a string array (copying all chars again), and now you are worrying about incomplete lines, so after all that effort you still don't have want you want.
Of course you should read lines right away, either with Stream.ReadLine() or even File.ReadAllLines() if your file size is small with respect to your physical memory. ("I have to read large files" does not tell us much). That would be much simpler, faster and less error-prone.
If you insist on reading char arrays your way, you could throw back the incomplete line at the end of your buffer by repositioning your stream (with reader.BaseStream.Seek as you already did somewhere).
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
modified on Friday, June 10, 2011 11:33 PM
|
|
|
|
|
Hi Luc,
Thank you very much for answering. You are right that this code is not the best. It was an example of what I want to do because I do not know how to reposition the stream to start reading again.
With “Large” files I mean over 140 MB. A similar method to the one I posted worked almost so fast as the ReadAllLines and it did not throw any OOM exception.
“ you could throw back the incomplete line at the end of your buffer by repositioning your stream (with reader.BaseStream.Seek”
Could you provide some code to reposition the stream starting either at the end of line 2 (in my example) or at the beginning of the last line (line 3) in the char array, please?
Thank you.
Sue
|
|
|
|
|
Hi Sue,
No, I am not going to provide any code that helps you continuing in the wrong direction,
wrong meaning more complex and slower than the regular approach, using ReadLine or ReadAllLines.
I suggest you throw away your current code and keep things straightforward and simple.
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
modified on Friday, June 10, 2011 11:33 PM
|
|
|
|
|
Usually I just use ReadLine, some times ReadAllLines.
What I have done before in a special case was to read the file one character at a time, appending to a StringBuilder until I reach the end of a line, then processing that line.
|
|
|
|
|
To PIEBALDconsult,
Thank you very much for your help.
Actually I use ReadLine and/or ReadToEnd but since I saw some performance improvement using Read(), I thought I´d give it a try.
The code I posted was just an example. It is not the one I´m using but it shows the same problem that I have - splitted strings.
I will try to find the position in the char array and append the string using StringBuilder.Append method (char, int begin pos, int count)
Sue.
|
|
|
|
|
Hi! I have a project which solves Boolean expressions. The output is a string which when displayed looks like a table.
Here is the output in DOS and in a TextBox:
DOS[^]
TextBox[^]
Why isn't the string aligned in the TextBox? How can I align it??? In DOS, the program works great.
Still learning...
|
|
|
|
|
The console window uses a fixed width font but the TextBox uses a proportional font. The simple solution to your problem is to set the font in the TextBox to Courier.
Alan.
|
|
|
|
|
10x Now it works great! Previously I used Arial.
Still learning...
|
|
|
|