binary files

This is a discussion on binary files within the C Programming forums, part of the General Programming Boards category; can somebody tell me why this isn't creating the file?...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    17

    binary files

    can somebody tell me why this isn't creating the file?
    Last edited by Sev3r; 06-27-2006 at 01:13 PM.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Code:
        for(i = 0; i<= length; i++)
        {
    		file[i] = *token[2];
    		*token[2]++;
        }
    In these sorts of situations, it's better to use strcpy() instead of copying the string character by character yourself. This will help you to avoid making mistakes like in the above code. This is what you meant to do:
    Code:
        char file[length+1];
        for(i = 0; i<= length; i++)
        {
    		file[i] = token[2][i];
        }

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    17
    i keep getting the seg. fault
    Last edited by Sev3r; 06-26-2006 at 10:05 PM.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    token is not a 2D array
    token is an array of what I assume are strings. You can index into a string using [].

    But if it helps you sleep better at night, do this instead:
    Code:
    file[i] = token[2] + i;

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    i keep getting the seg. fault
    Are you sure the data that is getting passed to the function is valid? Does token[2] point to a NULL terminated string?

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    17
    The input is tokenized with strtok() with the space delimeter
    token [2] is the 3rd token in the input.
    so it does not have the '\0' im guessing.

    does it need it?

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Are you sure you used strtok() correctly?

    And to answer your question: yes, token[2] needs to be null terminated. If it isn't then
    Code:
    int length = strlen(token[2]);
    will give you a seg fault.

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    17
    check your pm for full program

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    17
    ah success!
    forget the \n as a delimeter in my other function

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 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
  2. send/recv binary files using sockets in C++
    By dafatdude in forum Networking/Device Communication
    Replies: 14
    Last Post: 07-25-2004, 11:00 AM
  3. MFC: CStrings & binary files question(s)
    By BrianK in forum Windows Programming
    Replies: 0
    Last Post: 06-24-2004, 05:41 PM
  4. Binary files
    By Brian in forum C Programming
    Replies: 2
    Last Post: 02-18-2002, 12:13 PM
  5. storing string objects to binary files
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-06-2001, 11:33 PM

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