Need Help

This is a discussion on Need Help within the C++ Programming forums, part of the General Programming Boards category; I hope this helps. walk through it with a debugger and see what happens when you run the program the ...

  1. #31
    Nor
    Nor is offline
    h ֆhr s Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    I hope this helps. walk through it with a debugger and see what happens when you run the program the second time.
    Code:
    #include <iostream>
    #include <fstream>
    
    char MY_STRING[] = "Lets talk about files[:>)\n";
    void error(){std::cout << "Something bad happened";system("pause"); exit(1);}
    const unsigned int data_buffer_size = 30;
    int main()
    {
    	char data[ data_buffer_size ]; //data buffer
    	std::cout << MY_STRING;
    	system("pause");
    
    	std::cout << "Lets make a file\n";
    	std::fstream FILE;
    	//I'm using fstream because it is both an input and output stream.
    	//You can read and write to the same file. but lets keep it simple.
    	//Different ways to open a file.
    	//std::ios:: ...   unsigned int type
    	//       app,        append
    	//       ate,        open and seek to the end of file
    	//       binary,     I/O to be done in binary mode
    	//       in,         Open for read only access
    	//       out,        open for write access
    	//       trunc,      truncate file to 0 - length
    	/////////////////////////////////////////////////
    	FILE.open("FILENAME", std::ios::out | std::ios::app);  
    	//Default includes std::ios::text.
    	if(FILE.is_open()){
    		//My File has been opened. Lets do something with it.
    		FILE << MY_STRING << std::endl;
    		//is ALMOST the same as
    		FILE.write(MY_STRING, strlen(MY_STRING));
    		//Error check
    		if(FILE.bad())
    			error();
    		//So everything went well.
    		FILE.close();
    	}else{
    		error();
    	}
    
    	//Now lets read from the file.
    	FILE.open("FILENAME", std::ios::in);
    	if(FILE.is_open()){   
    		for( int LINE_NUMBER = 0; !FILE.eof(); LINE_NUMBER++){
    			//This reads 1 line from the file to the buffer.
    			FILE.getline(&data[0], data_buffer_size); 	
    			std::cout << LINE_NUMBER << ": " << data << std::endl;
    		}
    		FILE.close();
    	}
    	system("pause");
    	return 0;
    }
    also try changing 'MY_STRING' to more than 30 chars and see what happens.
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  2. #32
    Nor
    Nor is offline
    h ֆhr s Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    Quote Originally Posted by xbusterx View Post
    Well I'm trying to open the file, but it's not opening.
    I'm not sure what header you need to include for this.
    Code:
    ShellExecute(NULL, "open", _T("notepad.exe"), "FILENAME", NULL, SW_SHOWNORMAL);
    Quote Originally Posted by Nor View Post
    Sebastiani I got to ask. the code in your signature. whats it do?
    Sweet dude.
    Last edited by Nor; 11-18-2008 at 08:01 PM.
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  3. #33
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,622
    Just a few thoughts...
    Code:
    char MY_STRING[] = "Lets talk about files[:>)\n";
    Should probably be const std::string and inside the function.

    Code:
    void error(){std::cout << "Something bad happened";system("pause"); exit(1);}
    This is the kind of code Sebastiani has in the signature and shows clearly how code bunched together in unreadable. So don't bunch it together like that. Put it on several rows.

    And then system("pause") should be avoided. Any type of pause should be avoided, really. And it is usually bad practice to call exit from within a function. Just something to think of. For example, it could return true or false, and if false, then main function cleans up and returns.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #34
    Nor
    Nor is offline
    h ֆhr s Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    Quote Originally Posted by Elysia View Post
    Just a few thoughts...
    And then system("pause")
    system("pause") is the easiest way I know to halt the program so the user can view the output.
    It is one line and is self explanatory for a new programmer.
    Most debuggers create a new console output for 'std:ut', without halting the program the informations displayed will disappear.
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  5. #35
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,622
    It also locks the code to the Windows platform, making it non-portable.
    The best way is to do no pauses at all.
    Run the program through a good IDE that doesn't close the window when the program quits, and you won't need to.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #36
    Nor
    Nor is offline
    h ֆhr s Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    Quote Originally Posted by Elysia View Post
    It also locks the code to the Windows platform, making it non-portable.
    OK Elysia.
    Please replace all occurrences of 'system("pause");' with 'pause()'
    And include this before 'error()'
    Code:
    void pause(){
    	char key;
    	std::cout << "Press ENTER to continue" << std::endl;
    	for(;;){
    		std::cin >> key;
    		if(key)
    			break;
    	}
    }
    Now Elysia I got one for you. How would you make
    Code:
    ShellExecute(NULL, "open", _T("notepad.exe"), "FILENAME", NULL, SW_SHOWNORMAL);
    portable across windows and Linux?
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  7. #37
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,749
    Quote Originally Posted by Nor
    How would you make ... portable across windows and Linux?
    That depends on what exactly you are trying to do. If this is a genuine question, ask it in a new thread.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #38
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,622
    Quote Originally Posted by Nor View Post
    OK Elysia.
    Please replace all occurrences of 'system("pause");' with 'pause()'
    And include this before 'error()'
    The question begs - why do you need to pause?
    In pretty much any application - you don't.

    Now Elysia I got one for you. How would you make
    Code:
    ShellExecute(NULL, "open", _T("notepad.exe"), "FILENAME", NULL, SW_SHOWNORMAL);
    portable across windows and Linux?
    With a single line, it is not possible.
    But you can abstract it into a function and use defines to call the correct API respectively.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #39
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> system("pause") is the easiest way I know to halt the program so the user can view the output.

    Try cin.get( ). It's completely portable, and it doesn't resort to using dangerous API's like system( ). Again, with experience you will find there is less and less reason to do such a thing. Invoking a program from the command line or from an IDE is the way to go in most cases.

    >> Now Elysia I got one for you. How would you make *** portable across windows and Linux?

    You don't. But as Elysia already pointed out, a sufficient level of abstraction can make just about anything portable.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

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