noob tutorial (reading file) clarification
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: noob tutorial (reading file) clarification

Hybrid View

  1. #1
    Join Date
    Feb 2013
    Posts
    2

    noob tutorial (reading file) clarification

    HTML Code:
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    using namespace std;
    
    int main (){
        char filename[50];
        ifstream asd;
        cin.getline(filename,50);
        asd.open(filename);
        
        //to close the program if txt does not open
        if (!asd.is_open()){
                            exit(EXIT_FAILURE);
                            }
                            
        char asdasd [50];
        asd >> asdasd;
        while(asd.good()){
                          cout << asdasd << " " << " ";
                          asd >> asdasd;
                          }
                          
        system ("pause");
        return 0;
    }
    I learned this from a tutorial and i mostly understood it but i'm having some problems to understand why it needs <while(asd.good()){> before cout and another <asd >> asdasd;> after cout. Without the while i only have the first word of the txt and without the second <asd >> asdasd> i have compile error. Any clarification d be really appreciated. Thanks

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,245

    Re: noob tutorial (reading file) clarification

    Code:
    asd >> asdasd;
    This reads characters from the file stream asd to the char array asdasd until a terminating char is read (space, tab, newline , end-of-file etc). It doesn't read the whole file. Hence you need to keep reading the next set of characters until all have been read. This is the purpose of the while loop. Effectively it's saying that while characters have been read (ie eveything is good) output the characters and get the next set of characters. If there are no more characters in the file to read (ie end of file reached) then everything is not good and the while loop terminates. You can actually rewrite this loop more simply as

    Code:
    while (asd >> asdasd){
    	cout << asdasd << " " << " ";
    }
    Which does the same job.

    I don't know which tutorial you got it from but the program has a serious flaw. As I said above, characters are read into the asdasd array until a terminating char is read. This can be any size. However, asdasd has only been declared to hold a maximum of 50 chars (including the terminating NULL so effectively only 49). If the file has a sequence of non-terminating characters longer than 49 characters then the program will happily read them from the file and overwrite what ever happens to be in memory following the asdasd array. This is called buffer overrun and is a security issue. A better way of reading is to use the string class where the programmer doesn't have to be aware of the size of memory being used. The code could then be

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main () {
    string		filename,
    		asdasd;
    
    ifstream	asd;
    
    	cout << "Enter file name: ";
    	cin >> filename;
    	asd.open(filename.c_str());
        
           //to close the program if txt does not open
           if (!asd.is_open()) {
    		return (EXIT_FAILURE);
    	}
    
    	//Read text until problem ie eof
           while (asd >> asdasd) {
    		cout << asdasd << "  ";
           }
                          
          system ("pause");
          return 0;
    }
    Also you normally don't terminate a program by calling exit() unless the situation is disastrous. Normally return is used with the exit code. There is potentially a problem using exit() in this situation as when you call the exit() or _exit() functions, the destructors for any temporary or automatic objects that exist at the time of the call are not called. An automatic object is an object that is defined in a function where the object is not declared to be static. A temporary object is an object created by the compiler.

    Hope this helps your understanding.

  3. #3
    Join Date
    Feb 2013
    Posts
    2

    Re: noob tutorial (reading file) clarification

    yes, that helped a lot, thanks

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center