Join 132,396 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,234 people online right now. Registration is fast and FREE... Join Now!
as i'm new to c++ i have to open a file which may be (cpp or txt file) and i have to read the file line by line and the comment lines indicated by // should be printed out and the code which i have written is as follows and also i have attached the cpp file from which comment lines should be read.plz help me
So you are trying to print the comments from the program, right? or print program neglecting comments?? Please explain your expected output again.
And the program you wrote there is printing everything you get from input file. You need to check that whether the line has '/' as it's first two characters [excluding initial spaces and tabs if any] before printing the line.
So you are trying to print the comments from the program, right? or print program neglecting comments?? Please explain your expected output again.
And the program you wrote there is printing everything you get from input file. You need to check that whether the line has '/' as it's first two characters [excluding initial spaces and tabs if any] before printing the line.
I hope this will help you.
thanks for u'r reply but i need to print only the comment lines excluding program codes...the program i hav written is giving all the lines as output...can u plz explain me with codes
So you are trying to print the comments from the program, right? or print program neglecting comments?? Please explain your expected output again.
And the program you wrote there is printing everything you get from input file. You need to check that whether the line has '/' as it's first two characters [excluding initial spaces and tabs if any] before printing the line.
I hope this will help you.
can u plz tell me how to give the condition as dis is my first assignment and already i have tried dis for a week s
I have added a bit complex condition there to make sure that it prints the comment even if it starts in the middle of the line and it avoids the lines having // but are not the comments. If you don't want so much things to be implemented, then you can remove that if conditions and keep just the first one of them.
I have added a bit complex condition there to make sure that it prints the comment even if it starts in the middle of the line and it avoids the lines having // but are not the comments. If you don't want so much things to be implemented, then you can remove that if conditions and keep just the first one of them.
I hope this will help you.
Thank u very much for u'r help as the code works and this is wat i expected but is it possible to write these output coment line in to the output text file which we opened as example.txt..
yes, all you need to do is write the buffer to your file in the if condition. So may be you will put something like output.write(print_buff,strlen(print_buff)) instead of cout there. Try it and come back if it doesn't work.
yes, all you need to do is write the buffer to your file in the if condition. So may be you will put something like output.write(print_buff,strlen(print_buff)) instead of cout there. Try it and come back if it doesn't work.
I tried by writing the code as follows but in the text file i got only (//////)dis..
#include<stdio.h> #include<conio.h> #include<iostream> #include <fstream> #include<string> using namespace std;
int main()
{
// & Attempting to open an text file.
string a[255];
char buffer[225]; char *print_buffer;
char inputFile[]="c://dev-cpp//untitled2.cpp";
ifstream input(inputFile);
ofstream output;
output.open("example.txt");
if (!input)
cout << "Error in file name";
else
{
cout << "File: " << inputFile << endl;
while (!input.eof())
{
input.getline(buffer,255); // Get line from input file
print_buffer = strstr(buffer,"//"); // This gives you the pointer pointing to the location where your // is in the line // else it will return NULL.
if(print_buffer!=NULL&&((*print_buffer)==buffer[0]||(*(print_buffer-1))==' ' || (*(print_buffer-1))=='\t')) { output.write((print_buffer+2),strlen((print_buffer+2))); // print the comments in a file. output.put('\n'); //every comment should be on a new line. } }
} getch();
return 0;
}
I JUST replaced cout statement with write method. Have a look at the code and tell me if this satisfies your requirements.
--EDIT-- Added comments to code and a newline after writing every comment into the output file.
This post has been edited by AmitTheInfinity: 28 Aug, 2008 - 10:58 PM