-
Text file scanning
I am new to C++ and I have been pulling my hair out trying to accomplish this simple task.
I have been able to open a text file and scan it's contents. When I do this it prints to screen the entire document. I want to be able to only display it on a single line as if it was flickering through each line or (scanning it). Is there a way to detect the carriage return then go back to the beggining of the display line and show the next one. Here is my code. Any help would be much appriciated.
#include <fstream.h>
#include <iostream.h>
#include <stdio.h>
int main(void)
{
char contents[50000];
char filename[30];
if (filename != "*"){
cout << "What file do you want to display? PRESS * To QUIT---->";
cin.getline(filename, 30, '\n');
ifstream file(filename);
cout << filename;
do
{
file.getline(contents, 50000, '\n');
cout << contents;
cout << "\n";
}
while (file.eof()!=1);
file.close();
cout <<"Done reading file";
}
return 0;
}
Thank You,
Jay
-
Well, you could restructure the file so there is more than one newline char in it and use getline to read it in one line at a time. That seems to be the traditional way. Particularly if the text editor you use doesn't have line wrapping.
Another approach would be to read in just 80 char (more or less depending on your screen) at a time rather than 5000. or use a loop to read in one char at a time 80 times.
-
I think that I did not explain it very well before. I have no trouble scanning the document, but, when the text is printed to the screen it displays the entire document. I want the output that is printed to the screen to only consist of one line that is changing.
Kind of like when you install a program into windows and it goes through the files that are being installed. It lists them in the same spot one after the other. I want this to happen with each line of the text document.
Sorry if my last post was confusing it is hard to explain this idea.
Thanks for your info.
Jay
-
Rather hackish, but...
After you print a line, leave it up there a second, clear the screen, move the cursor back to where you want to start, and start over...
-
Okay, assume you have the entire file contents in the char array called contents. If you want to display just one line of material on the screen at a time you have to know how many char across the screen there can be. With usual fonts that is about 80 more or less. Therefore, unless you have some other embedded flags indicating end of the line you need to read out the first 80 (or whatever) char from contents and display them on the screen. Pause. Clear the screen. Read out the second set of 80 char and display them on the screen. Pause. Clear the screen. Read out the third set of 80 char and display them to the screen. etc. etc. etc. When you do this however you will have no knowledge of whether you are breaking the line in the middle of a word or not. You can probably write a more sophisticated program to determine the spot to break the line so you don't exceed the limit of the screen and don't break in the middle of a word, but it will take some work. If you have a flag of some sort (say newline chars or semicolons or tildes or whatever) embedded in the file, then things become much easier. If you are trying to write your own word wrap program I wish you luck.
-
What compiler are you using?
With a borland compiler, use gotoxy(x,y) in <conio.h>.
With windows compilers, there is SetConsoleCursorPosition().
Or you could try backspaces, but this erases the text:
cout << "\b\b\b\b";
-
maybe have contents as a two-dimentional array?
set the second index to however long you want a single line to be (usually 80 like everyone else is saying). the first index will be the number of lines. after the array is filled, you can display individual line using only the first index. I'm just now working with arrays of strings (2d char arrays), so i'm not too experienced in all the details of them.