User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 402,375 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,102 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 681 | Replies: 2 | Solved
Reply
Join Date: Mar 2007
Posts: 3
Reputation: Eyies is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Eyies Eyies is offline Offline
Newbie Poster

C++ File Generator

  #1  
Mar 29th, 2007
Hey all, I'm new to this forum, first post.. so please excuse me (also excuse the perhaps not so eloquent style, it is still in preliminary!) =). I'm writing a short piece of code in compliance with a webserver I've been programming with a group. This code is to auto generate an assorted sizes of files (which will later be used to test latency timings and average GET request timings) starting from size Class 0 which are files less
than 1KB, class 1 files are less than 10K, class 2 files are less than 100K and class 3 files
are less than 1MB. It should auto-populates the specified directory with the four
classes of files. There will be 9 files per class. Files in class 0 are in increments of
0.1KB (starting at 102 bytes) while class 1, class 2 and class 3 are in increments of 1KB. Syntax to use the code is HTMLGenerator -d <directory>. Output files should have the form of class<classsize#>_<increment>.

My greatest problem is that our school's compiler is old and does not contain <sstream> hence making my INT to String conversion difficult. Currently the code seg faults, likely due to improper usage of the *buffer. Any questions regarding the code I will promptly reply as I'm still working away. Any suggestions on how I can get this done? Thank you all greatly for your time and help ! =)

  1. #include <string>
  2. #include <cstring>
  3. #include <iostream>
  4. #include <fstream>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string>
  8.  
  9.  
  10. using namespace std;
  11.  
  12. int main(int argcount, char* args[]){
  13. int number_of_arguments = argcount-1;
  14.  
  15. for (int i=1;i<=number_of_arguments;i++){
  16. string argTemp = args[i];
  17.  
  18. if (argTemp == "-d"){
  19. //begin loop
  20. string classSize,increm;
  21. for (int i=0;i<1;i++){
  22. if (i==0){classSize="102";increm="100";}
  23. if (i==1){classSize="1024";increm="1000";}
  24. if (i==2){classSize="10240";increm="10000";}
  25. if (i==3){classSize="102400";increm="100000";}
  26.  
  27.  
  28. int incremTEMP=atoi(increm.c_str());
  29. int classSizeTEMP=atoi(classSize.c_str());
  30. for (int j=0;j<9;j++){
  31. string dest=args[2];
  32. dest=dest+"/class"+classSize+"_"+increm;
  33. ofstream myfile (dest.c_str());
  34. if (!myfile.is_open()) { //check validity of file
  35. cerr << "Error: cannot open the file " << dest << endl;
  36. exit (-1);
  37. }else{
  38. for (int k=0;k<(classSizeTEMP+incremTEMP)/2;k++){
  39. myfile<<"LO";
  40. }
  41. }
  42. myfile.close();
  43. incremTEMP=incremTEMP+j*incremTEMP;
  44.  
  45. //problems here (i'm really not sure i'm using
  46. //sprintf correctly at all, let alone storing the
  47. // data in buffer back into increm
  48. char *buffer[100];
  49. sprintf(buffer[100], "%d",incremTEMP);
  50. for (int i=1;i<=100;i++) increm+=buffer[i];
  51. delete *buffer;
  52. }
  53. }
  54. }
  55. }
  56. }
  57.  
Last edited by Eyies : Mar 29th, 2007 at 11:11 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Mar 2007
Posts: 3
Reputation: Eyies is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Eyies Eyies is offline Offline
Newbie Poster

Re: C++ File Generator

  #2  
