Bin Files

This is a discussion on Bin Files within the C++ Programming forums, part of the General Programming Boards category; i need to know whats wrong with this code.... its out of the book im reading... and ... well it ...

  1. #1
    Registered User PanzTec's Avatar
    Join Date
    Sep 2004
    Posts
    24

    Bin Files

    i need to know whats wrong with this code.... its out of the book im reading... and ... well it wont work for me...

    Code:
    #include <iostream.h>
    #include <fstream.h>
    using namespace std;
    
    int get_int(int default_value);
    char name[20];
    
    int main() {
    	char filename[81];
    	int n;
    	int age;
    	int recsize = sizeof(name) + sizeof(int);
    
    	cout<<"Enter file name: ";
    	cin.getline(filename, 80);
    
    	fstream fbin(filename, ios::binary | ios::init | ios::out);
    	if (!fbin) {
    		cout << "Could not open file " << filename;
    		return -1;
    	}
    
    	cout << "Enter file record number: ";
    	n = get_int(0);
    
    
    
    	cout << "Enter name: ";
    	cin.getline(name, 19);
    	cout << "Enter age: ";
    	age = get_int(0);
    
    	fbin.seekp(n * recsize);
    	fbin.write(name, 20);
    	fbin.write(reinterpret_cast<char*>(&age),
    		sizeof(int));
    	fbin.close();
    	return(0);
    }
    
    
    int get_int(int default_value) {
    	char s[81];
    
    	cin.getline(s, 80);
    	if (strlen(s) == 0)
    		return default_value;
    	return atoi(s);
    }
    The Matrix Will Live Again!

  2. #2
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    What errors do you get?

    In any case you shouldn't use iostream.h and fstream.h but iosteam and fstream as your includes.

  3. #3
    Registered User PanzTec's Avatar
    Join Date
    Sep 2004
    Posts
    24
    Code:
    --------------------Configuration: Bin - Win32 Debug--------------------
    Compiling...
    Bin.cpp
    D:\UFS\NETWORK PROGRAMMING\Bin\Bin.cpp(3) : error C2871: 'std' : does not exist or is not a namespace
    D:\UFS\NETWORK PROGRAMMING\Bin\Bin.cpp(17) : error C2676: binary '|' : 'enum ios::open_mode' does not define this operator or a conversion to a type acceptable to the predefined operator
    D:\UFS\NETWORK PROGRAMMING\Bin\Bin.cpp(46) : error C2065: 'strlen' : undeclared identifier
    D:\UFS\NETWORK PROGRAMMING\Bin\Bin.cpp(48) : error C2065: 'atoi' : undeclared identifier
    Error executing cl.exe.
    
    Bin.exe - 4 error(s), 0 warning(s)
    The Matrix Will Live Again!

  4. #4
    C++ Learner :D
    Join Date
    Mar 2004
    Posts
    69
    include your header files like this:

    Code:
    #include<iostream>
    #include<fstream>



    edit: sorry I just realised that sigfriedmcwild had already said it

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Quote Originally Posted by PanzTec
    D:\UFS\NETWORK PROGRAMMING\Bin\Bin.cpp(3) : error C2871: 'std' : does not exist or is not a namespace
    This is due to the iostream.h/fstream.h vs iostream/fstream header issues mentioned in sigfriedmcwild's post.

    Quote Originally Posted by PanzTec
    D:\UFS\NETWORK PROGRAMMING\Bin\Bin.cpp(46) : error C2065: 'strlen' : undeclared identifier
    strlen is declared in the cstring header... so you will need to #include that one as well as the others.

    Quote Originally Posted by PanzTec
    D:\UFS\NETWORK PROGRAMMING\Bin\Bin.cpp(48) : error C2065: 'atoi' : undeclared identifier
    atoi would be declared in either the cstdlib or cmath headers so you would need to include one of those headers.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User PanzTec's Avatar
    Join Date
    Sep 2004
    Posts
    24
    sweet!!! that almost worked
    one last problom...
    Code:
    --------------------Configuration: Bin - Win32 Debug--------------------
    Compiling...
    Bin.cpp
    d:\ufs\network programming\bin\bin.cpp(17) : error C2676: binary '|' : 'enum std::ios_base::_Openmode' does not define this operator or a conversion to a type acceptable to the predefined operator
    Error executing cl.exe.
    
    Bin.exe - 1 error(s), 0 warning(s)
    The Matrix Will Live Again!

  7. #7
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    This doesn't look like it's the cause of that error but I think you meant "ios::init" to be "ios::in". Aside from that it generates no errors with my Visual-I compiler [although I prefer using ios_base instead of ios and instead of checking `if (!fbin)' checking `if (!fbin.is_open())'].

  8. #8
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Hello,

    I believe LuckY pointed out the problem. Though, for future reference:


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  9. #9
    Registered User PanzTec's Avatar
    Join Date
    Sep 2004
    Posts
    24
    yeah he idi... now i feel really dumb... lol thanks lucky
    The Matrix Will Live Again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading .dat files from a folder in current directory...
    By porsche911nfs in forum C++ Programming
    Replies: 7
    Last Post: 04-04-2009, 09:52 PM
  2. 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
  3. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 01:41 PM
  4. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  5. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM

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