Problem with memory allocation =(

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2007
Posts: 10
Reputation: MiloTN is an unknown quantity at this point 
Solved Threads: 0
MiloTN MiloTN is offline Offline
Newbie Poster

Re: Problem with memory allocation =(

 
0
  #11
Mar 1st, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 10
Reputation: MiloTN is an unknown quantity at this point 
Solved Threads: 0
MiloTN MiloTN is offline Offline
Newbie Poster

Re: Problem with memory allocation =(

 
0
  #12
Mar 1st, 2007
Here we go^^ Ive edited it somewhat (the problem above is still present).

  1. #include <conio.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <errno.h>
  6. #define initial_value 0.07
  7. #define increment 0.125
  8. int read_one_float( float* out_float, FILE *f ) ;
  9. int main(int argc, char *argv[])
  10. {
  11. while (1)
  12. {
  13. FILE *file_pointer = 0;
  14. char ch = 0;
  15. char filename[40] = "" ;
  16. long lSize = 0;
  17. float *buffer = 0;
  18. float tmp_float = 0 ;
  19. int i = 0, j = 0 ;
  20. // Input filename.
  21. printf("\n%c %c %c %c Enter a filename %c %c %c %c : ", 4, 5, 3, 6, 6, 3, 5, 4);
  22. gets(filename);
  23.  
  24. // Try to open the file.
  25. if ( (file_pointer = fopen(filename, "rb")) == NULL )
  26. {
  27. perror("\nError opening file");
  28. puts("\nEnter x to exit, any other to try again.");
  29. if ( (ch = getch()) == 'x')
  30. break;
  31. }
  32. else
  33. {
  34. printf("\nSuccessful opened %s!\n", filename);
  35. //if we reach here file is opened successfully..
  36. //given it's an ascii file lets assume that there is
  37. //one float number per line. So total number of floats
  38. //in file is same as number of new lines + 1.
  39. // calculate the size of our buffer.
  40. while( EOF != (ch = fgetc(file_pointer)) )
  41. if( ch == '\n' )
  42. lSize++ ;
  43. lSize++ ;
  44. rewind (file_pointer);
  45.  
  46. // allocate memory to contain the whole file.
  47. buffer = (float*)malloc(sizeof(float)*lSize);
  48. if (buffer == NULL) return 1;
  49. else printf("\n%d bytes of memory allocated to buffer...", sizeof(float)*lSize);
  50.  
  51. // copy the file into the buffer.
  52. while( 0 != read_one_float( &tmp_float, file_pointer ) )
  53. buffer[i++] = tmp_float ;
  54. printf("\nNumber of entries read : %d\n\n", i-- ) ;
  55. // terminate
  56. fclose (file_pointer);
  57. free (buffer);
  58. // prints values assigned into the array.
  59. printf("\nData read from file is :\n") ;
  60. for( j = i; j >= 0; j-- )
  61. printf("\nbuffer[%d] = %f", i-j, buffer[i-j] ) ;
  62. printf("\n\n") ;
  63. // waits for user input to continue
  64. system("pause");
  65. system ("cls");
  66. puts("\nEnter x to exit, any other begin again!.");
  67. if ( (ch = getch()) == 'x')
  68. break;
  69. }
  70. }
  71. }
  72. //--------------FUNCTIONS--------------
  73.  
  74. int read_one_float( float* out_float, FILE *f )
  75. {
  76. char buf[100] = "" ;
  77. if (0 == fgets(buf, 100, f))
  78. return 0 ;
  79. *out_float = atof( buf ) ;
  80. return 1;
  81. }

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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: Problem with memory allocation =(

 
0
  #13
Mar 1st, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 10
Reputation: MiloTN is an unknown quantity at this point 
Solved Threads: 0
MiloTN MiloTN is offline Offline
Newbie Poster

Re: Problem with memory allocation =(

 
0
  #14
Mar 1st, 2007
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
  1. buffer[1]
, thus cutting off the last value in the file...

Any suggestions?

Chris
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2127 | Replies: 13
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC