Thread: Need Help

  1. #31
    ‡ †hë Ö†hÈr 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
    ‡ †hë Ö†hÈr 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 09:01 PM.
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  3. #33
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    ‡ †hë Ö†hÈr 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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    ‡ †hë Ö†hÈr 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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #38
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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;
    }

Popular pages Recent additions subscribe to a feed