C/C++
Now Reading
Displaying Text from a File C++2 min read
0
The short URL of the present article is: https://4hax.net/djh04

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 syntax:

#include
#include
#include
#include
#include
#include

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 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 }
if nChar > 0
{
nChar–
getline (fileName, line nChar);
}
Else
{
cout break;

cout 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. 😀

Oh the link for the full code is here:

http://pastebin.com/vuMSGrK9


The short URL of the present article is: https://4hax.net/djh04
What's your reaction?
LOVE IT
0%
INTERESTED
0%
MEH...
0%
WHAT?
0%
ANGRY
0%
SAD
0%
About The Author
ayush

Leave a Response