Thread: problem with a small c program

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    45

    problem with a small c program

    hi,

    im processing a binary file which contains 1,249,820 data points, and since the size of the file is 1,249,820 bytes, im assuming each data point uses 1 byte of memory. basically, im running through every point, and which ever point that is >= 50, i will set that point to 0 (each point runs from 0-255). so i hav written sth like

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main()
    {
    FILE *fp3;
    fp3 = fopen("xxx\\b3", "rb");
    
    FILE *fp3_o;
    fp3_o = fopen("xxx\\b3_o", "wb");
    
    long int i, size = 1249820;
    char *b3_dn = malloc(size * sizeof(char));
    
    fread(b3_dn, sizeof(char), size, fp3);
    
    for (i=0; i<size; i++){
    if(b3_dn[i] >= 50) b3_dn[i] = 0;
    }
    
    fwrite(b3_dn, sizeof(char), size, fp3_o);
    
    free(b3_dn);
    
    return 0;
    }
    when i run it, however, the results are not wat i expect, ie the values which are >= 50 remain unchanged, many values <50 become 0, and basically values are randomly changed to 0 all over. can someone help?

    btw, char takes 1 byte and int takes 4 byte on my system.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    post a sample of the input data

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    45
    its a whole table of numbers which range from 0-255 whereby each number is separated by a tab. see attached file for a small sample (its not the entire file). i saved it into a .txt file using a binary viewer cos c board does not allow uploading of binary files

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You shouldn't read text files using "rb".

    --
    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
    Nov 2008
    Posts
    45
    i thought u have to use that if u r reading a binary file? can u post a simple example of how i can go about solving this problem? thx so much in advance

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, but your test.txt is not a binary file, is it?
    fread() is also the wrong way to read data in from a text file, you should be using fscanf() or some such.

    And could you please use full english words?

    --
    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.

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    45
    sorry about the abbreviated words...i am too used to typing in chat language.

    the attached file that i have uploaded is a .txt file because we cant upload a binary file onto c board. so, actually i have to work with binary files and not .txt files. would be grateful for any sample..

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    so, you wrote a program to read the binary file and produce the text file - if so, why can't you produce the ? Or is the sample you posted not a REAL sample, but some completely different file? [Note that the distinction between text and binary file is mainly based on how newline is handled - all files are actually binary in the sense that data is ALWAYS, at ALL times stored in binary form in files (at least until we get to the "magnetic stored in the actual media")].

    If your file is just renamed to "txt" with no other processing, then you DO have a text-file [or you have ONLY data that happens to be in the a very selected range of 10, 13, 32, 48-57 in binary, in patterns that appear to form numbers in the range 80-90.

    --
    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
    Nov 2008
    Posts
    45
    mats, i am not sure you have understood my question entirely: i have to process a binary file element by element, and write the processed outcome to an output binary file. the file that i uploaded is NOT the original data file because we cannot upload binary files onto this forum. so, i have to use a software that can view binary files and export them to .txt format so that i can upload it here.

    and no, i cannot simply rename my binary files to .txt files because that does not help anything. i still need to work with binary files.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I know what you mean, may I suggest an alternative, however?

    Upload a sample of the file, as a binary file, to MegaUpload or any free file sharing depot you like. Then post the url to that uploaded file, (that they will give you), in this thread.

    I'll be done with this computer in an hour or less, and I can take a look at it, then. I have your program already, but I need the binary file to test my idea to fix it.

    It's frustrating, but hang in there.

  11. #11
    Registered User
    Join Date
    Nov 2008
    Posts
    45
    hi adak, thx.

    i have uploaded a test binary file to MEGAUPLOAD - The leading online storage and file delivery service. its not the entire file, just a small subset of it.

    please take a look.

  12. #12
    Registered User
    Join Date
    Nov 2008
    Posts
    45
    apparently the forum has some blocking mechanism in place..

    the url is [http://] [www.megaupload.com] [/?d=8E1UMY4F]

    hope this works

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I can follow the link, but not download your test-file.

    Why don't you just rename the file to something.bin.txt or such? [That's what I did with the zip-file for the minibasic project - the upload manager just checks the extension, not the actual content].

    --
    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
    Sep 2006
    Posts
    8,868
    This is the full link to the binary file:

    MEGAUPLOAD - The leading online storage and file delivery service

    That site has become more irritating with the wait and hard to read captcha, but it's free, and I did get the fie.

  15. #15
    Registered User
    Join Date
    Nov 2008
    Posts
    45
    ok here it is. i have just simply added a '.txt' to the end of the binary file...not sure if that will affect anything. thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [NEED HELP] I just can't solve this program problem!
    By frodonet in forum C Programming
    Replies: 7
    Last Post: 09-23-2007, 02:44 PM
  2. Problem with simple XOR program
    By spike_ in forum C++ Programming
    Replies: 8
    Last Post: 08-17-2005, 12:09 AM
  3. Small Problem that could be a big problem
    By sytaylor in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2004, 09:49 AM
  4. simple frontend program problem
    By gandalf_bar in forum Linux Programming
    Replies: 16
    Last Post: 04-22-2004, 06:33 AM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM