944,103 Members | Top Members by Rank

Ad:
  • C++ Code Snippet
  • Views: 1844
  • C++ RSS
-1

Simple File i/o routines

by on Jun 3rd, 2009
This is my file manipulation library.
C++ Code Snippet (Toggle Plain Text)
  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]
Comments on this Code Snippet
Nov 1st, 2009
0

Re: Simple File i/o routines

Nonsense!
Light Poster
niXman is offline Offline
39 posts
since Oct 2009
Nov 1st, 2009
-7

Re: Simple File i/o routines

You don't need those loops in readb() and writeb(). All it takes is one line of code, using fread() and fwrite().
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Nov 1st, 2009
0

Re: Simple File i/o routines

There a lot of things too much. And almost half of lines of code errors.
PS
this code could go here?
Light Poster
niXman is offline Offline
39 posts
since Oct 2009
Message:
Previous Thread in C++ Forum Timeline: Filtering a vector's contents based on regex
Next Thread in C++ Forum Timeline: Sort by alphabet





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC