473,387 Members | 1,364 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Problem in file handling!

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

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<fstream>
  3. using name space std;
  4. int main()
  5. {
  6. read_emails();
  7. return 0;
  8. }
  9.  
  10. void read_emails()
  11. {
  12. fstream infile;
  13.  
  14. char path[100];
  15. cout<<"Format of the path is like this: c:/tc/test.txt";
  16. cout<<"Give the path of the file (*.txt or *.dat) to test: ";
  17. cin>>path;
  18. infile.open(path, ios::in);
  19.  
  20. while(!infile)
  21. {
  22. cout<<"Error! can not open the file.";
  23. //again I prompt the user to give the path of the file 
  24. cout<<"Format of the path is like this: c:/tc/test.txt";
  25. cout<<"Give the path of the file (*.txt or *.dat) to test: ";
  26. cin>>path;
  27. infile.open(path, ios::in);
  28. }
  29.  
  30. }
  31.  
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@hotmail.com>
info@sunaulo.com
sunilraj123@hotmail.com
sunilsthapit@hotmail.com
usunil45@yahoo.com
mailsuniltyata@yahoo.com
sunil_sthapit@csumb.edu
suneeta72@rediffmail.com
sunitasubedi@hotmail.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):
Apr 16 '10 #1
6 2620
weaknessforcats
9,208 Expert Mod 8TB
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.
Apr 16 '10 #2
I did use cin.ignore() with in while body but same problem no change...
Apr 16 '10 #3
newb16
687 512MB
I have a doubt about forward slashes.
Apr 16 '10 #4
drhowarddrfine
7,435 Expert 4TB
@newb16 is right. The slashes are the wrong way for Windows.
Apr 16 '10 #5
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.
Apr 16 '10 #6
newb16
687 512MB
@SUNIL TYATA
You need to call ios::clear for the input stream after failed open() attempt (...On failure, the failbit flag is set ...)
Apr 17 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Nimmi Srivastav | last post by:
Brief:- I am having trouble getting a FileWriter to write to a non-existent file if I instantiate the FileWriter with a String that is manipulated at run time. It works fine with a hard-coded char...
9
by: Hans-Joachim Widmaier | last post by:
Hi all. Handling files is an extremely frequent task in programming, so most programming languages have an abstraction of the basic files offered by the underlying operating system. This is...
2
by: Chris Fonnesbeck | last post by:
I thought I knew how to do error handling in python, but apparently I dont. I have a bunch of code to calculate statistical likelihoods, and use error handling to catch invalid parameters. For...
8
by: dbuser | last post by:
Hi, I need help on a problem, as described below. I am reading a file "input.txt"which has data like this: abc def gh izk lmnopq rst uvwxyz I am using fstream object to read the file and...
5
by: learner | last post by:
I have datafiles like this: 0 1941 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.02 0.00 0.00 1 0 1941 0.00 0.03 0.00 0.03 0.04 0.02 0.00 0.00 0.00 0.00 2 0 1941 0.00 0.00 0.00 0.00 0.52...
5
by: Karl | last post by:
Hi, I have some code that will save the contents of a Rich Text Box in either a Text or Rich Text Format file. The code is using the SaveFileDialog and is working correctly. I have been...
10
by: Charles Law | last post by:
For some reason, when I click the X to close my MDI parent form, the action appears to be re-directed to one of the MDI child forms, and the parent remains open. I am then unable to close the...
19
by: rmr531 | last post by:
First of all I am very new to c++ so please bear with me. I am trying to create a program that keeps an inventory of items. I am trying to use a struct to store a product name, purchase price,...
8
by: JDavis | last post by:
I am using System.Net.Sockets to connect a client socket to a server that requires three inputs when I connect: host, port and an identification number that identifies the person connecting. ...
5
by: kailashchandra | last post by:
I am trying to upload a file in php,but it gives me error msg please Help me? My Code is like below:- i have one php file named upload.php and i have another html file named upload.html and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.