473,394 Members | 1,946 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,394 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 2232
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.