c++ file i/o

i need to open a file and read it

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 1977 Views - Last Post: 01 September 2008 - 06:30 AM Rate Topic: -----

#1 priyashe  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 28-August 08

c++ file i/o

Post icon  Posted 28 August 2008 - 04:16 AM

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



//

#include "stdafx.h"
#include<stdio.h>
#include<conio.h>
#include<iostream>
#include <fstream>
#include<string>
using namespace std;

void main()

{



// & Attempting to open an text file.

string a[255];


char buffer[225];

char inputFile[]="d://parse//parse.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);

cout << buffer << endl;

}

}


return;

}


parse.cpp

#include "stdafx.h"
#include<stdio.h>
#include<conio.h>
#include<iostream>
#include <fstream>
#include<string>

#include<ctype.h>
using namespace std;

void main()

{



//& ATTEMPTING TO OPEN A TEXT FILE
char buffer[225];

string data,flags[10];


int i;
int a=0,b;

char c;


char comment[10];


//& OPEN A INPUT FILE

char inputFile[]="d://parse//parse.cpp";

ifstream input(inputFile);

ofstream output;

output.open("example.txt");

Is This A Good Question/Topic? 0
  • +

Replies To: c++ file i/o

#2 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • Joined: 25-January 07

Re: c++ file i/o

Posted 28 August 2008 - 04:27 AM

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. :)
Was This Post Helpful? 0
  • +
  • -

#3 priyashe  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 28-August 08

Re: c++ file i/o

Posted 28 August 2008 - 04:50 AM

View PostAmitTheInfinity, on 28 Aug, 2008 - 04:27 AM, said:

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
Was This Post Helpful? 0
  • +
  • -

#4 priyashe  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 28-August 08

Re: c++ file i/o

Posted 28 August 2008 - 04:58 AM

View PostAmitTheInfinity, on 28 Aug, 2008 - 04:27 AM, said:

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
Was This Post Helpful? 0
  • +
  • -

#5 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • Joined: 25-January 07

Re: c++ file i/o

Posted 28 August 2008 - 05:31 AM

#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[]="d://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);
 
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')))
    cout << (print_buffer+2) << endl;

}

}
getch();

return 0;

}




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. :)
Was This Post Helpful? 0
  • +
  • -

#6 priyashe  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 28-August 08

Re: c++ file i/o

Posted 28 August 2008 - 09:01 PM

View PostAmitTheInfinity, on 28 Aug, 2008 - 05:31 AM, said:

#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[]="d://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);
 
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')))
    cout << (print_buffer+2) << endl;

}

}
getch();

return 0;

}




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..
Was This Post Helpful? 0
  • +
  • -

#7 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • Joined: 25-January 07

Re: c++ file i/o

Posted 28 August 2008 - 10:51 PM

yes, of course it is possible. You can use method Write to write data in a file. The link provided will give you more info.

I hope this will help you. :)
Was This Post Helpful? 0
  • +
  • -

#8 priyashe  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 28-August 08

Re: c++ file i/o

Posted 28 August 2008 - 11:05 PM

View PostAmitTheInfinity, on 28 Aug, 2008 - 10:51 PM, said:

yes, of course it is possible. You can use method Write to write data in a file. The link provided will give you more info.

I hope this will help you. :)



ya but i need the same comment lines we got in the output screen to be printed in output text file..is it possible..
Was This Post Helpful? 0
  • +
  • -

#9 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • Joined: 25-January 07

Re: c++ file i/o

Posted 28 August 2008 - 11:21 PM

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.
Was This Post Helpful? 0
  • +
  • -

#10 priyashe  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 28-August 08

Re: c++ file i/o

Posted 28 August 2008 - 11:42 PM

View PostAmitTheInfinity, on 28 Aug, 2008 - 11:21 PM, said:

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 "stdafx.h"
#include <iostream>
#include <fstream>
#include<string>




	using namespace std;
	
	int main()


	{
		char character;

		

		string a[255];
	
		 char buffer[225];
	
		 char *print_buffer;
	

		  string ch;

		  

			
		



		ifstream in_stream;


		ofstream out_stream;


	
		in_stream.open("d://parse//parse.cpp");

		out_stream.open("Copy_of_parsecpp.txt");


		in_stream.get(character);


		while (!in_stream.eof())


		{


				//	cout << character;


		   out_stream .put (character );

			  // 	in_stream.get(character);

			in_stream.getline (buffer,225);

			print_buffer = strstr(buffer,"//"); 


			if(print_buffer!=NULL&&((*print_buffer)==buffer[0]||((*(print_buffer-1))==' ' || (*(print_buffer-1))=='\t')))  
				
			cout << (print_buffer+2) << endl;
			

		}
	




out_stream.close();

in_stream.close();

	return 0;
	}



