473,230 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,230 software developers and data experts.

overloading fstream "names", and writing to many files

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
2 2224
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
9,208 Expert Mod 8TB
Please use code tags on your next post.
Dec 13 '07 #3

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

Similar topics

205
by: Jeremy Siek | last post by:
CALL FOR PAPERS/PARTICIPATION C++, Boost, and the Future of C++ Libraries Workshop at OOPSLA October 24-28, 2004 Vancouver, British Columbia, Canada http://tinyurl.com/4n5pf Submissions
1
by: Jon | last post by:
Hi all! In a insert-trigger I have two joins on the table named inserted. Obviously this construction gives a name collition beetween the two joins (since both joins starts from the same table) ...
22
by: Ian | last post by:
The title says it all. I can see the case where a function is to be called directly from C, the name mangling will stuff this up. But I can't see a reason why a template function can't be...
6
by: Larry Woods | last post by:
I am trying to name my submenus (MainMenu control) and they show up in the menu dropdown...like they are O.K., but when I check my controls the names are still "MenuItemX". OTOH, the top-level...
7
by: jccorreu | last post by:
I've got to read info from multiple files that will be given to me. I know the format and what the data is. The thing is each time we run the program we may be using a differnt number of files,...
1
by: esakal | last post by:
Hello, I created several control. i want that when the user add them to the toolbox they will appear under custom name catagory (for example : "Sakal controls"). what is the attribute that i...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
4
by: Don Lancaster | last post by:
I want to programically generate some JS variable names in a loop. The variables are NOT to be array values. The variable names might be eq0, eq1, eq2.... Inside an ii indexed loop, eval...
2
by: Draggonn | last post by:
Hello all. I'm not sure if this is the right part of the forum to ask, but I couldn't find any better place. I am trying to make a program that will take a file of any extension, and split it into...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.