c++ file i/o

i need to open a file and read it

  • (2 Pages)
  • +
  • 1
  • 2

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

#16 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 109
  • View blog
  • Posts: 1,530
  • Joined: 25-January 07

Re: c++ file i/o

Posted 29 August 2008 - 12:35 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[]="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')))  
{           
           cout << (print_buffer+2) << endl;
           out_stream.write((print_buffer+2),strlen((print_buffer+2)));
           out_stream.put('\n');
}


}

}
getch();

return 0;

}




This is what I tried and it worked absolutely fine for me. In my previous post, I have attached the output I got too!

Please check if you are doing anything different. or whether it is due to the IDE.
Was This Post Helpful? 0
  • +
  • -

#17 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 - 01:45 AM

View PostAmitTheInfinity, on 29 Aug, 2008 - 12:35 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[]="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')))  
{           
           cout << (print_buffer+2) << endl;
           out_stream.write((print_buffer+2),strlen((print_buffer+2)));
           out_stream.put('\n');
}


}

}
getch();

return 0;

}




This is what I tried and it worked absolutely fine for me. In my previous post, I have attached the output I got too!

Please check if you are doing anything different. or whether it is due to the IDE.



ya it works fine ...thanx a lot
Was This Post Helpful? 0
  • +
  • -

#18 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 - 02:22 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...



can u plz explain me the operation of if loop...
Was This Post Helpful? 0
  • +
  • -

#19 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 109
  • View blog
  • Posts: 1,530
  • Joined: 25-January 07

Re: c++ file i/o

Posted 29 August 2008 - 02:34 AM

if(print_buffer!=NULL&&((*print_buffer)==buffer[0]||((*(print_buffer-1))==' ' || (*(print_buffer-1))=='\t'))) 



here, first condition is verifying whether pointer is NULL, this pointer will be NULL in case if the line does not have string "//" in it [result of function strstr].

if it is NULL then we don't need to print anything. if it is not NULL then may be we have a comment on that line.
Now it can be a comment starting at start of line or starting in between. if it starts at index 0 then it is surely a comment
but if it is in between then we have to have some checks.

like printf("//Is this a comment?"); we can not consider this line as comment but our program might. so we added last two conditions to verify this. Though there are few cases where we miss out the comments from the program.

see here printf("//Is this a comment?"); //That was not but this is and program won't catch it!!! yes, our program misses this comment as the comment is at the second occurrence of "//" and we check only first one. So there are chances of improvement too.

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

#20 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 31 August 2008 - 11:41 PM

Is it possible to read data's send from the webserver using port's to be stored in a text file
Was This Post Helpful? 0
  • +
  • -

#21 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 01 September 2008 - 12:22 AM

I had created a project in vb in which using socket connection we'll connect with the web server and send data's to the server by giving ip address and now i have to store it in text file is it possible/,,,
Was This Post Helpful? 0
  • +
  • -

#22 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 109
  • View blog
  • Posts: 1,530
  • Joined: 25-January 07

Re: c++ file i/o

Posted 01 September 2008 - 12:30 AM

View Postpriyashe, on 1 Sep, 2008 - 12:52 PM, said:

I had created a project in vb in which using socket connection we'll connect with the web server and send data's to the server by giving ip address and now i have to store it in text file is it possible/,,,



Seems that your question is VB related and not connected to this topic. Please post is as a new topic in VB forum in that case. It will make more sense to people here.
Was This Post Helpful? 0
  • +
  • -

#23 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 01 September 2008 - 12:40 AM

thanx for u'r reply but is it possible in c++
Was This Post Helpful? 0
  • +
  • -

#24 gabehabe  Icon User is online

  • GabehabeSwamp
  • member icon




Reputation: 1346
  • View blog
  • Posts: 10,918
  • Joined: 06-February 08

Re: c++ file i/o

Posted 01 September 2008 - 06:30 AM

Yeah~ Look up ifstream.

Here's a quick sample:
#include <fstream>
#include <iostream>

using namespace std;

int main ()
{
    ifstream infile;
    infile.open ("C:\\dev-cpp\\Untitled-2.cpp");
    if (!infile.is_open()) // check if the file is open
    {
        cout << "Error~ file could not be opened. Aborting";
        cin.get();
        return EXIT_FAILURE; // failed execution
    } // otherwise, we can now assume that the file is open from here on in:
    char buffer; // a character buffer, to read it character by character
    while (!infile.eof()) // while the end of the file has not been reached
    {
        infile.get(buffer);
        cout << buffer;
    }
    cin.get();
    return EXIT_SUCCESS;
}

NOTE: I just typed this in less than a minute in only the fast reply box~ there may be a few errors/typos in there.

Hope this helps :)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2