Was This Post Helpful? 0
  • +
  • -

#11 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • Joined: 25-January 07

Re: c++ file i/o

Posted 28 August 2008 - 11:50 PM

huh.... Let's finish this now.

#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 August 2008 - 11:58 PM

Was This Post Helpful? 0
  • +
  • -

#12 priyashe  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 28-August 08

Re: c++ file i/o

Posted 28 August 2008 - 11:57 PM

View PostAmitTheInfinity, on 28 Aug, 2008 - 11:50 PM, said:

huh.... Let's finish this now.

#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);
 
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)));

}

}
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.



no it's not working...
Was This Post Helpful? 0
  • +
  • -

#13 priyashe  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 28-August 08

Re: c++ file i/o

Posted 29 August 2008 - 12:04 AM

View Postpriyashe, on 28 Aug, 2008 - 11:57 PM, said:

View PostAmitTheInfinity, on 28 Aug, 2008 - 11:50 PM, said:

huh.... Let's finish this now.

#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);
 
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)));

}

}
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.



no it's not working...




I replaced the code as follows and tried but thing is i m getting comment lines in the text file but in the output it's not showing..


#include "stdafx.h"
#include <iostream>
#include <fstream>
#include<string>




	using namespace std;
	
	int main()


	{
		 char character;

		 string a[255];
	
		 char buffer[225];
	
		 char *print_buffer;
	
		 ifstream in_stream;

		 ofstream out_stream;

		 in_stream.open("d://parse//parse.cpp");

		 out_stream.open("Copy_of_parsecpp.txt");

		 in_stream.get(character);


		 while (!in_stream.eof())


		{

		   //cout << character;


		   out_stream .put (character );

		  // 	in_stream.get(character);

			in_stream.getline (buffer,225);

			print_buffer = strstr(buffer,"//"); 


			if(print_buffer!=NULL&&((*print_buffer)==buffer[0]||((*(print_buffer-1))==' ' || (*(print_buffer-1))=='\t')))  
				
			// cout << (print_buffer+2) << endl;
				
			 out_stream.write((print_buffer+2),strlen((print_buffer+2)));


			

		}
	

		  out_stream.close();

		  in_stream.close();

	   	  return 0;
	}



Was This Post Helpful? 0
  • +
  • -

#14 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • Joined: 25-January 07

Re: c++ file i/o

Posted 29 August 2008 - 12:05 AM

Damn... :crazy: you commented that cout, how it will print it on console then?

replace that if like this now.

 if(print_buffer!=NULL&&((*print_buffer)==buffer[0]||((*(print_buffer-1))==' ' || (*(print_buffer-1))=='\t')))  
{           
           cout << (print_buffer+2) << endl;
           out_stream.write((print_buffer+2),strlen((print_buffer+2)));
           out_stream.put('\n');
}



and I think there is no other place than this where you need methods get or put, so remove them from every other place.

Attached File(s)


This post has been edited by AmitTheInfinity: 29 August 2008 - 12:09 AM

Was This Post Helpful? 0
  • +
  • -

#15 priyashe  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 28-August 08

Re: c++ file i/o

Posted 29 August 2008 - 12:24 AM

View PostAmitTheInfinity, on 29 Aug, 2008 - 12:05 AM, said:

Damn... :crazy: you commented that cout, how it will print it on console then?

replace that if like this now.

 if(print_buffer!=NULL&&((*print_buffer)==buffer[0]||((*(print_buffer-1))==' ' || (*(print_buffer-1))=='\t')))  
{           
           cout << (print_buffer+2) << endl;
           out_stream.write((print_buffer+2),strlen((print_buffer+2)));
           out_stream.put('\n');
}



and I think there is no other place than this where you need methods get or put, so remove them from every other place.



ya why i commented dat line is while compillation i'm geting an error saying .exe has stopped working while including cout and didn't get output also..
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2