Newbie need help with C++ fstream and char

Hello,

I am relatively new to C++ and really need help with learning how to read character from a file and depending on the character will out it into the appropriate file it will sort work like a c++ compiler does a compile will compile code that doesn't have comment marker like the // or /// or /**/ marker so i want to simulate that by outputting code that has the comment marker into a comment.txt file and any other source code into a Code.txt file.

Below is the source code of what i have, so the code compile but it get caught in the while loop and doesn't exit. So maybe there is someone out who can help with this i would surely appreciate it.

P.S. Also not only does it get stuck in the while loop it outputs using cout zero instead of the char value.

// Summary

/*******************************************************
* Abstract: Program will read an external source *
* file and create two output files, *
* one with non comments (Code.txt) *
* and the other with only the *
* comments (Comment.txt). *
* *
********************************************************/

#include <iostream> //allows us to perform input and output
#include <stdio.h>
#include <iomanip> //defines a set of manipulators that can affect to both input and output streams of the iostream hierarchy
#include <fstream> //allows us to open file from memory
#include <string> //allows us to use string data type

using namespace std;

int main()
{
char ch, prev;

cout << "Welcome to Program #5! We will attempt to read the source code of another " << endl;
cout << "program, and create two output files, one with only the comments, and one " << endl;
cout << "with only the source code." << endl;
getchar(); // pause

cout << endl;

ifstream inFile;
ofstream outFile;

// open files
inFile.open ( "D:\\Documents and Settings\\Willie Johnson Jr\\My Documents\\AAA_Consulting Projects\\AAA_Kasamba\\Client - want2climb\\InSourceCode.txt" );
outFile.open ( "D:\\Documents and Settings\\Willie Johnson Jr\\My Documents\\AAA_Consulting Projects\\AAA_Kasamba\\Client - want2climb\\Code.txt" );
outFile.open ( "D:\\Documents and Settings\\Willie Johnson Jr\\My Documents\\AAA_Consulting Projects\\AAA_Kasamba\\Client - want2climb\\Comment.txt" );

// process file
inFile.get ( ch );

prev = ch;

while ( !inFile.eof ( ) )
{
inFile.get ( ch );
while (prev != '*' && ch != '/')
{
prev = ch;
cout << prev ;
cout << ch << "cout 1";
outFile << ch;
}

if (ch == '/')
{
prev = ch;
inFile.get ( ch );
if (ch == '*')

outFile << ch;
cout << ch << "cout 2";
}

else
{
outFile << ' ';
cout << ' ' << "cout 3";
}

inFile.get ( ch );
}

//close files
inFile.close ( );
outFile.close ( );

cin.get( );
cin.get( );
return 0;

}
[3253 byte] By [lew26] at [2007-11-20 1:38:55]
# 1 Re: Newbie need help with C++ fstream and char
Please edit your post and add code tags - your code is very hard to read without indentations.

Besides that: Have you tried debugging through your code in single-step mode to see what exactly goes wrong, and why your loop doesn't terminate?
gstercken at 2007-11-10 23:17:50 >
# 2 Re: Newbie need help with C++ fstream and char
Thank you for responding :)

I have attach the source code below also i have so far solved why it wasn't passing the coditioin now i need help with the seekg command i found out that inmy code that on the section,

inFile.seekg(0, ios_base::beg);

will cause it to return to the beggining of the file.

What i want it to do is return to the beggining of that line that it is currently on so i can call the getline()

Any Suggestions??
lew26 at 2007-11-10 23:18:46 >
# 3 Re: Newbie need help with C++ fstream and char
You realize you're calling open() twice on the outFile object? One object used like this can't represent two output streams.

also, think about what happens if you pass code like this into your program:

#include <iostream>
using namespace std;
int main()
{
cout << "In C++, comments /* can be like this */ or // like this" << endl;
return 0;
}
SLCDJM at 2007-11-10 23:19:45 >
# 4 Re: Newbie need help with C++ fstream and char
First thing reading One by One Character from File is not a good Idea.it's better if you read Complete File at Once .and Then Perform your calculation on the buffer which you make by reading the File

#include <iostream>
#include <fstream>
using namespace std;

ifstream::pos_type size;
char * memblock;

int main () {
ifstream file ("example.txt", ios::in|ios::binary|ios::ate);
if (file.is_open())
{
size = file.tellg();
memblock = new char [size];
file.seekg (0, ios::beg);
file.read (memblock, size); //now you have your all File content in your memblock
file.close();

cout << "the complete file content is in memory";


}
else cout << "Unable to open file";
//here open your Others File for output Perform all Calculation an Checking on your memblock and write Appropriate Character to your Output File According to your requirement.

delete[] memblock; // don't forget to release your allocated memory by delete
return 0;
}

Thanx
humptydumpty at 2007-11-10 23:20:55 >
# 5 Re: Newbie need help with C++ fstream and char
I was thinking about doing that just putting everything into an array, just have to figure out formula to read the data in the memory block...humm
lew26 at 2007-11-10 23:21:50 >
# 6 Re: Newbie need help with C++ fstream and char
So now i think you got the answer of your question.or still you are looking for something else

Thanx
humptydumpty at 2007-11-10 23:22:49 >
# 7 Re: Newbie need help with C++ fstream and char
well you gave me another direction to try, i was about to give up, i like c++ it fast and give you more control and been searching all over the net, the original problem i had i figure out just i got stuck on figuring out a algorithm that would read the comment and output them to the appropriate files.

I have attached the file below
lew26 at 2007-11-10 23:23:53 >
# 8 Re: Newbie need help with C++ fstream and char
I got it to output the first comment at the beggining of the file /*******/ except the last character '/'

but had problem get it to add the other comment '//' that start with these two characters. To the appropriate file maybe i been on it to long or i don't most of my code i had to do searches in order to get most of it to work so now i got the getfile, and seekg command some what down pack just gotta get the algorithm right.

Any help is surely appreciate becuase i'm frustrated and tired and wanted to get it before i went to sleep.
lew26 at 2007-11-10 23:24:50 >
# 9 Re: Newbie need help with C++ fstream and char
well you gave me another direction to try, i was about to give up, i like c++ it fast and give you more control and been searching all over the net, the original problem i had i figure out just i got stuck on figuring out a algorithm that would read the comment and output them to the appropriate files.

I have attached the file below
Above Example is Writen in C++ Only.just carefully have a look on the example.And logic you have to implement by yourSelf Only. Anyone can help you to resolve your problem. but if anybody Else Solved the Problem .Don't think you Will learn Anything from this.

Hints:-
Avove Code is in C++ .so you can retrive Characters from the memblock and can perform calculation on that.

Second you can use very helpfull STL class which is string

Thanx
humptydumpty at 2007-11-10 23:25:54 >
# 10 Re: Newbie need help with C++ fstream and char
You are definately right, for now i off to bed pretty sure after a good night rest i'll be fresh for tommorrow and ready to go at it again.

I sure appreciate everything and you have a wonderful night. :)
lew26 at 2007-11-10 23:26:55 >
# 11 Re: Newbie need help with C++ fstream and char
thanx Dude :p
.but i am in india and right now here time is 9 A.m of the morning.
so From my Side good time .may be in the way it will Convert into Good night for you
humptydumpty at 2007-11-10 23:27:59 >