Thread: urls and filehandles

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    86

    urls and filehandles

    firstly, filehandles

    When a file contains text, for example, "hello, person"

    an ofstream being used like this: ofstream>>variable would only read "hello,"

    my thinking is that a loop like this:

    Code:
    while(variable!=NULL){
    
            ofstream>>variable;
    
    }//end while
    woudl read in the entire file

    could someone cofirm that, or give me a different way to read in the whole file?

    second, how do I get the html code from a webpage?

    thanx

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    The end of file is indicated by an EOF char, not NULL, which is a gauranteed location in memory, or a null char, which isn't the same as NULL. When the input stream finds the EOF char it will cause the stream to fail, and set the EOF bit. Therefore, the following syntax is commonly use to read in file data.
    Code:
    while(inputStreamName >> variableName)
    {
       //do something with variableName other than overwriting it each time through loop
    }
     
    //determine why stream failed
    if(inputStreamName.eof())
      //eof() evaluates to true if EOF bit is flipped
      cout << "file read in successfully" << endl;
    else
      cout << "error reading in file" << endl;

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    86
    so...how do I concatenate the input?

    would this work?:

    Code:
    while(! inputStreamName.eof()){
    
              inputStreamName>>temp;
    
              variable+=temp;
    
    }//end while

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    Quote Originally Posted by pktcperlc++java
    so...how do I concatenate the input?

    would this work?:

    Code:
    while(! inputStreamName.eof()){
    
              inputStreamName>>temp;
    
              variable+=temp;
    
    }//end while
    That really depends on what you are reading from the file. Concatenating strings like that requires you to use a string object supporting the += operator. If its all numerical data, then += will give you the sum, so on and so forth.

    There is no easy "do it this way all the time answer". Just make sure to save off what was read during each iteration so its not overwritten in the next iteration.

    PK

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    86
    I should have been clearer

    I meant to ask if the evaluation in the while loop was valid

  6. #6
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Yes, it is valid. No, it will not work as you expect. Read the FAQ and do as elad suggested or your loop will go one too many times.

    In addition, by using >> you are ignoring whitespace, and so when you add on the data to the variable then the whitespace is missing. There are many solutions to this, including reading the input without skipping whitespace, adding the whitespace back in if you know what it is, reading in the data from the file a character at a time, and using getline to read a line at a time.

  7. #7
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Do you mean displaying file contents? If you do you can do this:

    Code:
    ofstream fin("FILENAME");
    char ch;
    while(fin.get(ch))
    {
         cout<<ch;
    }
    fin.close();
    i think that's right...

  8. #8
    Registered User
    Join Date
    Nov 2004
    Posts
    86
    so, modified, would this work?

    Code:
    ofstream fin("FILENAME");
    char ch;
    while(fin.get(ch)){
    storageString+=ch;
    }//end while
    fin.close();

  9. #9
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    are you trying to get the contents of the file written into a string? I had the same question not too long ago and nobody gave me a simple solution so i used this and it worked:

    Code:
    ...
    ifstream fin("filename");
    char ch;
    char newstring[100];
    int x;
    x=0;
    while(fin.get(ch))
    {
         cout<<ch;
         newstring[x]=ch;
         x++;
    }
    ...
    i think that's right (im not at home right now so i cant check) but it worked for me.

  10. #10
    Registered User
    Join Date
    Nov 2004
    Posts
    86
    would this work? maybe?

    Code:
    #include <fstream>
    #include <string>
    #include <cstring>
    
    using namespace std;
    using std::string;
    
    string readFile(string filename){
    
    	string file;
    	char *inread;
    	
    	ifstream inf (filename);
    	
    	while(inf.pos()<inf.eof()){
    	
    		inf>>inread[inf.pos];
    		
    	}//end while
    	
    	inf.close()
    	
    	for(int i=0;i<strlen(inread);i++){
    	
    		file+=inread[i];
    		
    	}//end for
    	
    	return file;
    	
    }//end readFile
    I was assuming that ifstream had a function like pos that might say what the current position in the file might be

    if it doesn't inf.pos() coudl be replaced with an incrementing value to mark position (starting at 0)

  11. #11
    Registered User
    Join Date
    Nov 2004
    Posts
    86
    for(int i=0;i<strlen(inread);i++){

    file+=inread[i];

    }//end for
    sry, I forgot to add whitespace

    after the
    Code:
    file+=inread[i]
    there should be an if statement:

    Code:
    if(i!=strlen(inread)-1){
    
              file+=" ";
    
    }//end if

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > would this work? maybe?
    Nope - inread is an uninitialised pointer.

  13. #13
    Registered User
    Join Date
    Nov 2004
    Posts
    86
    how bout this?

    Code:
    #include <fstream>
    #include <string>
    #include <vector>
    
    using namespace std;
    using std::string;
    
    string readFile(string filename){
    
            string file;
            vector <char> inread;
            
            ifstream inf (filename);
            
            while(inf.pos()<inf.eof()){
            
                    inf>>inread[inf.pos];
                    
            }//end while
            
            inf.close()
            
            for(int i=0;i<inread.size();i++){
            
                    file+=inread[i];
                    
                    if(i!=inread.size()-1){
    
                                    file+=" ";
    
                    }//end if
    
            }//end for
            
            return file;
            
    }//end readFile
    does pos() actually exit?

  14. #14
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    I was assuming that ifstream had a function like pos that might say what the current position in the file might be
    Why do you need to assume anything? Look at the documentation, I know it may seem like a novel idea but it seems to work for everyone else.

    [edit]

    BTW this is the 3rd time you have made this post - I mean exact name and everything. Whats the deal?

    [/edit]
    Last edited by andyhunter; 01-01-2005 at 07:13 PM.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  15. #15
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    File pointers do exist, for example fseek() in C or seekp(), seekg(), tellg() and tellp())in C++. They wouldn't seem to the most efficient way to read a whole file, but if you knew exactly how much of a file you wanted to read, or if you wanted to start reading, stop reading, etc at a particular location in a file, then using them would make more sense. Such knowledge is more readily available if the file contents are perfectly ordered.

    eof() returns a non-zero value if the end of file has been reached, and a zero value if the end of file hasn't been reached. What the non-zero value is is not indicated and I wouldn't recommend it be measured against as in

    while(inf.pos()<inf.eof())

Popular pages Recent additions subscribe to a feed