473,387 Members | 1,569 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.

trouble opening my .txt file

Im having trouble opening the .txt file in dev-c++, I've entered the correct name for the .txt file which is saved under the same folder, but whenever I compile and run, it will just show :
"
Begin reading file.
End reading file.

--------Welcome-------
Please choose:
etc.
"
The content of the .txt file won't show.

below is part of my header file "Example.h:
Expand|Select|Wrap|Line Numbers
  1.  
  2.  class Example
  3. {
  4.       private:
  5.               char seats[4][5][11];
  6.               bool validate(char);
  7.       public:
  8.              Example(){
  9.                       readInformation();
  10.                       }
  11.              int LVL, COL, ROW;
  12.              void readInformation();
  13.              void writeInformation();
  14. };
  15. void Example :: readInformation() 
  16. {
  17.      int i, j,k;     
  18.      ifstream fin;
  19.      cout<< "Begin reading file.\n";
  20.  
  21.      fin.open("abc.txt");
  22.      if (!fin)
  23.      {
  24.                cout<<"Input file opening failed.\n";
  25.                exit(1);
  26.      }
  27.      while(!fin.eof())
  28.      {
  29.          char c;
  30.          fin.get(c);
  31.          if (validate(c)) 
  32.          {
  33.                seats[i][j][k] =c;
  34.                if(j==COL-1)
  35.                {
  36.                    j=0;
  37.                    i++;
  38.                }
  39.                else
  40.                {
  41.                    k+=1;
  42.                    i+=1;
  43.                }
  44.          }
  45.      }
  46.  
  47.      fin.close();
  48.      cout<< "End reading file\n" <<endl;
  49. }
  50.  
  51. bool Example :: validate(char c)  
  52.         if (c=='A' || c == 'B' || c == 'D' || c == 'E') 
  53.                 return true; 
  54.         else  
  55.                 return false; 
  56. }
  57.  
  58. //void Example :: writeInformation()
  59.  
and this is my cpp file:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. #include "Example.h"
  7.  
  8. int main()
  9. {
  10.     Example ex;
  11.     cout<< "-----Welcome-----\n";
  12.     int choice;
  13.     char ch;
  14.     do
  15.     {
  16.          cout<< "Please choose: \n";
  17.          cout<< "1. Display info\n 2.Make reservation\n";
  18.          cin>>choice;
  19.  
  20.          switch(choice)
  21.          {
  22.              case 1:
  23.                   ex.readInformation();
  24.                   break;
  25.              case 2:
  26.                   cout<<"make reservation\n";
  27.                   break;
  28.              default:
  29.                   cout<<"Invalid input! Enter again:\n";
  30.                   cin>>choice;
  31.          }
  32.          cout<< "Continue? (Y/N)";
  33.          cin>>ch;
  34.          if(ch == 'N' || ch == 'n')
  35.          {
  36.                cout<<"Thank you and Bye!\n";
  37.          }
  38.     }while (ch == 'Y' || ch == 'y');
  39.     system ("pause");
  40.     return 0;    
  41. }
  42.  
this is what I have in my abc.txt file:
A B B A
A B B A
A B B A
A B B A
A B B A
D B B D
D B B D
D B B D
D B B D
D B B D
D B B D
D B B D
D B B D

can anyone please help, thanks.
Apr 7 '10 #1
3 1474
newb16
687 512MB
There is no code to 'show' something when you read from the file - it just reads and stores it in seats[] array. Why must it 'show' something?
Apr 7 '10 #2
weaknessforcats
9,208 Expert Mod 8TB
exit(1) ?

Not in C++. You need to return and error code. There is no way the function that tries to open the file has any idea of what to do if it doesn't open. That may be OK or it may not depending upon the application.

Consider this code is 125 levels deep in a program of 125,000 functions running on a server with 4000 users attached and you do exit(1). Boom! There's the C:>\ and the support staff says "Gee, I wonder where that happened".

Alyways code error handlers. In this case, call the funciton in main(), check for the error code and take the action in main().
Apr 7 '10 #3
jkmyoung
2,057 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. 1. Display info:
  2. ...
  3. case 1: 
  4.                   ex.readInformation(); 
  5.  
If you type in 1, you're telling it to read in information, but never display it. What does this seat information represent anyways? You need to choose a way to display it.


On second look, the read function doesn't make much sense either. The incrementing is all wrong.
... side note: give the guy a break with the exit(1); it was probably written by a teacher or ta, and they're not taking this beyond the classroom.
Apr 7 '10 #4

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

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
3
by: tmaster1 | last post by:
Hi, This ASP (Visual Studio 6) code works on other PCs (The ASP code runs locally on localhost), but it causes IE to hang on a new PC (again, the ASP code runs locally on localhost). I think it...
2
by: Jozef | last post by:
Hello, I am trying to put together a module and open a workspace on a database that has a simple password (using Access XP). This is the lin that I'm having trouble with; Set wrk =...
6
by: Daniel Walzenbach | last post by:
Hi, I have a web application which sometimes throws an “out of memory” exception. To get an idea what happens I traced some values using performance monitor and got the following values (for...
1
by: Westbrook, Christopher L (WESTBCL04) | last post by:
I am having some trouble, and I can't figure out why. I'm trying to use the simple keylogger project from source forge, and then create a web interface to open the resulting files, so people from...
3
by: curt.bathras | last post by:
I am trying to open and read a file using the following: BufferedStream stream = new BufferedStream(File.OpenRead(aFilename)); If the file specified by aFilename is being used by another...
1
by: MLH | last post by:
Am having trouble with the filter property setting below. Would like to filter the listing to car makes beginning with "D". I'm blowing it on the filter spec somehow??? Sub OpenRecordsetX() ...
2
by: spidey12345 | last post by:
what i need this program to do is to read paragraphs like "st blah blh test ere se sit blha eere w" and then it will reformat to "st blah...
2
by: ARC | last post by:
Just curious if anyone is having issues with Acc 2007 once the number of objects and complexity increases? I have a fairly large app, with many linked tables, 100's of forms, queries, reports, and...
9
by: Matt | last post by:
Hi friends, Okay so well, I have quite a problem right now with a file stream. What I am doing is to use the Cannon SDK dlls to get control over my old Cannon A60 Camera for some surveillance...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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.