C++ Files.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: C++ Files.

  1. #1
    Join Date
    Jul 2017
    Posts
    10

    C++ Files.

    Can someone tell me why the statement if( read.eof() ) is not executed?
    I'm reading the whole file so this stream should reach the end of file, yet i
    can't understand why the statement is not running.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    int main(){
    	
    	std::ifstream read("Source.cpp", std::ios::binary |
    		std::ios::ate);
    
    	std::ofstream write("C:\\Users\\zervos\\Desktop\\s.cpp",
    		std::ios::binary);
    
    	std::streampos size;
    
    	char *bytes;
    
    	if (read.is_open() && write.is_open()) {
    
    		size =  read.tellg();
    
    		bytes = new char[size];
    
    		read.seekg(0, std::ios::beg);
    
    		read.read(bytes, size);
    
    		if ( read.eof() )
    			std::cout << "eof" << std::endl;
    
    		read.close();
    
    		write.write(bytes, size);
    
    		delete[] bytes;
    
    		write.close();
    
    	}
    
    	else
    		std::cout << "Files error" << std::endl;
    	
    	system("pause");
    	return 0;
    }

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,584

    Re: C++ Files.

    eof is true when the get pointer is at the eof and another read is done. It's not a status but an error condition. If you try this
    Code:
    		read.read(bytes, size);
    		if (read.eof())
    			std::cout << "eof1" << std::endl;
    
    		read.read(bytes, 1);
    
    		if (read.eof())
    			std::cout << "eof2" << std::endl;
    You'll find the eof2 is displayed, not eof1.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C, C++ Compiler: Microsoft VS2017.2

  3. #3
    Join Date
    Jul 2017
    Posts
    10

    Re: C++ Files.

    I searched how to read chunks of bytes from a file. Then i programmed this which is not working.
    I'm trying to read a file byte to byte but my output file (at the end of the execution) is zero bytes while the original is 585 bytes
    Obviously i did not understand something.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    int main(){
    	
    	std::ifstream read("Source.cpp", std::ios::binary |
    		std::ios::ate);
    
    	std::ofstream write("C:\\Users\\zervos\\Desktop\\s.cpp",
    		std::ios::binary);
    
    
    	char *byte = new char;
    
    	if (read.is_open() && write.is_open()) {
    
    		while (1) {
    
    			read.read(byte, 1);
    
    			if (!read.gcount())
    				break;
    
    			write.write(byte, 1);
    		}
    
    		read.close();
    		write.close();
    
    		delete byte;
    	}
    
    	else
    		std::cout << "Files error" << std::endl;
    	
    	system("pause");
    	return 0;
    }
    These are really stupid questions, but i cant find a good documentation for c++ libraries. Also in visual studio when you select a method it doesn't showing you info about what the method does, but only the definition. If you can point me on a website that would be nice.
    Last edited by babaliaris; July 10th, 2017 at 11:59 AM.

  4. #4
    Join Date
    Jul 2017
    Posts
    10

    Re: C++ Files.

    Quote Originally Posted by babaliaris View Post
    I searched how to read chunks of bytes from a file. Then i programmed this which is not working.
    I'm trying to read a file byte to byte but my output file (at the end of the execution) is zero bytes while the original is 585 bytes
    Obviously i did not understand something.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    int main(){
    	
    	std::ifstream read("Source.cpp", std::ios::binary |
    		std::ios::ate);
    
    	std::ofstream write("C:\\Users\\zervos\\Desktop\\s.cpp",
    		std::ios::binary);
    
    
    	char *byte = new char;
    
    	if (read.is_open() && write.is_open()) {
    
    		while (1) {
    
    			read.read(byte, 1);
    
    			if (!read.gcount())
    				break;
    
    			write.write(byte, 1);
    		}
    
    		read.close();
    		write.close();
    
    		delete byte;
    	}
    
    	else
    		std::cout << "Files error" << std::endl;
    	
    	system("pause");
    	return 0;
    }
    These are really stupid questions, but i cant find a good documentation for c++ libraries. Also in visual studio when you select a method it doesn't showing you info about what the method does, but only the definition. If you can point me on a website that would be nice.
    LOL sorry my bad, i forgot that: std::ifstream read("Source.cpp", std::ios::binary | std::ios::ate);

    I start reading from the end of the file

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,584

    Re: C++ Files.

    but i cant find a good documentation for c++ libraries
    Have a look at http://www.cplusplus.com/reference/

    Reading/writing a file in chucks is fairly easy. Consider
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main()
    {
    	const size_t bufsz = 20;
    
    	ifstream read("data.txt", ios::binary);
    	ofstream write("data4.txt", ios::binary);
    
    	if (read.is_open() && write.is_open()) {
    		char buf[bufsz];
    
    		do {
    			read.read(buf, bufsz);
    			write.write(buf, read.gcount());
    		} while (read.gcount());
    
    		read.close();
    		write.close();
    	} else
    		std::cout << "Files error" << std::endl;
    
    	//system("pause");
    	return 0;
    }
    which copies read to write using a buffer of size bufsz characters.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C, C++ Compiler: Microsoft VS2017.2

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)