stream help!?

This is a discussion on stream help!? within the C++ Programming forums, part of the General Programming Boards category; Hi, this console app asks for a filename, then opens the file with that name (if it exists ofcourse), then ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    80

    stream help!?

    Hi, this console app asks for a filename, then opens the file with that name (if it exists ofcourse),
    then it writes the file content to screen. After that the user may add text to the end of that file...
    Now to the question... how do I make the program to behave more lika a text editor which let
    you edit the file with more freedom. Like notepad but in a console environment.
    Any help is appreciated.
    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <conio.h>
    #include <iomanip.h>
    #include <string.h>
    
    int main()
    {
    	int radnr=1;
    	char filename[256], yn='y', thetext[500];
    	while (yn=='y')
    	{
    		cout << "Filename: ";
    		cin >> filename;
    		ifstream text;
    		text.open(filename);
    		if (!text)
    		{
    			cerr << "Could not open file!" <<     endl;
    		}
    		else
    		{
    			for (;!text.eof(); radnr++)
    			{
    				text.getline(thetext, 500);
    				cout << setw(4) << setfill('0') << radnr << " " 
    				<< thetext << endl; 
    		
    			}
    			text.close();
    			ofstream skriv(filename, ios::app);
    			for (;true; skriv << thetext << endl)
    			{
    				cin.getline(thetext, 500);
    				if (strcmp (thetext, "/break")==0)
    				return 0;
    			}
    			skriv.close();
    		}
    		cout << "Open another file? (y/n)" << endl;
    		yn=getch();
    	}
    	return 0;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Like notepad but in a console environment.
    You'll want to keep the file (or at least parts of it) in memory, then it's just a matter of defining what operations can be performed on the buffer and writing algorithms to do them. Look up the Unix editor ed for ideas on what kind of interface to use.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    80
    OK, thanks.
    Currently I only have WinXP installed.. is there a way to check ed anyway with cygwin maybe? Or is the only way by installing another OS?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Documentation can be found online with a simple search such as "man ed". What you want to look for are the commands and line addressing scheme. It's actually quite elegant when you think about it, especially considering that a clone can be written in 100% standard C or C++.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    80
    Thanks, I'll do a search right away.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New stream flushing FAQ candidate?
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 07-01-2009, 06:57 PM
  2. Discard wrong data from stream
    By mesmer in forum C Programming
    Replies: 7
    Last Post: 11-16-2008, 02:30 AM
  3. Closing a stream
    By cunnus88 in forum C++ Programming
    Replies: 8
    Last Post: 02-21-2008, 05:15 PM
  4. Help! About text stream and binary stream
    By Antigloss in forum C Programming
    Replies: 1
    Last Post: 09-01-2005, 09:40 AM
  5. Replies: 9
    Last Post: 07-01-2002, 08:50 AM

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