Error opening files!!! :-(

This is a discussion on Error opening files!!! :-( within the C++ Programming forums, part of the General Programming Boards category; Hi everyone! Using Linux with NFS, I run this program and it crashes when trying to open the file "grafo4.grf", ...

  1. #1
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244

    Error opening files!!! :-(

    Hi everyone!
    Using Linux with NFS, I run this program and it crashes when trying to open the file "grafo4.grf", a file that exists. The real code is different, I just left the parts where errors occur.
    Could anyone give me a pointer to a solution at least?
    As always, thanks all replys!
    Here is the code:
    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main(){
      
      char file_name[10];
      ifstream input;
      int fileNumber = 0;
      bool done = false;
    
      cout << "Check 1 " << endl;
      while(!done){
        sprintf(file_name, "%s%d%s", "grafo", fileNumber, ".grf");
        input.open(file_name);
        if(!input) done = true;
        else fileNumber++;
        input.close();
      }
    
      cout << "Check 2 " << endl;
      /* abre o arquivo correto */
      sprintf(file_name, "grafo%d.grf",--fileNumber);
      input.open(file_name);
      if(!input){
        cerr << "Error opening file " << file_name << endl;
        return 1;
      }
      input.close();
    
      cout << "Check 3 " << endl;
      for(int i=0;i<=fileNumber;i++){
        sprintf(file_name, "grafo%d.grf", i);
        input.open(file_name);
        if( !input ){
          cerr << "Error opening file " << file_name << endl;
          return 1;
        }
        input.close();
      }
    
      cout << "Check 4 " << endl;
      input.open("corte.crt");
      if(!input){
        cerr << "Error opening file " << file_name << endl;
        return 1;
      }
      input.close();
      cout << "Ok =D" << endl;
      return 0; 
    
    }
    Nothing more to tell about me...
    Happy day =)

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    > sprintf(file_name, "%s%d%s", "grafo", fileNumber, ".grf");
    Well you only allocated 10 chars for filename, and the fixed part of the string is 10 chars before you add any digits to it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ressources files
    By mikahell in forum Windows Programming
    Replies: 4
    Last Post: 06-19-2006, 06:50 AM
  2. add source files to embedded VC 4.0
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2006, 03:28 AM
  3. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 04:45 AM
  4. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 10:46 AM
  5. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 01:41 PM

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