text file won't show

This is a discussion on text file won't show within the C++ Programming forums, part of the General Programming Boards category; I'm trying to get this to read a text file that I have and print it out on the screen, ...

  1. #1
    jed
    jed is offline
    Registered User
    Join Date
    Aug 2006
    Posts
    17

    text file won't show

    I'm trying to get this to read a text file that I have and print it out on the screen, when I run it, it shows a blank screen. What am I missing?
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main()
    {
    	string line;
    	ifstream openfile;
    	openfile.open("Program.txt");
    	if (openfile.is_open())
    	{
    		while (!openfile.eof())
    		{
    			getline (openfile,line);
    			cout<<line<<endl;
    		}
    	openfile.close();
    	}
    	else cout << "unable to open file";
    	cin.get();
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    The program looks ok to me. Generally you don't want to use eof( ) to control your while loop, you'd use while (getline (openfile,line)) instead, but that shouldn't really be the problem here.

    Are you sure the file exists and has text in it? Are you sure the correct file is being opened and not one in a different directory than you expected?

  3. #3
    jed
    jed is offline
    Registered User
    Join Date
    Aug 2006
    Posts
    17
    I have the text file in the same directory and name is correct. Can I specify a path to my desktop?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    You can, but it is more difficult than its worth. Try using the full path to the file just to verify that the program is opening the correct one. The working directory used when running the program isn't always the same as the directory that the executable is in.

    When putting the full path, make sure to change \ to / or \\. For example, openfile.open("C:\\Program.txt");

  5. #5
    jed
    jed is offline
    Registered User
    Join Date
    Aug 2006
    Posts
    17
    If the file doesn't open, shouldn't it tell me so?

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    Yes. My best guess is that you have another empty file in another directory (your project directory?) that it is opening instead.

    Does your text file have more than one line? If you press enter when the blank console appears, does the program finish?

  7. #7
    Registered User Moni's Avatar
    Join Date
    Oct 2002
    Location
    Dhaka, Bangladesh.
    Posts
    104
    try: "Program.txt.txt" in the file name.

    or check if showing file extentions are ticked there.
    We all are the components of a huge program...... the programmer is always debugging us with His debugger.

  8. #8
    Registered User
    Join Date
    Apr 2004
    Posts
    17

    why dont you try this instead

    try changing your condition to this:
    Code:
      while ( getline (openfile,line) ){
        cout << line << endl;
      }
    Last edited by cold_dog; 09-05-2006 at 12:49 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  3. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 03:49 PM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21