File handling error.

This is a discussion on File handling error. within the C++ Programming forums, part of the General Programming Boards category; I'm using Borland's C++ 5.5 compiler with Code::blocks.I was experimenting with file handling and used the seekg() function to move ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    4

    File handling error.

    I'm using Borland's C++ 5.5 compiler with Code::blocks.I was experimenting with file handling and used the seekg() function to move the get pointer , the program seems to compile without any errors but when I run it , it crashes.The program runs fine if I don't use seekg().Also, seekp() function works fine and the program doesn't crash.. but it doesn't seem to work.I'm using code::blocks version 10.05.I copied a program from a c++ tutorial site which demonstrates the use of seekg(), it still crashes.The exact line I typed is "fin.seekg(0,ios::beg)", is it wrong?Any help?

    Thanks.

    Code:
    #include <iostream.h>
    #include <fstream.h>
    using namespace std;
    
    
    ifstream::pos_type size;
    char * memblock;
    
    
    int main () {
      ifstream file ("example.bin", ios::binary);
      if (file.is_open())
      {
        size = file.tellg();
        memblock = new char [size];
        file.seekg (0, ios::beg);
        file.read (memblock, size);
        file.close();
    
    
        cout << "the complete file content is in memory";
    
    
        delete[] memblock;
      }
      else cout << "Unable to open file";
      return 0;
    }
    If I delete "file.seekg (0, ios::beg);", it works.
    Last edited by Sne9x; 04-15-2012 at 07:41 AM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Sne9x View Post
    If I delete "file.seekg (0, ios::beg);", it works.
    Works in what sense? Note that size is 0 in that code, which might be the cause of the crash. Try inserting:

    Code:
    file.seekg(0, ios::end);
    Before the tellg() call.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    4
    Yeah I tried that. it doesn't work.That's a sample program from some c++ site.I wrote this.
    Code:
    #include<iostream.h>
    #include<fstream.h>
    #include<string.h>
    #include<conio.h>
    void main()
    {char c[20];
    int i;
    clrscr();
    cout<<"\nPress 1 to write to the file, 2 to read.\n";
    cin>>i;
    if(i==1)
    {
        ofstream fout;
        fout.open("name.dat",ios::binary|ios::ate);
        cout<<"\nEnter the name.\n";
        cin>>c;
        fout.write((char*)&c,sizeof(c));
        fout.close();
    }
    
    
    if(i==2)
    {
        ifstream fin;;
        fin.open("name.dat",ios::binary|ios::in);
        cout<<"\nDisplaying names.";
        fin.seekg(20,ios::beg);
        fin.read((char*)&c,sizeof(c));
        cout<<c;
    }
    getch();
    }
    The file has two names and I want to read the 2nd one, so I used fin.seekg(20,ios::beg).. which makes it crash.If I remove it, it works fine , but reads the first name.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    The type of main() should be int, not void.

    Borland is a pretty old C++ compiler. You might want to try installing one of the other code::blocks compilers:

    Installing a supported compiler - CodeBlocks

    Namely, either MinGW or MSVC++, since the Digital Mars compiler is also old.

    Then you can stop using ".h" with your C++ header names, as that is incompatible with contemporary standards. Your first post actually did not crash for me after I changed that, using the g++ compiler, some recent version of which comes with MinGW. MinGW also comes with a somewhat nicer shell than the MS default.
    Last edited by MK27; 04-15-2012 at 08:18 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    4
    Yeah, planning to install a new compiler.How is it different from Borland C++ 5.5? I've been writing codes in borland c++ style and I guess this is different.
    Also, I think something's wrong with the compiler 'coz I tried the same program on Borlans C++ 4.5 on Windows 7 32 bit and seekg() worked perfectly.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Sne9x View Post
    Yeah, planning to install a new compiler.How is it different from Borland C++ 5.5?
    I've never used Borland myself, since it is older than any C++ standard (1998, 2003, 2011), but using an actively developed standard compliant compiler (g++, msvc++, intel++) should not be all that different, you won't easily find any bugs in them, and they have a much larger user base. Most of the online material you find will be standard compliant too.

    C++ - Standardization
    Last edited by MK27; 04-15-2012 at 08:30 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    4
    Most of the syntax of Borland C++ is different from MinGW, can you suggest some sites/tutorials which teach how to write code in MinGW?

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Sne9x View Post
    Most of the syntax of Borland C++ is different from MinGW, can you suggest some sites/tutorials which teach how to write code in MinGW?
    Borland can't be that weird. Basically: material you find now online is going to be standard compliant, except by mistake, and where explicitly labeled otherwise, and therefore, should work with any modern, standard compliant compiler (such as the one used by MinGW). The idea of having a standard means that code you develop on one compliant compiler will work on another, to the extent that it follows the relevant standard. Pretty much no one objects to this, and hence, C++ is now considered a standardized language.

    Alex Allain, who's responsible for this site, has a lot of tutorial material on it:
    C++ Tutorial - Learn C++ - Cprogramming.com

    The two references I primarily use are:

    Reference - C++ Reference
    C++/C++0x/C++11 reference - cppreference.com

    They first one is I think a little more in-depth, but the second one explicitly labels features that are part of the newest 2011 standard (C++11). Before applying those, you'll want to check your version of MinGW supports them:

    C++0x/C++11 Support in GCC - GNU Project - Free Software Foundation (FSF)

    To get the version, type "g++ -v" at the command-line; it should be at least 4.3. You may also have to add a compiler option in code::blocks -- but don't worry about C++11 much for now. It is mostly backward compatible C++03, which is what the compiler will use by default, and online tutorials use (again, unless they explicitly refer to C++11).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help on error handling.
    By broli86 in forum C Programming
    Replies: 9
    Last Post: 06-19-2008, 12:55 PM
  2. Error handling and file ouput
    By cunnus88 in forum C++ Programming
    Replies: 2
    Last Post: 02-03-2008, 10:04 AM
  3. Error handling...
    By XSquared in forum C Programming
    Replies: 5
    Last Post: 07-13-2003, 04:57 AM
  4. Error handling
    By mepaco in forum C++ Programming
    Replies: 12
    Last Post: 09-06-2002, 11:45 AM
  5. Add Error handling to the FAQ
    By Magos in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 04-29-2002, 04:56 AM

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