Mar 29th, 2007
Hi all, I have managed to find a IntToString algorithm and I THINK its working, however the files are not generating. Sigh. If you could look over my code that would be great!

  1. #include <string>
  2. #include <cstring>
  3. #include <iostream>
  4. #include <fstream>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string>
  8.  
  9.  
  10. using namespace std;
  11.  
  12. string IntToString(unsigned int x)
  13. {
  14. static const int Radix = 10;
  15. static const int MaxDigits = 20;
  16.  
  17. if (0 == x) return "0";
  18.  
  19. char Tmp[MaxDigits + 1];
  20. char* Cur = Tmp + MaxDigits;
  21. *Cur = '\0';
  22. while (x && (Cur > Tmp))
  23. {
  24. --Cur;
  25. *Cur = (x%Radix) + '0';
  26. x /= Radix;
  27. }
  28. return Cur;
  29. }
  30.  
  31. int main(int argcount, char* args[]){
  32. int number_of_arguments = argcount-1;
  33.  
  34. for (int i=1;i<=number_of_arguments;i++){
  35. string argTemp = args[i];
  36.  
  37. if (argTemp == "-d"){
  38. cout<<"sup"<<endl;
  39. //begin loop
  40. string classSize,increm;
  41. for (int i=0;i<3;i++){
  42. cout<<i<<endl;
  43. if (i==0){
  44. classSize="102";increm="100";}
  45. if (i==1){classSize="1024";increm="1000";}
  46. if (i==2){classSize="10240";increm="10000";}
  47. if (i==3){classSize="102400";increm="100000";}
  48.  
  49.  
  50. int incremTEMP=atoi(increm.c_str());
  51. int classSizeTEMP=atoi(classSize.c_str());
  52. for (int j=0;j<9;j++){
  53. string dest=args[2];
  54. dest=dest+"/class"+classSize+"_"+increm;
  55. ofstream myfile (dest.c_str());
  56. cout<<dest<<endl;
  57. cout<<increm<<endl;
  58. if (!myfile.is_open()) { //check validity of file
  59. cerr << "Error: cannot open the file " << dest << endl;
  60. exit (-1);
  61. }else{
  62. for (int k=0;k<(classSizeTEMP+incremTEMP)/2;k++){
  63. myfile<<"LO";
  64. }
  65. }
  66. myfile.close();
  67. incremTEMP=incremTEMP+(j+1)*incremTEMP;
  68. increm=IntToString(incremTEMP);
  69. }
  70. }
  71. }
  72. }
  73. }
Last edited by Eyies : Mar 30th, 2007 at 12:03 am.
Reply With Quote  
Join Date: Mar 2007
Posts: 3
Reputation: Eyies is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Eyies Eyies is offline Offline
Newbie Poster

Re: C++ File Generator

  #3  
Mar 30th, 2007
lol never mind. I figured it all out, now to implement different flags, sorry for the spam guys. =)
  1. #include <string>
  2. #include <cstring>
  3. #include <iostream>
  4. #include <fstream>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string>
  8.  
  9.  
  10. using namespace std;
  11.  
  12. string IntToString(unsigned int x)
  13. {
  14. static const int Radix = 10;
  15. static const int MaxDigits = 20;
  16.  
  17. if (0 == x) return "0";
  18.  
  19. char Tmp[MaxDigits + 1];
  20. char* Cur = Tmp + MaxDigits;
  21. *Cur = '\0';
  22. while (x && (Cur > Tmp))
  23. {
  24. --Cur;
  25. *Cur = (x%Radix) + '0';
  26. x /= Radix;
  27. }
  28. return Cur;
  29. }
  30.  
  31. int main(int argcount, char* args[]){
  32. int number_of_arguments = argcount-1;
  33.  
  34. for (int i=1;i<=number_of_arguments;i++){
  35. string argTemp = args[i];
  36.  
  37. if (argTemp == "-d"){
  38. cout<<"sup"<<endl;
  39. //begin loop
  40. string classSize,increm;
  41. for (int i=0;i<3;i++){
  42. cout<<i<<endl;
  43. if (i==0){classSize="102";increm="100";}
  44. if (i==1){classSize="1024";increm="1000";}
  45. if (i==2){classSize="10240";increm="10000";}
  46. if (i==3){classSize="102400";increm="100000";}
  47.  
  48.  
  49. int incremTEMP=atoi(increm.c_str());
  50. int classSizeTEMP=atoi(classSize.c_str());
  51. for (int j=0;j<9;j++){
  52. string dest=args[2];
  53. dest=dest+"/class"+classSize+"_"+IntToString(incremTEMP);
  54. ofstream myfile (dest.c_str());
  55. if (!myfile.is_open()) { //check validity of file
  56. cerr << "Error: cannot open the file " << dest << endl;
  57. exit (-1);
  58. }else{
  59. for (int k=0;k<(classSizeTEMP+incremTEMP)/2;k++){
  60. myfile<<"LO";
  61. }
  62. }
  63. myfile.close();
  64. incremTEMP=incremTEMP+atoi(increm.c_str());
  65. }
  66. }
  67. }
  68. }
  69. }
  70.  
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 12:38 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC