I need help running my program this is what it asks me for "Write a program that asks 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, the entire file should be displayed along with a message indicating the entire file has been displayed" I think i have coded the program correctly but when I try to run it, it doesnt run...please help... this is what i have so far...
Thanks in advance...

#include <iostream>
#include <fstream>
 using namespace std;

 int main()
 {
 // Variables needed to read file one character at a time.
 const int LENGTH = 81;
 fstream file;
 char ch, fileName[LENGTH];

 // Get file name and open file.
 cout << "Enter a file name: ";
 cin >> fileName;
 file.open(fileName, ios::in);
 if (!file)
 {
 cout << fileName << " could not be opened.\n";
 return 0;
 }

 // Read file one character at a time and echo to screen.
 file.get(ch);
 while (!file.fail())
 {
 cout << ch;
 file.get(ch);
 }

 // Close file
file.close();
return 0;
}

Recommended Answers

All 6 Replies

Dont write
using namespace std;
Try without it. It worked on my pc. :-D

I think i have coded the program correctly but when I try to run it, it doesnt run...

Can you be more specific about what happens when it "doesn't run"? Does the program not start at all? Does it ask you for a filename but then give an error? Does it get past the filename part and loop forever? Does it not loop at all? Did you try to debug your code? We need more information to help you.

Dont write
using namespace std;
Try without it. It worked on my pc. :-D

You need a newer compiler, dude. Yours is non-compliant with standard C++.

Where is the text file? If it is not the same location where you .cpp file is then you need to put the exact path to where the file is.

Sorry for not specifying...but when I run it, I type the name of the txt file, either "file" or "file.txt" (which is correctly placed in the same folder as my .cpp) and it just says "file.txt could not be opened" or "file could not be opened" then i just hit any key to continue and it exits...I know i put the txt file correctly in the same path as the .cpp and the project but for some reason it doesnt open the txt file... do you think it has something to do with the coding? Am I missing something?
Ps. I have visual studio 2012, which is pretty much the same as 2010 or 2008...just incase...
Thanks everyone for responds!

It can't find the file. I'd suggest trying perror() to get a little more information:

if (!file)
{
    perror("Error opening file");
    return 0;
}

perror() is declared in <cstdio>, so be sure to include that header. Most likely it'll say something like "file not found", in which case you need to track down the issue that Nathan mentioned, which is placing the file in the current working directory. The current working directory is usually where the executable resides, but sometimes it could be a debug folder if you're using an IDE.

You need a newer compiler, dude. Yours is non-compliant with standard C++.

Hi there! Now I understood why namespace was not recognised by my compiler. BTW it was borland c++ 4.5. I'd better download VC++. Thank you for pointing out!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.