Basic file read, file write program
Hey all,
I'm trying to write a program which takes in a file called original.dat, which is a source code file, and separates the comments (denoted by // and /**/) from the regular code. The program writes the comments to a file called comment.dat and the code to a file called source.dat.
Just a little background, I had a God-awful programming I teacher before, and my programming II teacher expects us to be intimately familiar with all aspects of coding.
I know I have some unused identifiers in there, they are just remnants of other attempts I made.
I am using the Bloodshed Dev C++ compiler, and I was trying to test this program with some .dat files written in Dev, and all I get out is a bunch of system trash, with lots of files paths included.
I could use some advice; to my knowledge, this works as I expect. It stores the whole input file into a string, and then goes character by character through the string and checks for '/' and then if it encounters that, checks for another '/' or a '*' to denote comment tags, and then writes them out to the comment.dat file. It also echoes the data written to the screen. But I was unable to test this to my satisfaction.
Code:
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <conio.h>
#include <string>
#define ENDFILE "CTRL-Z"
#define infile "original.dat"
#define outfile1 "source.dat"
#define outfile2 "comment.dat"
using namespace std;
int outcomments( ifstream &xx);
int main()
{
int line1;
ifstream originalin;
ofstream sourceout;
ofstream commentout;
originalin.open(infile);
sourceout.open(outfile1);
commentout.open(outfile2);
line1=0;
line1 = outcomments(originalin);
system("pause");
return 0;
}
int outcomments(ifstream &xx)
{ //begin function
ifstream originalin;
ofstream commentout;
ofstream sourceout;
originalin.open(infile);
sourceout.open(outfile1);
commentout.open(outfile2);
string file;
string comment_slash;
string comment_slash_star;
char ch;
int i,line1;
line1=0;
i=0;
while(!xx.eof())
{ //begin outer while
originalin>>file; //stores entire file in string
if(file[i]=='/')
{ //begin outer if
i++;
if(file[i]=='/')
{ //begin inner if
commentout<<'/';
while(file[i]!='\n')
{ //begin inner while
commentout<<file[i];
cout<<'/'<<file[i]; //echo
i++;
} //end inner while
commentout<<endl;
} //end inner if
else
{ //begin inner else
if(file[i]=='*')
{ //begin inner inner if
commentout<<'/';
while(file[i]!='/')
{ //begin inner while
commentout<<file[i];
cout<<file[i]; //echo
i++;
} //end outer while
} //end inner inner if
} //end inner else
} //end outer if
else
{
sourceout<<file[i]; //if characters are not // or /* then the code
} //is written to the source file, without comments
} //end outer while
return 0;
} //end function