| | |
Problem with memory allocation =(
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Feb 2007
Posts: 10
Reputation:
Solved Threads: 0
The code you provided works quite well thekashyap^^ (it compiles! thats a start!)
The problem with it seems to be that it reads a float value of say 0.7 as 0.00000, which is weird, I ran it with a file that has the values:
0.7
1.5
2.6
3.5
4.5
4.8
5
5.1
5.2
5.2
And the return is:
[code=C]buffer[0] = 0.000000
buffer[1] = 1.500000[/code=C]
etcetc to a final buffer of buffer[9]
Id like to see a linked list demonstrated, but as for right now, that stuff is way over my head! (id be jumping forward about 200 pages in my textbook ;
Thanks for all the help guys^^
Chris
The problem with it seems to be that it reads a float value of say 0.7 as 0.00000, which is weird, I ran it with a file that has the values:
0.7
1.5
2.6
3.5
4.5
4.8
5
5.1
5.2
5.2
And the return is:
[code=C]buffer[0] = 0.000000
buffer[1] = 1.500000[/code=C]
etcetc to a final buffer of buffer[9]
Id like to see a linked list demonstrated, but as for right now, that stuff is way over my head! (id be jumping forward about 200 pages in my textbook ;

Thanks for all the help guys^^
Chris
•
•
Join Date: Feb 2007
Posts: 10
Reputation:
Solved Threads: 0
Here we go^^ Ive edited it somewhat (the problem above is still present).
The error is clearly on line 53, I just dont know what to do about it. ++i doesnt work as well
Hope that helps
Chris
C Syntax (Toggle Plain Text)
#include <conio.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #define initial_value 0.07 #define increment 0.125 int read_one_float( float* out_float, FILE *f ) ; int main(int argc, char *argv[]) { while (1) { FILE *file_pointer = 0; char ch = 0; char filename[40] = "" ; long lSize = 0; float *buffer = 0; float tmp_float = 0 ; int i = 0, j = 0 ; // Input filename. printf("\n%c %c %c %c Enter a filename %c %c %c %c : ", 4, 5, 3, 6, 6, 3, 5, 4); gets(filename); // Try to open the file. if ( (file_pointer = fopen(filename, "rb")) == NULL ) { perror("\nError opening file"); puts("\nEnter x to exit, any other to try again."); if ( (ch = getch()) == 'x') break; } else { printf("\nSuccessful opened %s!\n", filename); //if we reach here file is opened successfully.. //given it's an ascii file lets assume that there is //one float number per line. So total number of floats //in file is same as number of new lines + 1. // calculate the size of our buffer. while( EOF != (ch = fgetc(file_pointer)) ) if( ch == '\n' ) lSize++ ; lSize++ ; rewind (file_pointer); // allocate memory to contain the whole file. buffer = (float*)malloc(sizeof(float)*lSize); if (buffer == NULL) return 1; else printf("\n%d bytes of memory allocated to buffer...", sizeof(float)*lSize); // copy the file into the buffer. while( 0 != read_one_float( &tmp_float, file_pointer ) ) buffer[i++] = tmp_float ; printf("\nNumber of entries read : %d\n\n", i-- ) ; // terminate fclose (file_pointer); free (buffer); // prints values assigned into the array. printf("\nData read from file is :\n") ; for( j = i; j >= 0; j-- ) printf("\nbuffer[%d] = %f", i-j, buffer[i-j] ) ; printf("\n\n") ; // waits for user input to continue system("pause"); system ("cls"); puts("\nEnter x to exit, any other begin again!."); if ( (ch = getch()) == 'x') break; } } } //--------------FUNCTIONS-------------- int read_one_float( float* out_float, FILE *f ) { char buf[100] = "" ; if (0 == fgets(buf, 100, f)) return 0 ; *out_float = atof( buf ) ; return 1; }
The error is clearly on line 53, I just dont know what to do about it. ++i doesnt work as well

Hope that helps

Chris
Last edited by MiloTN; Mar 1st, 2007 at 7:45 am.
Here is my output (am working on Unix):
fwk@smlcdev2(fwk_kash_src)>cat floats.txt
1.1
1.2
-1.4
6.234
2134
fwk@smlcdev2(fwk_kash_src)>cc test.c
fwk@smlcdev2(fwk_kash_src)>a.out
Enter a filename(max 40 chars): floats.txt
Successful opened floats.txt!
24 bytes of memory allocated to buffer for 6 entries...
number of entries read = 4
Data read from file is.
buffer[0] = 1.100000
buffer[1] = 1.200000
buffer[2] = -1.400000
buffer[3] = 6.234000
buffer[4] = 2134.000000
fwk@smlcdev2(fwk_kash_src)>
PS: There is nothing wrong with 1.1 being printed as 1.100000. It's only the way printf() is printing it. You can say %.2f instead of %f, it'll print only 2 digits after decimal point. Like this:
buffer[0] = 1.10
buffer[1] = 1.20
buffer[2] = -1.40
buffer[3] = 6.23
buffer[4] = 2134.00
So just print it the way you want.
fwk@smlcdev2(fwk_kash_src)>cat floats.txt
1.1
1.2
-1.4
6.234
2134
fwk@smlcdev2(fwk_kash_src)>cc test.c
fwk@smlcdev2(fwk_kash_src)>a.out
Enter a filename(max 40 chars): floats.txt
Successful opened floats.txt!
24 bytes of memory allocated to buffer for 6 entries...
number of entries read = 4
Data read from file is.
buffer[0] = 1.100000
buffer[1] = 1.200000
buffer[2] = -1.400000
buffer[3] = 6.234000
buffer[4] = 2134.000000
fwk@smlcdev2(fwk_kash_src)>
PS: There is nothing wrong with 1.1 being printed as 1.100000. It's only the way printf() is printing it. You can say %.2f instead of %f, it'll print only 2 digits after decimal point. Like this:
buffer[0] = 1.10
buffer[1] = 1.20
buffer[2] = -1.40
buffer[3] = 6.23
buffer[4] = 2134.00
So just print it the way you want.
•
•
Join Date: Feb 2007
Posts: 10
Reputation:
Solved Threads: 0
Thanks for the tip on shortening the decimal places^^
The problem with the code im having tho is the first value in my (or indeed any of my) files im opening comes out to be 0.00000, even if its something like 187.3
As I said, its something to do with the statement on line 53, changing to shifts all the values 1 place down the array, so I end up with 0.00000 *then* the first value of the file being read at position , thus cutting off the last value in the file...
Any suggestions?
Chris
The problem with the code im having tho is the first value in my (or indeed any of my) files im opening comes out to be 0.00000, even if its something like 187.3
As I said, its something to do with the statement on line 53, changing
c Syntax (Toggle Plain Text)
i++
C Syntax (Toggle Plain Text)
i--
C Syntax (Toggle Plain Text)
buffer[1]
Any suggestions?
Chris
![]() |
Similar Threads
- problem with freeing memory (C)
- dynamic memory allocation (C++)
- why memory allocation (C)
- memory allocation ptr to array? how? (C)
- Why use Dynamic and Static Memory Allocation... (C)
- memory allocation (C)
- Dynamic memory allocation homework (C++)
- How to use alloc's memory allocation functions? (C)
Other Threads in the C Forum
- Previous Thread: Problems linking libs, I think...
- Next Thread: Merge Sort
Views: 2127 | Replies: 13
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays asterisks bash binarysearch calculate changingto char character cm command copyimagefile creafecopyofanytypeoffileinc createprocess() database directory dynamic execv feet fgets file floatingpointvalidation fork forloop framework function functions getlogicaldrivestrin givemetehcodez global grade graphics gtkwinlinux hacking histogram homework ide include incrementoperators input intmain() iso kernel keyboard kilometer lazy license linked linkedlist linux list lists looping loopinsideloop. lowest matrix meter microsoft mqqueue number oddnumber odf openwebfoundation overwrite pause pdf pointer posix process program programming radix recursion recv recvblocked research reversing segmentationfault sequential single socket socketprogramming spoonfeeding standard strchr string student system testing threads turboc unix urboc user variable whythiscodecausesegmentationfault windowsapi





