I want to make a program that will start off by opening a text file(blank). The user will put an expression in the file and as he hits the enter(enters a newline character) the result will be shown in the next line of the same open file. Itz almost like replacing the console with a text file. The way mathematica and maple evaluates itz inputs. Is it possible without too much hassle?

Is there any reason this has to be an interactive file rather than an interactive terminal session that writes to a file periodically?

There is actually no reason. The program i have writes to a file periodically. But i was thinking if it could be done, just for fun!

It's possible, but not practical since on the surface an interactive terminal session has identical functionality and is considerably faster because an interactive file constantly reads from and writes to a slow physical device.

hmmm, I undestand. But even then i would like to try that out, provided that someone helps me along the way. The first major problem is to check for the input as the user is typing on the notepad, is it even possible?

Yes, do research on kbhit. You should find out quickly whether your implementation supports it or not, and how to write your own if it doesn't.

I used kbhit() before as i made some small games. I used it inside game loops when u need to read the input only when kbhit() returns a nonzero value(i.e there's been a keyboard hit). I dont know if it can help us with the interactive file thingy. Atleast the following piece of code does not.

#include<conio.h>
#include<iostream>
#include<fstream>

using namespace std;

int main()
{
	ofstream out("out.txt");
	out.close();
	system("out.txt");
	char ch;
	while(true)
	{
		if(kbhit())
		{
			ch = getchar();
			cout<<ch;
		}
	}
	
}

Whether it works or not really depends on what you want it to do. What were you expecting?

The program opens a blank text file as it starts. The user writes something on that file. I want to know what the user is typing on that textfile. Since i havent done anything like this before, i dont know what to expect from kbhit() in this regard. Maybe a function that can globally read keyboard inputs is necessary in this case, not something that only works when the console is active. I am not sure kbhit() or getchar() will do that job for me.

Think getch instead of kbhit. An interactive file isn't a file you write directly to with the keyboard. An interactive file is a file that is saved with every change made. You use the console as the middleman between the keyboard and file, but every character you enter is written straight away.

The only difference between, say, vanilla Notepad and Notepad with an interactive file is that vanilla Notepad only saves when you tell it to. Notepad with an interactive file would save constantly so that if power is interrupted and you have to reboot, you don't lose any of your work.

>> You use the console as the middleman between the keyboard and file, but every character you enter is written straight away.

But that is the problem i have been talking about. I dont want to use the console as a middle man. Although my program will start as a console program i want it know what is being written in the file it just opened. Which takes us back to post # 1.

Let me describe you the scenerio once again. Someone runs my program,
1. My program starts of opening a console (obviously).
2. Then it creates a blank file and opens it up for the user to write something in it
Say the user wants to simplify a boolean expression so he enters it in the file.
3. Then when he is finished entering the boolean expression he presses enter ( which means he just created a new line character in the file). My program can check in real time what is being written in the file. So when the new line character is entered, it reads the file for the boolean expression and produces the minimized expression which is also displayed in the file.
4. The user closes the file, my program ends.

I know it sounds like MFC! But is it not possible just using console application, and that is without much hassle.

>I dont want to use the console as a middle man.
So you don't want the user to see either what he types or the contents of the file at any given time? Without an intermediate buffer (the console window for example), a file is nothing but a mysterious and invisible "thing" that you can only hope is working correctly.

Now that I know what you're trying to do, it seems pretty silly.

>1. My program starts of opening a console (obviously).
So open a console and use that as your intermediary between the user and the file.

>2. Then it creates a blank file and opens it up for the user to write something in it
Same thing. The console window is your window into the file.

>So when the new line character is entered, it reads the file
Why not just save the expression as a string, write it to the file when a newline is entered, process the string for a result, and then write the result to the file?

As an intellectual exercise this would result in you writing your own text editor that just happens to be dreadfully inefficient. The only interesting part of such an exercise would be to write it so that it can be plugged into any program so that the program can use an "interactive" file.

As an intellectual exercise this would result in you writing your own text editor that just happens to be dreadfully inefficient.
>> So the only way to do would be to design my very own text-editor. Sounds like lots of coding and pain. Am i right?

Yea, pretty much. Though writing a text editor is an amusing exercise.

Can I do so by console programming, without the help of any MFC.

Sure, a good way to write a portable text editor is to design an ed clone.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.