Text file scanning

This is a discussion on Text file scanning within the C++ Programming forums, part of the General Programming Boards category; I am new to C++ and I have been pulling my hair out trying to accomplish this simple task. I ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    3

    Smile 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

  2. #2
    Unregistered
    Guest
    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.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    3
    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

  4. #4
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    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...

  5. #5
    Unregistered
    Guest
    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.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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";

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 06:35 PM

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