Thread: More newbie question - code error

  1. #1
    sunzoner
    Guest

    Question More newbie question - code error

    I have a program that reads inputs from file and using a switch statement choose to read more information from other files.

    Although I can read the first file ("a.txt"), i can't seems to read the second file ("0.txt").
    loc is suppose to be a global variable read from a.txt and is used for loading another set of files.

    I don't know what is the problem as the text ("This is a sample text." ) in 0.txt is not loaded or displayed.

    Can anyone tell me where did I go wrong?

    Thanks in advance.

    int LoadLoc()
    {
    char desc[256];
    switch(loc)
    {
    case 1:
    cout << "Loading location. \n";
    ifstream b_file ("0.txt");
    b_file >> desc;
    cout << desc;
    b_file.close();
    break;
    /*
    default:
    cout << "Loading location. \n";
    //break;
    */
    }
    presskey();
    }

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Put some more debug lines in it and do some more error checking.
    I'm sure if you do that you will find the error yourself:
    Code:
    int LoadLoc() 
    { 
      char desc[256]; 
      ifstream b_file;
    
      cout << "LoadLoc: loc = " << loc << endl;
    
      switch(loc) 
      { 
        case 1: 
    
          // open file for reading, fails if file does not exist
          b_file.open("0.txt", ios::nocreate);
    
          if(b_file)
          {
            b_file.getline(desc, 256);
            cout << desc; 
            b_file.close(); 
          }
          else
          {
            cout << "Error: unable to open file 0.txt" << endl;
            return -1;
          }
    			
          break; 
    
        default: 
          cout << "Error: unknown location(" << loc << ")" << endl;
          return -2;
          break; 
    
      } 
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. How to monitor process creation?
    By markiz in forum Windows Programming
    Replies: 31
    Last Post: 03-17-2008, 02:39 PM
  3. file reading
    By gunghomiller in forum C++ Programming
    Replies: 9
    Last Post: 08-07-2007, 10:55 PM
  4. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM