Problem in file handling!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SUNIL TYATA
    New Member
    • Feb 2010
    • 18

    #1

    Problem in file handling!

    Once the program goes through the while loop which is checking the file existence like in this code

    Code:
    #include<iostream>
    #include<fstream>
    using name space std;
    int main()
    {
    read_emails();
    return 0;
    }
    
    void read_emails()
    {
    fstream infile;
    
    char path[100];
    cout<<"Format of the path is like this: c:/tc/test.txt";
    cout<<"Give the path of the file (*.txt or *.dat) to test: ";
    cin>>path;
    infile.open(path, ios::in);
    
    while(!infile)
    {
    cout<<"Error! can not open the file.";
    //again I prompt the user to give the path of the file 
    cout<<"Format of the path is like this: c:/tc/test.txt";
    cout<<"Give the path of the file (*.txt or *.dat) to test: ";
    cin>>path;
    infile.open(path, ios::in);
    }
    
    }
    then it continue arises the error even if we give the correct path.
    I have no idea what should be the best solution for this.

    My out put trial was

    Format of file path-> c:/file.txt

    Give the path for the file(txt or dat):c:/tc/email.txt //correct user input


    Done!

    ------------------
    LIST OF E-MAILS:
    ------------------
    <nsubedi@hotmai l.com>
    info@sunaulo.co m
    sunilraj123@hot mail.com
    sunilsthapit@ho tmail.com
    usunil45@yahoo. com
    mailsuniltyata@ yahoo.com
    sunil_sthapit@c sumb.edu
    suneeta72@redif fmail.com
    sunitasubedi@ho tmail.com

    Wrote down on the addresses.dat is completed.

    Do you want to continue?('Y' for YES of 'N' for NO): y

    Format of file path-> c:/file.txt

    Give the path for the file(txt or dat):c:/ //User gave the incorrect input for checking
    Error! opening file.

    choose the correct path
    Format of file path-> c:/file.txt
    Give the path for the file(txt or dat):c:/tc/email.txt //but this time user gave the corrct file path as user already gave before but It gives Error msg (why?)
    Error! opening file.

    choose the correct path
    Format of file path-> c:/file.txt
    Give the path for the file(txt or dat):




    y
    Error! opening file.

    choose the correct path
    Format of file path-> c:/file.txt
    Give the path for the file(txt or dat):y
    Error! opening file.

    choose the correct path
    Format of file path-> c:/file.txt
    Give the path for the file(txt or dat):y
    Error! opening file.

    choose the correct path
    Format of file path-> c:/file.txt
    Give the path for the file(txt or dat):y
    Error! opening file.

    choose the correct path
    Format of file path-> c:/file.txt
    Give the path for the file(txt or dat):y
    Error! opening file.

    choose the correct path
    Format of file path-> c:/file.txt
    Give the path for the file(txt or dat):y
    Error! opening file.

    choose the correct path
    Format of file path-> c:/file.txt
    Give the path for the file(txt or dat):c:/tc/email.txt
    Error! opening file.

    choose the correct path
    Format of file path-> c:/file.txt
    Give the path for the file(txt or dat):c:/c++/array1.txt
    Error! opening file.

    choose the correct path
    Format of file path-> c:/file.txt
    Give the path for the file(txt or dat):
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    I expect when you enter the path the first time incorrectly, the string is followed by the enter key which is still in the input buffer. The next cin hits it and returns a null path even though the correct path follows. I suggest a cin.ignore() to eat that enter key.

    Comment

    • SUNIL TYATA
      New Member
      • Feb 2010
      • 18

      #3
      I did use cin.ignore() with in while body but same problem no change...

      Comment

      • newb16
        Contributor
        • Jul 2008
        • 687

        #4
        I have a doubt about forward slashes.

        Comment

        • drhowarddrfine
          Recognized Expert Expert
          • Sep 2006
          • 7434

          #5
          @newb16 is right. The slashes are the wrong way for Windows.

          Comment

          • SUNIL TYATA
            New Member
            • Feb 2010
            • 18

            #6
            If you are talking about the forward slash for the path of the file as long as I know and it works with out any problem.

            c:/tc/email.txt
            file path is working


            But once If i give the file path like
            c=/tc/m.txt //in this file path there is something wrong in place of colon it was '=' sign.

            this is going to be incorrect and while loop certainly give error message to the user with another prompt by asking to give the correct file path.

            Even I give the correct file path this time it does not work.

            It suppose to give me correct out put rather than Error message.

            Please! need a help.

            Comment

            • newb16
              Contributor
              • Jul 2008
              • 687

              #7
              Originally posted by SUNIL TYATA
              If you are talking about the forward slash for the path of the file as long as I know and it works with out any problem.

              c:/tc/email.txt
              file path is working


              But once If i give the file path like
              c=/tc/m.txt //in this file path there is something wrong in place of colon it was '=' sign.

              this is going to be incorrect and while loop certainly give error message to the user with another prompt by asking to give the correct file path.

              Even I give the correct file path this time it does not work.

              It suppose to give me correct out put rather than Error message.

              Please! need a help.
              You need to call ios::clear for the input stream after failed open() attempt (...On failure, the failbit flag is set ...)

              Comment

              Working...