|
Yes, this is the strangest thing I've ever seen programming, it should be the same value, but mysteriously it isn't.
I must be making something extremely incorrect, but I cannot find it.
I cannot understand how, if inside the OnTimer event I call SetValor and the code is this:
if (boolvar)<br />
{<br />
SetValor(...<br />
}
Then when I'm inside the function I see the code entering in the first code:
if (!boolvar)<br />
{<br />
FIRST CODE<br />
}<br />
else<br />
{<br />
SECOND CODE<br />
}
This is incredible and I have no clue on how to change that.
Moreover, as I was explaining in the first post, that value is not being changed anywhere else, it only changes after one click in the MSChart object.
[Mod]
Sorry, I've missed to say Thank you!
[/Mod]
|
|
|
|
|
I'm losing sleep over this...
Sorry in advance about the long question
I've got an unformatted binary(.mid) file I want to display as text. When I use fscanf, like this:
*****************************
#include <stdio.h>
int main ()
{
void *list;
int i;
FILE * pFile;
pFile = fopen ("C:/Moondance.mid", "rb");
for(i=1; i<5; i++) //read 4 chars
{
fread(&list, 1, 1, pFile);
printf("%c ", list);
}
list=0;
for(i=1; i<5; i++) // read 4 bytes of decimals
{
fread(&list, 1, 1, pFile);
printf("%x ", list);
}
fclose (pFile);
return 0;
}
************************
it shows a byte at a time like this:
M T h d 0 0 0 6
(file header) (next chunk size)
where 0 0 0 6 is really 00 00 00 06, a 4-byte number
But when I try to replace the "for" number reader code with this:
******
fread(&list, 4, 1, pFile);
printf("%x ", list);
******
I get:
M T h d 6000000
It seems to have switched endedness or read each byte in the wrong order into the pointer.
I want it to print out as just decimal "6"
help?
regzim
|
|
|
|
|
regzim wrote: When I use fscanf, like this...
Are you hiding its usage someplace?
Why do you have list defined as a void* rather than a char ? fread() needs a spot to store the information it reads.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
fread doesn't care at all about 'endianness'.
the 'endianness' here is in the act of treating those four bytes like an integer value.
#include < stdio.h>
int main(int argc, char* argv[])
{
unsigned char b[4] = {1,2,3,4};
unsigned int p = *(unsigned int *)&b[0];
printf("%x", p);
return 0;
}
prints "4030201"
|
|
|
|
|
Aw, crumbs. I meant to write in the fscanf version, but it really doesn't matter. My intended point was that every function (except fread) I have been able to find to read a file either reads it one byte at a time, or needs a delimiter, like a null or so. Sorry about that, David.
Chris- I still don't get it... does that mean anytime I try to put four bytes together into an int to print, it stores it backwards? Was your example intended as the way not to do it, or as the only way to do it? The four-byte 00 00 00 06 is translated as an integer, "6". I could just "is.ignore" the extra zeros, but what if those spaces are filled with hex code for a larger integer?
Thanks for your replies,
regzim
|
|
|
|
|
If you do a binary write of an int to a file, then there are two ways to order the bytes, either msb first or lsb first. As long as you read / write on the same hardware you are OK and you don't need to worry about the endian nature. If you are reading byte by byte, and then want to reconstruct your int you need to know which order they were written. If you transfer binary files between machines with different hardware you can also run into difficulties.
See http://www.cs.umass.edu/~verts/cs32/endian.html[^]
|
|
|
|
|
regzim wrote: Was your example intended as the way not to do it, or as the only way to do it?
it's an example of the way it is: the byte order of an integer in memory is not the same as what you'd write on a piece of paper.
regzim wrote: The four-byte 00 00 00 06 is translated as an integer, "6".
it's translated as an integer, 6000000
regzim wrote: I could just "is.ignore" the extra zeros, but what if those spaces are filled with hex code for a larger integer?
why would an integer value ever have 'extra' data in it? if you only want that one byte, then just read that one byte. if you need all four bytes, you have to correct for byte order and accept all four bytes. right?
|
|
|
|
|
I think I get now that this isn't an endianness issue at all.
>>why would an integer value ever have 'extra' data in it? >>if you only want that one byte, then just read that one >>byte.
well, for example:
00 00 00 06
is simply decimal "6", and the leading zeros are moot. But
f0 09 43 f7
| |
------------
|
(really a 4-byte long int)
(in my context) is a four byte/32 bit unsigned long int that makes for a very large nuber, and every byte is important to the resulting long int value, unlike the previous example's leading zeros. Until I "getc" the data in the file, I don't know what I'm going to get at all but I do know it's going to be four bytes that together represent a long int at any given time. I hope I'm putting this right...
>> if you need all four bytes, you have to correct for byte order and accept all four bytes. right?
Right. And I think I can finally ask a much more succint question than the rambling diatribe I had before:
I need to accept all four bytes at once. How do I correct for byte order?!
P.S. Keep in mind that I'm C++ self taught and there may be some gaps in my knowledge of the fundamentals, so don't assume I already know the most obvious, simple answers...
regzim
|
|
|
|
|
regzim wrote: I need to accept all four bytes at once. How do I correct for byte order?!
if you know for sure that the byte order of the data you've read is in the wrong order, you can use a function like _swab to do it. or, you can use this:
void SwabInt32(__int32 &v)
{
__int32 t;
BYTE* cp = (BYTE*)&v;
t = cp[0] << 24;
t |= cp[1] << 16;
t |= cp[2] << 8;
t |= cp[3];
v=t;
}
that just reverses the order of the bytes in the input __int32.
give it a whirl.
|
|
|
|
|
Ahhhhhh...
God bless you, Chris. In taking apart your code, I've realized I could do one of two things:
1. "fread" 4 bytes at a time into a long variable and reverse the order of bytes
<or>
2. "getc" a byte at a time, and bitwise left-shift them a byte at a time until all four bytes are filled, like so:
{
FILE * pFile; // pointer to the file
unsigned long Value;
Value = getc(pFile); // put first byte in Value Value = (Value << 8) + (getc(pFile)); //move everything over 1 byte
Value = (Value << 8) + (getc(pFile)); //move everything over 1 byte
Value = (Value << 8) + (getc(pFile)); //move everything over 1 byte
printf("Chunk Size: %d\n", Value);
}
This works so far, but I'll have to road test it a little while to see...
THANK YOU for your time! Now I can sleep...
regzim
|
|
|
|
|
sorry about that- screwed up the formatting-
Value = getc(pFile); // put first byte in Value Value = (Value << 8) + (getc(pFile)); //move everything over 1 byte
is supposed to read
Value = getc(pFile); // put first byte in Value
Value = (Value << 8) + (getc(pFile)); //move everything over 1 byte
regzim
|
|
|
|
|
no problem.
just a note on getc v fread: it's generally faster to read as much at a time as you can. with 4 bytes v 1 byte, the difference might not be great, but in general, it's worth the trouble to avoid single-byte reads, if you can do a multi-byte read.
|
|
|
|
|
Hey guys, it has been a while /cry. I seem to be having some issues with comparing values in a file, such as finding the highest value in a file in mixed order and outputting to a file in correct order. I have a file created with the following text 4,3,1,2, my object is to find the highest value then output to a file in correct orsder to 1,2,3,4. This is all I have and I am a little rusty cause it has been a while:
// iofilecomp.cpp : Defines the entry point for the console application.
// iofilecomp.cpp : Explains how to copy the contents from one file to another.
//
#include "stdafx.h"
#include <conio.h>
#include <string>
#include <fstream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
ifstream ins; //reads from file
ofstream ous; //writes to file
ins.open("in.dat");
ous.open("ou.dat");
string line;
if( ins.is_open )
{
while ( ! ins.eof() )
{
getline(ins, line);
cout << line << endl;
}
ins.close();
}
else cout << "Unable to open file.";
ous.close();
_getch();
return 0;
}
The above will only output in the order read from the file. Possibly could I get some assistance or pointed in the right direction please. Thank you.
|
|
|
|
|
read the file like you do, put the values in a vector , sort the vector and write it back to a file.
|
|
|
|
|
Herboren wrote: ...my object is to find the highest value then output to a file in correct orsder to 1,2,3,4.
If you are simply writing the values to a file in ascending order, why bother with finding the highest value?
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
have a file created with the following text 4,3,1,2, my object is to find the highest value then output to a file in correct orsder to 1,2,3,4. This is all I have and I am a little rusty cause it has been a while:
Go back and re-read what I typed , I never stated that it was pre written in ascending order. in the first line in big characters is what I had said.
|
|
|
|
|
Herboren wrote: my object is to...output to a file in correct orsder to 1,2,3,4.
How is that not writing to the file in ascending order?
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
=D Basically I have a pre-written file with random numbers, then locating the highest number in that file, (yes I know what the highest number is I just want the program to do it) then comparing the highest number to the other integers in that file and placing them in correct order in a different file, I dont know any other basic way to put it.
|
|
|
|
|
Herboren wrote: ...I dont know any other basic way to put it.
Maximilien told you how to do it, while Stephen showed you how.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Hi,
If you want the list of numbers to be written back in the file in ascending order, then all u have to do is sort the numbers u have as many others have mentioned, the sorting takes care of the highest number in each case.
locating the highest number in the file helps to sort only 1 time. say 4 3 1 2, now u find the highest number to be 4 and then u write as 3 1 2 4. now from the remaining 3 numbers u have to again find the highest which is 3 and then sort as 1 2 3 4. this is highly inefficient way of doing. this is called bubble sort i suppose. there are many algorithms like quick sort, heap sort for reading a string of values and writing them back in the desired order (ascending or descending). hope u find it useful.
regards,
-cedance
|
|
|
|
|
As a matter of fact it was bubble sort, from what I recall my last teacher telling us about it, I had totally forgotten about it but popped when you mention it. That why I wasnt familiar with the heap or quick sort, I suppose she wanted us to do something hard so that way we knew how to understand it afterwards.
|
|
|
|
|
Try something like this:
#include "StdAfx.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <iterator>
#include <algorithm>
int main()
{
using namespace std;
ifstream in("C:\\In.txt");
if (!in)
{
cerr << "Failed to open input file!" << endl;
return 1;
}
ofstream out("C:\\Out.txt");
if (!out)
{
cerr << "Failed to open output file!" << endl;
return 2;
}
vector<int> nums;
copy(istream_iterator<int>(in), istream_iterator<int>(), back_inserter(nums));
sort(nums.begin(), nums.end());
copy(nums.begin(), nums.end(), ostream_iterator<int>(out, "\n"));
return 0;
}
Note I read the whole file into a std::vector , sort it and then output the results.
Steve
|
|
|
|
|
I did find something rather useful and did find your code very useful working with vectors. Something called an insertion sort. looks custom but does a nice job.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <insertion_sort.h>
int main()
{
double vals[25];
int n=25;
srand((unsigned int) time(NULL));
for (int i=0; i<n; i++) vals[i]=((double) n*rand())/RAND_MAX;
printf("\nArray to be sorted:\n");
for (int i=0; i<n; i++) printf("%3.0f ", vals[i]);
Array::Sort::insertionSort<double>(vals, n);
printf("\n Sorted array:\n");
for (int i=0; i<n; i++) printf("%3.0f ", vals[i]);
printf("\n");
return 0;
}
here is the location of the header. <a href="http://www.stepanovpapers.com/algSimMeas/insertion_sort.H" rel="nofollow">Insertion Sort</a>
-- modified at 17:33 Sunday 11th March, 2007
|
|
|
|
|
You can do better then an insertion sort which is O(n^2). Use the STL's sort algorithm which comes with your compiler and get O(N log(N)) complexity. To access the algorithm include <algorithm> and replace the sorting code with this:
std::sort(vals, vals+n);
See http://www.devx.com/vb2themax/Article/19900[^] for a comparison of the efficiencies of different sorting algorithms.
For things like sorting and the like the STL will suite your needs just fine and you're better of learning it then downloading equivalent or inferior code.
Steve
|
|
|
|
|
Hi,
I've made a little Window with the MFC, but when reposition the window and then select new, the window jums some lines down and some lines right. Did you know how i could avoid this ?
void CTestView::OnFileNew()
{
CRect rc;
GetWindowRect (&rc);
CMainFrame *pFrame = (CMainFrame*)AfxGetMainWnd();
CClientDC dc(this);
dc.SetMapMode (MM_TEXT);
dc.LPtoDP (&rc);
// ClientToScreen (&rc);
pFrame->SetWindowPos (&CWnd::wndTop, rc.left, rc.top, 200, 100 ,SWP_NOREPOSITION);
}
Here is my codr, i hope you can help me !
Many THX
CrocodileBuck
|
|
|
|
|