C++ file previewer, how do i enter the file and my program is not working?
-
I have a program that is supposed to preview the first ten lines of a file, but I have no idea how to enter the file name. I tried using a word document file path, and nothing happened, I tried using a notepad file name, still nothing. The program is supposed to ask the user for the name of a text file. The program should display the first 10 lines of the file on the screen. If the file has fewer than 10 lines, then the entire file should be displayed along with a message indicating the entire file has been displayed. I am supposed to test it with a file that has 5 lines and a file that has 20 lines. Here is my code #include <iostream> #include <fstream> #include <string> using namespace std; int main() { cout << "please enter the name of the file: "; string fileName; getline(cin, fileName); ifstream file(fileName.c_str(), ios::in); string line; for (int count = 1; !file.eof(); ++count) { getline(file, line); cout << line << endl; if (count % 24 == 0) system("pause"); } system("pause"); } I have no idea how to enter the file name to preview it.
-
Answer:
EDIT: Also, you cannot use a word document, simple C++ operations like this cannot read Word files, you need to use something like a simple txt (text) file The first thing i would do is add an error check to see if the file even exists. You can do that like this _____________________________________ if(file.fail()) { cout<<"The input file could not be found!\n"; exit(1); } _____________________________________ ^ That will let you know if the file even exists, remember the file needs to be located within the same directory (or folder) in which your cpp file is saved in I edited ur code, try this ______________________ #include <iostream> #include <fstream> #include <string> #include <cstdlib> using namespace std; int main() { cout << "please enter the name of the file: "; string fileName; getline(cin, fileName); ifstream file(fileName.c_str()); if(file.fail()) { cout<<"The input file could not be found!\n"; exit(1); } string line; int counter=0; while(getline(file, line)) { ++counter; } cout<<endl<<counter<<endl; file.close(); file.open(fileName.c_str()); if(counter<10) { cout<<"\nThe file has fewer than 10 lines, the entire file will be displayed\n"; while(getline(file, line)) { cout<<line<<endl; } } else { counter=1; cout<<"\nThe file has more than 10 lines. Here are the first 10 lines\n"; while(getline(file, line)) { if(counter<=10) { cout<<line<<endl; } else if(counter > 10) { break; } ++counter; } } }
raye at Yahoo! Answers Visit the source
Related Q & A:
- How can I enter Mongo as a superuser or reset users?Best solution by Database Administrators
- How Can I use .net dll in C program?Best solution by Stack Overflow
- How do I enter the renewable energy industry?Best solution by answers.yahoo.com
- How do I enter my Pepsi codes?Best solution by Yahoo! Answers
- How can I enter my photo in my yahoo mail?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.