home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / cplus / 19923 < prev    next >
Encoding:
Text File  |  1993-01-27  |  2.2 KB  |  68 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!rational.com!stripe!rmartin
  3. From: rmartin@stripe.Rational.COM (Bob Martin)
  4. Subject: Re: HELP with File I/O
  5. Message-ID: <rmartin.728089285@stripe>
  6. Sender: news@rational.com
  7. Organization: Rational
  8. References: <C1DI47.JJt@well.sf.ca.us>
  9. Date: Tue, 26 Jan 1993 23:01:25 GMT
  10. Lines: 56
  11.  
  12. andras@well.sf.ca.us (Andrew Raskin) writes:
  13.  
  14.  
  15. |arsen@mindvox.phantom.com (Arsen Pereymer) writes:
  16.  
  17. |>  Hi everyone... I am having some trouble with file I/O
  18.  
  19. Perhaps, but there is a critical flaw which may be your problem...
  20.  
  21. |>  #include<stdio.h> 
  22. |>  #include<conio.h>
  23. |>  #include<string.h> 
  24. |>  #include<malloc.h> 
  25. |>  #include<dos.h> 
  26. |>  main() 
  27. |>  char *line;
  28. |>  FILE *in, *out, *read; int i=0;
  29. |>          clrscr();
  30. |>          in = fopen("c:\\autoexec.bat","r");
  31. |>          out = fopen("\\autoexec.dat","wb");
  32. |>          for(i = 0;i <= 1;i++) {
  33. |>                  line = (char *) malloc(sizeof(line));
  34.                                            ^^^^^^^^^^^^
  35. =======================================================
  36.  
  37. This is not doing what you think it is.  What is sizeof(line)?  It is
  38. the size of the variable line.  This variable is a char*, thus the
  39. size is the size of a pointer.  Probably 4, perhaps as small as 2.
  40. You are certainly going to be reading in more than 2 or 4 bytes, so it
  41. may very well be that your are corrupting the heap.  If fprintf is
  42. using the heap for its buffer, you could be corrupting the i/o buffer.
  43. This may be why the file appears unreadable....
  44.  
  45. |>                  fscanf(in,"%s",line);
  46. |>                  fprintf(out,"%s",&line);
  47. |>                  free(line);  }
  48. |>          fclose(in);
  49. |>          fclose(out);  char *r;
  50. |>          read = fopen("\\autoexec.dat","rb");
  51. |>          fscanf(read,"%s",&r);
  52. |>          printf("\nName : %s",r);
  53. |>          fclose(read);
  54. |>          getch();
  55. |>          return 0;
  56. |>
  57.  
  58. |>   Any suggestions? Thanks in advance....
  59. |>
  60.  
  61.  
  62.  
  63. --
  64. Robert Martin       | Design Consulting    | Training courses offered:
  65. R. C. M. Consulting | rmartin@rational.com |  Object Oriented Analysis
  66. 2080 Cranbrook Rd.  | Tel: (708) 918-1004  |  Object Oriented Design
  67. Green Oaks, Il 60048| Fax: (708) 918-1023  |  C++
  68.