DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Code Snippet: Simple File i/o routines (http://www.daniweb.com/forums/thread217391.html)

wavsyntax Jun 3rd, 2009 6:32 am
Simple File i/o routines
 
This is my file manipulation library.

  1. [code]
  2. #include <iostream>
  3. #include <sys/stat.h>
  4.  
  5.  
  6.  
  7. //Detect whether a file exists
  8. bool existsf(char file[]){
  9. struct _stat buff;
  10. if(!_stat(file,&buff)){
  11. return true;
  12. }
  13. else{
  14. return false;
  15. }
  16. }
  17.  
  18. //count bytes
  19. int countfb(char file[]){
  20. struct _stat buff;
  21. _stat(file,&buff);
  22. return buff.st_size;
  23. }
  24. //Read binary
  25. void readfb(char file[],char buffer[],int fsize){
  26. FILE *f=fopen(file,"rb");
  27. int check=0;
  28. int count=0;
  29. check=fgetc(f);
  30. while(count<fsize){
  31. buffer[count]=(char)check;
  32. check=fgetc(f);
  33. count++;
  34. }
  35. buffer[count]='\0';
  36. fclose(f);
  37. }
  38.  
  39. Write binary
  40. void writefb(char file[],char str[],int fsize){
  41. FILE *f=fopen(file,"wb");
  42. int count=0;
  43. while(count<fsize){
  44. fputc(str[count],f);
  45. count++;
  46. }
  47. fputc(str[count],f);
  48. fclose(f);
  49. }
  50.  
  51. //Count the number of chars in a file
  52. int countf(char file[]){
  53. FILE *f=fopen(file,"rb");
  54. int check=0;
  55. int count=0;
  56. check=fgetc(f);
  57. while(check>0){
  58. count++;
  59. check=fgetc(f);
  60. }
  61. fclose(f);
  62. return count;
  63. }
  64.  
  65. //Read file
  66. void readf(char file[],char*buffer){
  67. FILE *f=fopen(file,"rb");
  68. int check=0;
  69. int count=0;
  70. check=fgetc(f);
  71. while(check>0){
  72. buffer[count]=(char)check;
  73. check=fgetc(f);
  74. count++;
  75. }
  76. buffer[count]='\0';
  77. fclose(f);
  78. }
  79.  
  80. //Write to a file
  81. int writef(char file[],char string[],char* mode="wb"){
  82. FILE *f=fopen(file,mode);
  83. int count=0;
  84. while(string[count]!=0){
  85. fputc(string[count],f);
  86. count++;
  87. }
  88. fputc(string[count],f);
  89. fclose(f);
  90. return count;
  91. }
  92.  
  93. //Append text to a file
  94. int appendf(char* file,char* text){
  95. char data[countf(file)+1];
  96. readf(file,data);
  97. std::string newd;
  98. newd=text;
  99. newd+=data;
  100. writef(file,(char*)newd.c_str());
  101. return 0;
  102. }
  103. [/code]
niXman Nov 1st, 2009 4:02 am
Nonsense!

Ancient Dragon Nov 1st, 2009 9:20 am
You don't need those loops in readb() and writeb(). All it takes is one line of code, using fread() and fwrite().

niXman Nov 1st, 2009 11:57 am
There a lot of things too much. And almost half of lines of code errors.
PS
this code could go here? ;)


All times are GMT -4. The time now is 2:30 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC