By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,648 Members | 2,098 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,648 IT Pros & Developers. It's quick & easy.

overloading fstream "names", and writing to many files

P: 2
Sorry i know this is rather large to be posting, but in order to understand the question you have to see all the code

Expand|Select|Wrap|Line Numbers
  1. //#include <windows.h>        //needed for opening folders
  2. #include <stdlib.h>            //needed for converting integers to strings
  3. #include <iostream>            //needed for in/out commands ie. cin/cout
  4. #include <fstream>            //needed to open readable/writeable files ie. ifstream/ofstream
  5. #include <string>            //needed to create strings from input ie. string z="0"
  6. #include <cstdio>            //needed to get string length ie number=strlen(someString)            
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11.  
  12. //    int newfolder(0);
  13. //    newfolder=CreateDirectory("/../cr",NULL);
  14. //*************************** define variables ****************************************************
  15.  
  16.     char buf[50];
  17.     char buffer[500];
  18.  
  19.     int i(15);
  20.     int j(0);
  21.     int n(0);
  22.     int temp;
  23.     int time(3);
  24.     int length;
  25.     int holder;
  26.     int TimeBeforeEOT(10);
  27.  
  28.     string name="";
  29.     string cr="CR1-";
  30.     string z="0";
  31.     string zz="00";
  32.     string L="L";
  33.     string T=".tra";
  34.  
  35. //*************************** open readable file for inputs ***************************************
  36.  
  37.     ifstream fin("test.txt");
  38.  
  39.     if(!fin)
  40.     {
  41.         cout << "test.txt cannot be found or opened!!! " << endl;
  42.         char response1;
  43.         cin >> response1;
  44.         return 1;
  45.     }
  46.  
  47. //*************************** loop for multiple file outputs **************************************
  48.  
  49.     for(i;i<=30;i+=5)
  50.     {
  51.         holder=i;
  52.  
  53. //*************************** Logic for naming output files ***************************************
  54.  
  55.         if(time<holder)
  56.         {
  57.             temp=holder - TimeBeforeEOT;
  58.             sprintf(buf,"%d",temp);        // sprintf converts integer "temp" to a string "buf"
  59.             if(temp<10)
  60.             {
  61.                 string temp2=buf;
  62.                 name.erase();
  63.                 name.append(cr,0,4);
  64.                 name.append(zz,0,2);
  65.                 name.append(temp2,0,1);
  66.                 name.append(L,0,1);
  67.                 name.append(T,0,4);
  68.             }
  69.             if(temp>=10)
  70.             {
  71.                 string temp2=buf;
  72.                 name.erase();
  73.                 name.append(cr,0,4);
  74.                 name.append(z,0,2);
  75.                 name.append(temp2,0,2);
  76.                 name.append(L,0,1);
  77.                 name.append(T,0,4);
  78.             }
  79.             if(temp>=100)
  80.             {
  81.                 string temp2=buf;
  82.                 name.erase();
  83.                 name.append(cr,0,4);
  84.                 name.append(temp2,0,3);
  85.                 name.append(L,0,1);
  86.                 name.append(T,0,4);
  87.             }
  88.  
  89. //*************************** opening new output CR1 file *****************************************
  90.  
  91.             ofstream fil((name).c_str());    // .c_str() allows the "string" to be substituted for 
  92.                                             // the name; 
  93.             if(!fil)
  94.             {
  95.                 cout << name << " cannot be created or opened" << endl;
  96.                 char response5;
  97.                 cin >> response5;
  98.                 return 5;
  99.             }
  100.  
  101.             cout <<  name << endl;
  102.  
  103.         }
  104.  
  105. //*************************** begin writing data to new files *************************************
  106.  
  107.         while(j<=(n+15))
  108.         {
  109.             fin.getline(buffer,500);
  110. //            fil << buffer << endl;
  111. //            fprintf(fil,buffer);
  112.             cout << buffer << endl;
  113.             j++;
  114.         }
  115.  
  116.         n+=15;
  117.  
  118.     }
  119.  
  120. //*************************** end of program ******************************************************
  121.  
  122.     char response;
  123.     cin >> response;
  124.     return 0;
  125. }
  126.  

First off i am not a student, i wrote all of this code myself so i have worked on this for quite a while. I only started programming 10-11 weeks ago and i need help.
this code opens an input file, creates a file name <string> then opens a new output file with the created name, inputs a preset number of data into the first output file, creates a new file name using strings, opens another output file with the newest string name, reads data from original input file, writes to newest output file and the cycle goes on and on til "eof". the ouput files are created but no data is written; however, the buffer is reading the data because if you use the "cout" at the end of the code all the data is displayed. Why can i overload the "ofstream fil" to create files but not to print??? and how do i print to the files???
Dec 12 '07 #1
Share this Question
Share on Google+
2 Replies


P: 2
Expand|Select|Wrap|Line Numbers
  1. //*************************** opening new output CR1 file *****************************************
  2.  
  3.             ofstream fil((name).c_str());    // .c_str() allows the "string" to be substituted for 
  4.                                             // the name; 
  5.             if(!fil)
  6.             {
  7.                 cout << name << " cannot be created or opened" << endl;
  8.                 char response5;
  9.                 cin >> response5;
  10.                 return 5;
  11.             }
  12.  
  13.             cout <<  name << endl;
  14.  
  15. //*************************** begin writing data to new files *************************************
  16.  
  17.             while(j<=(n+15))
  18.             {
  19.                 fin.getline(buffer,500);
  20.                 fil << buffer << endl;
  21.     //            fprintf(fil,buffer);
  22.                 cout << buffer << endl;
  23.                 j++;
  24.             }
  25.  
  26.             n+=15;
  27.  
  28.         }
  29.  
  30.  
  31.  
  32.     }
  33.  
  34. //*************************** end of program ******************************************************
  35.  
  36.     char response;
  37.     cin >> response;
  38.     return 0;
  39. }
  40.  


I found my error, just in case anyone else has to do something similar or if someone was lookin at this to give me a response. All the code is right, i just ended the [ if(time<holder) ] loop prematurely. printing loop was correct but needed to be moved inside of the [if(time<holder)] loop. What was happening was the code was opening the first file, then the next file and the next file until the [if(time<holder)] loop was satisfied then it tried to print to the last file only. I am still unsure why the last file would not print though????
Dec 13 '07 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
Please use code tags on your next post.
Dec 13 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.