Expand|Select|Wrap|Line Numbers
- //#include <windows.h> //needed for opening folders
- #include <stdlib.h> //needed for converting integers to strings
- #include <iostream> //needed for in/out commands ie. cin/cout
- #include <fstream> //needed to open readable/writeable files ie. ifstream/ofstream
- #include <string> //needed to create strings from input ie. string z="0"
- #include <cstdio> //needed to get string length ie number=strlen(someString)
- using namespace std;
- int main()
- {
- // int newfolder(0);
- // newfolder=CreateDirectory("/../cr",NULL);
- //*************************** define variables ****************************************************
- char buf[50];
- char buffer[500];
- int i(15);
- int j(0);
- int n(0);
- int temp;
- int time(3);
- int length;
- int holder;
- int TimeBeforeEOT(10);
- string name="";
- string cr="CR1-";
- string z="0";
- string zz="00";
- string L="L";
- string T=".tra";
- //*************************** open readable file for inputs ***************************************
- ifstream fin("test.txt");
- if(!fin)
- {
- cout << "test.txt cannot be found or opened!!! " << endl;
- char response1;
- cin >> response1;
- return 1;
- }
- //*************************** loop for multiple file outputs **************************************
- for(i;i<=30;i+=5)
- {
- holder=i;
- //*************************** Logic for naming output files ***************************************
- if(time<holder)
- {
- temp=holder - TimeBeforeEOT;
- sprintf(buf,"%d",temp); // sprintf converts integer "temp" to a string "buf"
- if(temp<10)
- {
- string temp2=buf;
- name.erase();
- name.append(cr,0,4);
- name.append(zz,0,2);
- name.append(temp2,0,1);
- name.append(L,0,1);
- name.append(T,0,4);
- }
- if(temp>=10)
- {
- string temp2=buf;
- name.erase();
- name.append(cr,0,4);
- name.append(z,0,2);
- name.append(temp2,0,2);
- name.append(L,0,1);
- name.append(T,0,4);
- }
- if(temp>=100)
- {
- string temp2=buf;
- name.erase();
- name.append(cr,0,4);
- name.append(temp2,0,3);
- name.append(L,0,1);
- name.append(T,0,4);
- }
- //*************************** opening new output CR1 file *****************************************
- ofstream fil((name).c_str()); // .c_str() allows the "string" to be substituted for
- // the name;
- if(!fil)
- {
- cout << name << " cannot be created or opened" << endl;
- char response5;
- cin >> response5;
- return 5;
- }
- cout << name << endl;
- }
- //*************************** begin writing data to new files *************************************
- while(j<=(n+15))
- {
- fin.getline(buffer,500);
- // fil << buffer << endl;
- // fprintf(fil,buffer);
- cout << buffer << endl;
- j++;
- }
- n+=15;
- }
- //*************************** end of program ******************************************************
- char response;
- cin >> response;
- return 0;
- }
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???