Binary Input File to Array

This is a discussion on Binary Input File to Array within the C Programming forums, part of the General Programming Boards category; im trying to make a program that caputures the contents on a binary file (file.dat) and then asign all chracters ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    14

    Binary Input File to Array

    im trying to make a program that caputures the contents on a binary file (file.dat) and then asign all chracters to an array, then pass them through another function to output each charatcter

    the problem that i am having is that when i output the chracters i dont get any
    of the charachters on the file i recive just wierd icons or a letter (F) what im using to output

    puts(buffer[0]); or puts(buffer[0]) or putc(buffer[0], fp)

    is it possible to pass the array with all the file contents to another function?
    here is the code example:

    Code:
    void getWord(char* buffer) {
    
          int c, i = 0;
    
     
    
          while ((c = fgetc(fp)) != EOF)
    
                if (isalpha(c) || ispunct(c))
    
                      buffer[i++] = c;
    
                else {
    
                      buffer[i] = '\0';
    
                      return;
    
                }
    
          buffer[i] = '\0';
         Print (wbuf);   ///   Call Function Print
    
    }
    void output (wbuf [])
    {
           puts(wbuf[0]);
    }

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    You are populating "buffer" but printing "wbuf". Also, why aren't you incrementing i when you get a non-alpha, non-punct character? Or, for that matter, why even continue (unless you are printing hex values of the binary characters. Can't tell with just this snippet.)

    Todd
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > puts(wbuf[0]);
    To output the whole word use:
    puts(wbuf);
    Or:
    puts(&wbuf[0]);

    And puts() also adds a newline, so if don't want the newline, use printf().
    EDIT: Or fputs().
    Last edited by swoopy; 04-09-2008 at 05:01 PM.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Are you trying to make something like strings ?
    http://www.hmug.org/man/1/strings.php

    Anyways, puts takes a pointer to a character array with a null terminated C style string. You are passing a single char.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    14
    my mistake WBUF = BUFFER ARRAY it was A TYPO SORRRY

    Code:
    void ouput (buffer[])
    {
         puts(buffer)
    
    }
    this code prints other characters i dont know?

    what im trying to do is read from a .DAT
    file and then assign the chracters in the array BUFFER
    and then out put them one by one.
    Code:
    sam
    tom
    jake

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > Print (wbuf);
    Is this buffer also? Because you are filling buffer.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    14
    i corrected the typo its ontop of your post

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, what is the current status of your project? Is it working, not working - if it's not working, describe what is (not) happening and how that is differnet from what you expect...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    14
    what is not happening is that it is not displaying the words that i want from the array that has the values of the FILE.DAT while passing it out to an output function.



    Code:
    void ouput (buffer[])
    {
         puts(buffer)
    
    }this code prints other characters i dont know?
    
    what im trying to do is read from a .DAT
    file and then assign the chracters in the array BUFFER
    and then out put them one by one.
    Code:
    sam
    tom
    jake

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I don't see anything wrong with your getWord() function. How are you calling getWord() and are are you passing it an array or is it just a char pointer (char *)?

    I would recommend passing the file pointer into getWord(), instead of using a global variable.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    void getWord(char* buffer) {
    
          int c, i = 0;
    
     
    
          while ((c = fgetc(fp)) != EOF)
    
                if (isalpha(c) || ispunct(c))
    
                      buffer[i++] = c;
    
                else {
    
                      buffer[i] = '\0';
    
                      return;
    
                }
    
          buffer[i] = '\0';
         Print (wbuf);   ///   Call Function Print
    
    }
    Does the red line mean anything to you? [I'm assuming this is what you have, minus some typo fixes, right?]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    14
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    void getWord( char *buffer);
    void output();
    
    FILE *fp;
    
    int main()
    {
         fopen_s(&fp,"FILE.DAT","rb")
        getWord(fp);
    }
    void getWord(char *buffer)
    {
          int c, i = 0;
    
     
    
          while ((c = fgetc(fp)) != EOF)
    
                if (isalpha(c) || ispunct(c))
    
                      buffer[i++] = c;
    
                else {
    
                      buffer[i] = '\0';
    
                      return;
    
                }
    
          buffer[i] = '\0';
         Print (buffer);   ///   Call Function Print
    }
    void print(wbuf[])
    { 
         puts(buffer);
    }
    Last edited by siLent0; 04-09-2008 at 06:04 PM. Reason: typo

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Right, did you see my post? The code is (essentially) unchanged. Look at the red line and tell me what it does and when it happens.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Registered User
    Join Date
    Apr 2008
    Posts
    14
    The red line should return a NULL if the condition failed
    if not all of c is assign to buffer[i++]

    but i dont get a compile error just a logic error

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by siLent0 View Post
    The red line should return a NULL if the condition failed
    if not all of c is assign to buffer[i++]

    but i dont get a compile error just a logic error
    return a NULL? It's a void function.

    But what does "return" actually mean, in a function? How does it change the "control flow" of your program?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 05:27 PM
  2. how do you input data from a file into an array?
    By jorgejags in forum C Programming
    Replies: 5
    Last Post: 11-03-2008, 02:48 PM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  4. Deletion of input in Binary File
    By supz in forum C++ Programming
    Replies: 1
    Last Post: 04-14-2003, 05:48 AM

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