reading file

This is a discussion on reading file within the C++ Programming forums, part of the General Programming Boards category; Good day folks. I have a text file that I'd like to show the user but first I need to ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    291

    reading file

    Good day folks.

    I have a text file that I'd like to show the user but first I need to edit the text a bit.

    Code:
      std::fstream infile("test.txt");
    	
      if (! infile.eof() )
        std::cout << "could not open file" << std::endl;
    
      // some get or getline function
    I was thinking about using the get command but I dont know how big the file is so I dont know who big the buffer needs to be. Is there any way to check the file size or if anyone has any better ideas as to reading the file into a buffer Im all ears.

    Another thing, once the file has been placed into a buffer, how can I then as simple as possible edit the file. Say i.e. I have html and want to remove all html tags, how I go about doing such a thing. Links to any tutorials would be excellent.

    Appreciate any inout on the subject.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    One solution is getline() and string. Another solution and is the one you mentioned is to get the size of the file. Use seekg(), std::end, and tellg().

    Kuphryn

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Thanks for your suggestion kuphryn

    Code:
      file.seekp(0,std::ios::end);
    		
      int length = file.tellp();
    		
      char *buffer = new char[length+1];
    
      buffer[length] = '\0';
    
      file.get(buffer, length);
    The length thing works fine but file.get(...) doesnt place the data from the file into the variable buffer. Any idea what Im doing wrong ?

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    One solution is read().

    Kuphryn

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    I followed your suggestion and changed to read

    Code:
      file.read(buffer, length);
    Now when I output the data from the buffer I get a whole bunch 's (It's an I with a dot on top, a bit hard to see). This is all very bizard. It kind of looks like the orginal letters have been switched with this odd looking char. I have no idea what to make of this.

    Any thoughts.

    Your help is very much appreciated

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    What kind of data do you want to process? One example is text. Another example is binary. You are seeing binary codes.

    Code:
    file.open(TEXT("filename.abc", ios::in | ios::binary) // binary mode
    ""                                             ios::in) // text mode
    Kuphryn

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    I'd like to process text, not binary.

    Code:
    std::fstream file ("text.txt", std::ios::in | std::ios::out);
    
    file.seekp(0,std::ios::end);
    		
    int length = file.tellp();
    		
    char *buffer = new char[length+1];
    
    buffer[length] = '\0';
    
    file.read(buffer, length);
    
    m_text = buffer;
    
    UpdateData(FALSE);
    		
    file.close();
    Shouldnt the above output text ?

  8. #8
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Correct.

    m_text << file;

    Kuphryn

  9. #9
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Originally posted by kuphryn

    m_text << file;
    Your code gave me the following error :
    error C2678: binary '<<' : no operator defined which takes a left-hand operand of type 'class CString' (or there is no acceptable conversion)


    btw, m_text is CString.

    This is still not working.

  10. #10
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Okay. I did not know you are coding in Win32 environment.

    Consider CFile and CStdioFile.

    Although do not understand how you could get all weird characters via read(). That is definitely weird. I could understand a few weird characters symbolizing spaces, but not all.

    Post a link to your project. I want to see the actual code. Also, include the test file too.

    Kuphryn

  11. #11
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    I zipped the whole project and you can download it from this addr http://home.broadpark.no/~hsagdahl/socket_app.zip

    Its a mfc project made in visual c++.

    If you run the program just click 'browse' and it will connect to www.google.com and recive a html file. You'll also see the odd looking char in the edit box window.

    Thanks a bunch

  12. #12
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Okay. Give me some time. I was away and just got back home.

    Kuphryn

  13. #13
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Okay. There are two bugs.

    One you mistyped the "text.txt," which should be "test.txt." Secondly, "seekp()" should be "seekg()." The second bug is you need to seekg() to the beginner of the file since the pointer is at the end. Set the pointer back to the beginning of the file and then read in data.

    Kuphryn

  14. #14
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Thanks a bunch.

    Will make the changes once I get back home.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-22-2008, 12:19 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21