Thread: Need help with displaying a text file in C++ program...

  1. #1
    Registered User
    Join Date
    Mar 2003

    Exclamation Need help with displaying a text file in C++ program...

    I'm trying to make a simple DOS C++ text editor. I figured out how to save to the file when you create a new document, now I'm having trouble viewing existing files. The program won't stop looping. (or repeats the last word over and over until the loop finish value is set). I'm pretty sure I need to use pointers and have it fine the '\0' (null byte) or something... Here's a snippet from the load function:

    int loadFile()
      char fileNameLoad[12], text[25], a;
      header(); //seperate function just to view colors :D
      cout<< "\nEnter file name: "; 
      cin>> fileNameLoad; 
      ifstream viewFile(fileNameLoad); //opens fileNameLoad for viewing
      cout<< "\nContents of file:\n"; 
      while (text != '\0') // ???
        viewFile>> text;
        cout<< text << " ";
      cout<< "\n\nPress any key to return to Main Menu...";
      return 0;
    I'm aware as of now that it only opens and displays files in the same directory that the program is installed into, that doesn't bother me. I need to know how to make it so that the program knows where the EOF is...


  2. #2
    I lurk
    Join Date
    Aug 2002
    while (viewFile >> text) cout << text << ' ';

  3. #3
    Registered User
    Join Date
    Mar 2003
    Thanks a lot!

    I just have one more question, (just started programming in C++ not too long ago, reading up on some books from the library) would it be considered ''bad programming'' to put the whole Load and view section of the program into one function? Or should I just load it with the function and display it within the main() function?

  4. #4
    Registered User
    Join Date
    Nov 2002
    You can do it any way you want. You can place it all in main if you would like. But the fxns make it easier to read/split up. For this fxn, the way it is would probably be the quickest. Unless you have a global array. If you are returning an array for instance, that could take up quite a bit of memory. memory v. speed, usually on the programmers mind when designing a program.

    on a side note, the way you currently have the loadFile fxn, you do not need it to return an int. you can return void.

    also, you do not need text to be an array, nor do you need the char a.

  5. #5
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Phildelphia, PA
    My teacher always says that you should be able to describe what a function does in a single sentence. Aim to write functions that are very good at one and only one task. If you do that, your functions will have greater modularity, that is, you will be able to reuse them more often. Most importantly, always try to code the best solution; don't just bang out something that barely works. Be proud of what you create.

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  6. #6
    Registered User sikamikaniko's Avatar
    Join Date
    Mar 2003

    Might be like this:

    { cout << text; }
    Try it

  7. #7
    Registered User sikamikaniko's Avatar
    Join Date
    Mar 2003
    I think, that if you have many actions with reading and writing to a file, than it is better to separate those actions in different functions. But in this case it is not important. Also, if You make a simple menu with switch and many cases, than it is good to put every "case" action in its own function, so it can be easier to read the code and edit it But after all its just a matter of opinion!

  8. #8

    Different Approach

    2 things I noticed that I'm not sure if they seem that obvious:

    I always use ios::binary on my call to create object of fstream whenever I open a file for editing or pretty near anything...
    might wanna look into that.

    You can insert or delete information at any point in the file you want by moving around it with seekg, tellg, seekp, tellp.

    Finally, when you're outputting the information in the file, you can loop it to <objectname>.eof (end of file)


  9. #9
    I am he who is the man! Stan100's Avatar
    Join Date
    Sep 2002
    Has anyone noticed that 60% of the questions asked now are of saving and loading files? Note to all: Use the search feature, it's there for a reason.
    Stan The Man. Beatles fan

    When I was a child,
    I spoke as a child,
    I thought as a child,
    I reasoned as a child.
    When I became a man,
    I put childish ways behind me"
    (the holy bible, Paul, in his first letter to the Cor. 13:11)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM