Welcome to Dream.In.Code
Getting C++ Help is Easy!

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!




c++ file i/o

2 Pages V  1 2 >  
Reply to this topicStart new topic

c++ file i/o, i need to open a file and read it

priyashe
post 28 Aug, 2008 - 03:16 AM
Post #1


D.I.C Head

**
Joined: 28 Aug, 2008
Posts: 71

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




User is offlineProfile CardPM

Go to the top of the page

AmitTheInfinity
post 28 Aug, 2008 - 03:27 AM
Post #2


C Surfing ∞

Group Icon
Joined: 25 Jan, 2007
Posts: 1,015



Thanked 34 times

Dream Kudos: 125
My Contributions


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. smile.gif
User is offlineProfile CardPM

Go to the top of the page

priyashe
post 28 Aug, 2008 - 03:50 AM
Post #3


D.I.C Head

**
Joined: 28 Aug, 2008
Posts: 71

QUOTE(AmitTheInfinity @ 28 Aug, 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. smile.gif




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
User is offlineProfile CardPM

Go to the top of the page

priyashe
post 28 Aug, 2008 - 03:58 AM
Post #4


D.I.C Head

**
Joined: 28 Aug, 2008
Posts: 71

QUOTE(AmitTheInfinity @ 28 Aug, 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. smile.gif



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
User is offlineProfile CardPM

Go to the top of the page

AmitTheInfinity
post 28 Aug, 2008 - 04:31 AM
Post #5


C Surfing ∞

Group Icon
Joined: 25 Jan, 2007
Posts: 1,015



Thanked 34 times

Dream Kudos: 125
My Contributions


cpp

#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. smile.gif
User is offlineProfile CardPM

Go to the top of the page

priyashe
post 28 Aug, 2008 - 08:01 PM
Post #6


D.I.C Head

**
Joined: 28 Aug, 2008
Posts: 71

QUOTE(AmitTheInfinity @ 28 Aug, 2008 - 05:31 AM) *

cpp

#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. smile.gif




Thank u very much for u'r help as the code works icon_up.gif 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..
User is offlineProfile CardPM

Go to the top of the page

AmitTheInfinity
post 28 Aug, 2008 - 09:51 PM
Post #7


C Surfing ∞

Group Icon
Joined: 25 Jan, 2007
Posts: 1,015



Thanked 34 times

Dream Kudos: 125
My Contributions


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. smile.gif
User is offlineProfile CardPM

Go to the top of the page

priyashe
post 28 Aug, 2008 - 10:05 PM
Post #8


D.I.C Head

**
Joined: 28 Aug, 2008
Posts: 71

QUOTE(AmitTheInfinity @ 28 Aug, 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. smile.gif



ya but i need the same comment lines we got in the output screen to be printed in output text file..is it possible..
User is offlineProfile CardPM

Go to the top of the page

AmitTheInfinity
post 28 Aug, 2008 - 10:21 PM
Post #9


C Surfing ∞

Group Icon
Joined: 25 Jan, 2007
Posts: 1,015



Thanked 34 times

Dream Kudos: 125
My Contributions


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.
User is offlineProfile CardPM

Go to the top of the page

priyashe
post 28 Aug, 2008 - 10:42 PM
Post #10


D.I.C Head

**
Joined: 28 Aug, 2008
Posts: 71

QUOTE(AmitTheInfinity @ 28 Aug, 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.



I tried by writing the code as follows but in the text file i got only (//////)dis..

CODE


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



User is offlineProfile CardPM

Go to the top of the page

AmitTheInfinity
post 28 Aug, 2008 - 10:50 PM
Post #11


C Surfing ∞

Group Icon
Joined: 25 Jan, 2007
Posts: 1,015



Thanked 34 times

Dream Kudos: 125
My Contributions


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

cpp

#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
User is offlineProfile CardPM

Go to the top of the page

priyashe
post 28 Aug, 2008 - 10:57 PM
Post #12


D.I.C Head

**
Joined: 28 Aug, 2008
Posts: 71

QUOTE(AmitTheInfinity @ 28 Aug, 2008 - 11:50 PM) *

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

cpp

#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...
User is offlineProfile CardPM

Go to the top of the page

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 11/22/08 07:22AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month