How To: Displaying Text from a File C++

Displaying Text from a File C++

This is a little c++ program that I thought would be handy in the future. Although, I apologize in advance for any errors in the code since I haven't had the chance to compile it yet. Anyway, please if you are a script kiddie please do me a favor and at least read this article fully. :)

Planning

Of course before I start spitting out lines of English, I need to figure out what I want the program to do and how to do what I want the program to do (wow that't a mouthful).

The plan:

  • The program should be able to retrieve any file
  • Should be able to go line by line printing the contents withing the program
  • Must be in a While loop

Code Part 1

Firstly, we must have the right directive with the #include <(directive)> syntax:

#include <cstido>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <cstring>
#include <string>

I should note I'm still new to the language so I wasn't one-hundred percent sure which one I needed.

Code Part 2

Now the program needs to be able to ask the user for a input and store that input in a variable. But we also would need to have it able to eventually go line by line for the later code:

char string fileChar()
char string nChar;

ifstream inFile;
string fileName;

cout << "Enter in File: \n" <<
cin >> fileName;
inFile.open (fileName)
inFile >> str;

nChar = 1

'char string fileChar()' is seperating this snippet of the code from the rest of the program so that we may work one this part individually. 'ifstream inFile;' is allowing the program to physically modify a file on the computer. We will need this later for actually getting the file and also for the contents. 'nChar' right now does nothing but is crucial to the final program.

Code Part 3

Now the main part of the code:

int main()
While nChar != 0
{
getline (fileName, line nChar);
}
nChar + 1
getline (fileName, line nChar);
cout <<nChar<<
}
if nChar > 0
{
nChar--
getline (fileName, line nChar);
}
Else
{
cout << nChar <<
break;

cout <<Please press enter...<<
break;
return 0;

Ok so this part is a bunch of if statements in a while loop. In a brief summary, while nChar NOT equal 0 than getline of the current line in the file and prints the contents. Yet, if nChar greater than 0 than nChar subtracts one until the number reaches zero, else print the content of the file.

Ok... so that's that. I would like some feedback on how I could improve, but please give me some lovins. I would like it if y'all would tinker around and do whatever you want. Anyway, hack on my fellow partners in crime. :D

Oh the link for the full code is here:

http://pastebin.com/vuMSGrK9

Just updated your iPhone to iOS 18? You'll find a ton of hot new features for some of your most-used Apple apps. Dive in and see for yourself:

2 Comments

You're right, this would definitely not compile... In my opinion, there's a better way to do this:

string str, fileName;
cin >> fileName;
while ( getline(fileName, str) )
{
cout << str << endl;
}

(This is just the core of the program, you'll need the #include's and the namespace line and the int main()...

This is well and good, but in I wouldn't waste my time re-inventing the wheel. The cat command does the same thing (found in every Linux distro I've ever laid eyes on).

I'm new to C++ but thanks for the advice.

Share Your Thoughts

  • Hot
  • Latest