File is not read!

This is a discussion on File is not read! within the C++ Programming forums, part of the General Programming Boards category; Hi, I am totally new to this programming stuff, and requesting u all ,to help me out in this ......... ...

  1. #1
    Birdie
    Guest

    Unhappy File is not read!

    Hi,
    I am totally new to this programming stuff, and requesting u all ,to help me out in this .........

    My Q is I have to take valid input FIle from the user ,and open it,until user presses -1 to exit........ or until he/she doesnt type the valid name (which in my case is "records.txt")......
    My code is working ........but the problem is if I type valid name ,it get stucked.........

    Code:
    int main()
    {
    
    
    int number = 0;
    string file = " ";
       ifstream inputFile("records.txt", ios::in );
       cout<<"Enter the file (-1 to end)"<<endl;
       file=readLine();
    
       while(file !="-1")
       {
    		
    		
    	if(file != "records.txt" )
    	{
    	   cout<<"File doesnt exist"<<endl;
    	   inputFile.clear();
                      cout<<"Please re-enter"<<endl;
    	    file=readLine();
    			
    	}
    		
    	}
    	if (file == "-1")
    	{
    		exit(0);
    	}
    
    	if (file== "records.txt")
    	{
    	
    	     inputFile.open("records.txt".c_str(), ios::in);
    	     adatabase(database,number);// populating the database//
    	     displayMainMenu();
                         getMenuOption(database,number);
    
    
    	}
    	
    	return 0;
    }
    
    string readLine()
    {
        char buffer[SIZE];
    	cin.getline(buffer, SIZE, '\n');
    	return buffer;
    }

  2. #2
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    I can't be volumes of help to you, as I could not get this to compile (I'm very new too). But it may get more of a response if you list your compiler and your include files.

    Also, even though you have code in your program to make sure the user enters the correct file name, you do not appear to be making sure that the file can be opened (I don't know, the function you use may do that automatically).

    Also, since you have a number of statements under your test to see that the user enters the correct file name, perhaps you could put a print statement of some kind after each one to see exactly where your program is getting stuck (if you haven't already tried that). If you have tried that you could reply with exactly what line of code was giving you issues
    Demonographic rhinology is not the only possible outcome, but why take the chance

  3. #3
    Registered User billholm's Avatar
    Join Date
    Apr 2002
    Posts
    225

    Lightbulb

    Okay... how 'bout tryin' this Bird:

    Replace:

    file != "-1" or file == "records.txt"

    with:

    strcmp(file, "-1") != 0 or strcmp(file, "records.txt") == 0

    respectively.

    strcmp(char *s1, char *s2) returns:

    < 0 if s1 < s2
    > 0 if s1 > s2
    == 0 if s1 == s2
    All men are created equal. But some are more equal than others.

    Visit me at http://www.angelfire.com/my/billholm

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, 01:36 PM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 05:54 PM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 04:39 AM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM

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