You are here:

C++/Question regarding a graph in 'C'.

Advertisement


Question
QUESTION: I am working on a VC++6 Graph program that analyses the contents of a file. One of the first things that the program does is to find the amount of bytes contained in the file as this information is used throughout the program operation.
When it comes time for the program to make a graph regarding the nuances of the file, I’m using ‘FLOATPOINT’ which, I guess, is part of VC++ 6’s classes.
My problem is that FLOATPOINT demands an “expected constant expression” or a number such as 1000 (with which the program works) and not a variable (lngWordAmt) as shown in the code below:

float buffer [ 8 ];
long lngFileLen = _filelength ( _open ( “TrackName.trk”, _O_RDONLY ) );
long lngWordAmt = lngFileLen / 20;
FILE *stream1 = fopen (“TrackName.trk”, “rb” );




// FLOATPOINT pData1 [ 1000 ]
FLOATPOINT pData1 [ lngWordAmt ]

for ( intI =0; ( feof ( stream1 ) == 0 ); intI++ )
{
    fread ( buffer, 4, 5, stream1 );
    pData1 [ intI ] .x = ( float ) ( intI );
    pData1 [ intI ] .y = buffer [ 0 ];
}
fclose ( stream1 );

Can you suggest a way for FLOATPOINT to ‘take’ the variable ‘lngWordAmt’ or would you please suggest a better way to get the program’s graph points?

Thank you for your time spent to read and reply to my question,
-Neil

ANSWER: Hello Neil.

When declaring arrays in VC 6, you need a constant for the array size. The current C standard allows for a variable being used in an array declaration, but even VC 2008 has not caught up to that part of the standard. The gcc compiler does support it.

You can do this:
FLOATPOINT *pData1 = (FLOATPOINT*)malloc(lngWordAmt * sizeof(FLOATPOINT));
or this:
FLOATPOINT *pData1 = (FLOATPOINT*)calloc(lngWordAmt, sizeof(FLOATPOINT));

The calloc is nice because it initializes the memory to all 0. Note the difference in the parameter count.

You can continue using pData1 as an array with the indexing operator [].

One more thing to point out, with this:
_filelength ( _open ( “TrackName.trk”, _O_RDONLY ) );
you are losing the file descriptor returned by open so you will not be able to close the file. Of course the file will close when the program ends, but this is generally a sloppy practice.

I hope that helps you.

Best regards
Zlatko

---------- FOLLOW-UP ----------

QUESTION: Thanks again, Zlatko! I tried your suggestions and they both work perfectly! I couldn't find this solution in any of my 10 or 12 books on C and C++. {:o(

I could and should find a new way to find the file length in my books but I'd rather hear your opinion on the subject. How would you suggest that I replace '_filelength ( _open ( “TrackName.trk”, _O_RDONLY ) );'?

Thanks again for your time,
-Neil

Answer
Hi Neil.

Using the filelength function is OK, but I would do it this way:

int fd = _open ( “TrackName.trk”, _O_RDONLY );
long lngFileLen = _filelength(fd);
_close(fd);


so that fd gets closed when no longer needed.

Of course you should add error checking to verify that the open succeeded. The open returns -1 if it fails.

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2012 About.com, a part of The New York Times Company. All rights reserved.