The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > General Programming > C / C++


Welcome to the The ProgrammersTalk Community forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.
Tags: , , , ,

Reply
 
LinkBack Thread Tools    Display Modes   
  #1 (permalink)  
Old 08-03-2007, 12:19 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,109
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Icon13 How to read file in C++?

here's the code that I have for now:

PHP Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ifstream infileinfile2;
infile.open("pdf1.txt");
infile2.open("pdf2.txt");
if (!
infile) {
cout << "Error in reading pdf1.txt" << endl;
exit(
1);
}
if (!
infile2) {
cout << "Error in reading pdf2.txt" << endl;
exit(
1);
}
string r infile.read();
string r2 infile2.read();
while (
infile.read() != null) {
if (
!= r2) {
cout << "Different at " << << endl;
}
infile.read();
}
return 
0;

I want it to read it line by line, but also have the ability to tokenize (in java lol..) something, please help me where should I look for this information and suggestion to what do I need to do to read a file and compare it to other file.

I want to compare one file with the other to see if they're the same because there's going to be a server backup at my workplace, so we have to make sure if we still have the same data from the previous server.

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #2 (permalink)  
Old 08-03-2007, 01:00 PM
rpgfan3233 rpgfan3233 is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jul 2007
Posts: 118
iTrader: (0)
rpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura about
I'm not sure about tokenization (not sure what that actually is), but one thing that your code is doing is checking whether a file is equal to false or 0 after you open it. The method you should be using is if (!infile.is_open()), for example. Another, perhaps better, way you could check is to check the failbit using if (infile.fail()) since the failbit is set if the requested file is not opened or the variable already represents an open file (calling open() on the same variable without closing it properly makes the call to open() fail, setting the failbit).

As for file comparison, perhaps you would prefer binary read/write instead of text. That way, you could compare the bytes rather than just text, where text may not actually be text. Also, if you wanted to, you could shorten your code a bit:
Code:
//just remove the "| ifstream::binary" if you want to use text
ifstream infile("pdf1.txt", ifstream::in | ifstream::binary);
ifstream infile2("pdf2.txt", ifstream::in | ifstream::binary);
You could simply have them on the same line like you do in your code, but I split them up for slightly better readability.

__________________
"C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off."
-- Bjarne Stroustrup, creator of what is now known as C++
For more quotes by Bjarne Stroustrup, check out http://www.research.att.com/~bs/bs_faq.html#really-say-that.
Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to rpgfan3233 For This Useful Post:
HelloWorld (08-03-2007)
  #3 (permalink)  
Old 08-03-2007, 04:12 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,109
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Is there a way for me to read it line by line in C++?
I'm trying to make something like:

Code:
// Sorry, this is Java code in C++ forum
String line = br.readLine();

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #4 (permalink)  
Old 08-03-2007, 04:36 PM
Lee's Avatar
Lee Lee is offline
PT Staff*
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: Blackpool, UK
Posts: 615
iTrader: (0)
Lee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enough
Just found this bit of code:
Quote:
string str;
getline(infile,str); // Get the frist line from the file, if any.

while (infile) { // Continue if the line was sucessfully read.

// do somethin with str here

getline(infile,str); // Try to get another line.
}
I edited it to put 1 example of your code in, i think if you was to test this you would find its what you want, at least i hope.

Hope it helps somewhat,
Lee.
Reply With Quote
The Following User Says Thank You to Lee For This Useful Post:
HelloWorld (08-03-2007)
  #5 (permalink)  
Old 08-03-2007, 05:33 PM
Bench Bench is offline
Full Programmer
Join Date: Jul 2007
Location: UK
Posts: 109
iTrader: (0)
Bench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished road
When reading lines from files in a loop, the best place to put the getline is inside the while condition itself - this is the simplest way to make sure the file doesn't read over the end, and that the final iteration of the loop doesn't cause undefined behaviour
Code:
string str;
while( getline( infile, str ) )
{
    // ... str was read from infile OK
}

Sometimes its necessary to put the getline elsewhere, maybe if the sequence of file reads is a more complicated process, you want to put checks at every point along the way make sure each step is successful
Code:
string str;
int i;
while( true )
{
    infile >> i;
    if ( !infile )
        break;

    getline( infile, str );
    if ( ! infile )
        break;
    // read was successful - can do something with str and 'i'
}

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by Bench : 08-03-2007 at 05:44 PM.
Reply With Quote
Reply


Thread Tools
Display Modes

   Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 04:50 PM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50