Parsing and Fwrite Help Please.

This is a discussion on Parsing and Fwrite Help Please. within the C Programming forums, part of the General Programming Boards category; Hey peoples... This is a little program i use frequently.. But i need it to add further function. At the ...

  1. #1
    CNewbie
    Join Date
    Nov 2005
    Location
    Oz
    Posts
    31

    Parsing and Fwrite Help Please.

    Hey peoples...

    This is a little program i use frequently..
    But i need it to add further function.
    At the moment, it prints the correct numbers
    to a console window.

    But XPPro ........s itself everytime i try and cut and paste from that window to a text file..... (works maybe 40% of the time)

    So i'd like to add two things to this code...
    It needs to write the numbers to a text file (rather than to screen) can i just change the last printf to fwrite test.txt
    or something...

    I also want to add a comma after every number....
    for example.. (like parsing in my original script version)
    so i can use the resulting numbers in a array.

    Code:
    #include <stdio.h>
    #define MAX_LINE_LEN   256
    #define BYTES_PER_LINE 16
    main (argc,argv)int argc;char* argv[];
    {
        char  filename [MAX_LINE_LEN];
        FILE  *file;
        char  bytes[BYTES_PER_LINE];
        int   i, j;
        int   filename_provided=0;
        int   filesize;
        int   bytes_read=0;
        long  pos=0L;
        int   data_only=0;
        int   Dunno_X=0;   
     {
    		if (!strcmp(argv[1],"-d")) {data_only=1;}strcpy(filename,argv[2]); filename_provided=1;}
    		if (filename_provided)file=fopen(filename,"rb");/* Open the input file */    {}{{}}
       		while (!feof(file)){bytes_read=fread(bytes,1,BYTES_PER_LINE,file);
           	for (i=0; i<BYTES_PER_LINE; i++){
            if (file == stdin) /* If the value is stdin, we require special processing */           {{
            if (data_only){}
    		else{}}}{
            if((i<bytes_read) && ((filesize-1) != (pos-bytes_read+i))){
            if (Dunno_X) {}
            else {printf("%02d ",bytes[i] &0xff);}}           
    		else {}{}}
            if (!data_only) {}}{}
    		printf("\n");    }
            fclose(file);
        return 0;}

    And no this is not homework...


    So at the moment i use the console.

    Numbers -d test.ixm
    70 79 82 77 00 00 17 114 76 87 79 50 84 65 71 83
    00 00 00 18 70 108 111 111 114 00 84 104 117 110 100 101

    So i'd like it to look like this......
    70, 79, 82, 77, 00, 00, 17, 114, 76, 87, 79, 50, 84, 65, 71, 83,
    00, 00, 00, 18, 70, 108, 111, 111, 114, 00, 84, 104, 117, 110, 100, 101

    And then have them in a .txt file instead of the console...
    Any help, on making that happen is appreciated.

    Thanks again..

    Cheers,
    Scott.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What in the hell is that?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    CNewbie
    Join Date
    Nov 2005
    Location
    Oz
    Posts
    31
    Am i posting in the correct place?

    It simply reads a file as binary and gives me the decimal
    output (as i need for another program i'm making)

    I just need it to add a comma and print to a text file instead.

    I think i can handle the writing to a textfile bit with Fwrite...
    But parsing the correct amount of bytes. is a little over my head...

    The only reason i'm learning C (not wanting too)
    is to to make this tool that i use every day....

    So even to write that, i have C-Burnout....
    I have the shakes!

    So i hope someone else can help me connect the rest of the dots.

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    you have a horriable alignment cant make out what u are doing.

    ssharish2005

  5. #5
    CNewbie
    Join Date
    Nov 2005
    Location
    Oz
    Posts
    31
    Not much i can do about that....
    It's likely the most readable C, i can read....
    It should compile fine BTW....
    (Feel free to rewrite it neater if you can)

    In that case, maybe try to forget my code and look at the my question or example instead.

    At the moment it reads a file and produces the output as decimal.

    i.e

    Numbers.exe -d test.xxx
    will look at test.xxx and display on screen the decimal it contains..
    How does one add a comma after every 000 digit?

    I'm not sure i can explain it any better...
    It's a fairly simple command line program...

    I figured the programming prowess here, would instantly be able to work it out... But thanks anyway.

  6. #6
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    You have seemingly random indentation and redundant blocks (curly brackets).

    Here's what your code looks like when passed through an indentation program:
    Code:
    char *argv[];
    {
        char filename[MAX_LINE_LEN];
        FILE *file;
        char bytes[BYTES_PER_LINE];
        int i, j;
        int filename_provided = 0;
        int filesize;
        int bytes_read = 0;
        long pos = 0L;
        int data_only = 0;
        int Dunno_X = 0;
        {  /* redundant */
            if(!strcmp(argv[1], "-d"))
            {
                data_only = 1;
            }
            strcpy(filename, argv[2]);
            filename_provided = 1;
        }  /* redundant */
        if(filename_provided)
            file = fopen(filename, "rb");   /* Open the input file */
        {  /* redundant */
        }  /* redundant */
        {  /* redundant */
            {  /* redundant */
            }  /* redundant */
        }  /* redundant */
        while(!feof(file))
        {
            bytes_read = fread(bytes, 1, BYTES_PER_L INE, file);
            for(i = 0; i < BYTES_PER_LINE; i++)
            {
                if(file == stdin)   /* If the value is stdin, we require special processing */
                {  
                    {  [color=red] /* this whole file == stdin block does nothing */
                        if(data_only) /* redundant */
                        { /* redundant */ 
                        } /* redundant */
                        else /* redundant */
                        { /* redundant */
                        } /* redundant */
                    }
                }
                {
                    if((i < bytes_read)
                       && ((filesize - 1) != (pos - bytes_read + i)))
                    {
                        if(Dunno_X)
                        { /* redundant */
                        } /* redundant */
                        else
                        {
                            printf("%02d ", bytes[i] & 0xff);
                        }
                    }
                    else /* redundant */
                    { /* redundant */
                    } /* redundant */
                    { /* redundant */
                    } /* redundant */
                }
                if(!data_only) /* redundant */
                { /* redundant */
                } /* redundant */
            }
            { /* redundant */
            } /* redundant */
            printf("\n");
        }
        fclose(file);
        return 0;
    }
    If you expect people to make the effort to try to debug your code, format it in a manner such that it's readable.

    Update: here is the code formatted nicely with the redundant constructs removed if anyone can be bothered debugging it.

    Code:
    #include <stdio.h>
    #define MAX_LINE_LEN   256
    #define BYTES_PER_LINE 16
    /* K&R1 declarations went out of fashion in about 1987 */
    /*main (argc,argv)
    int argc;
    char *argv[];
    */
    int main(int argc, char *argv[])
    {
        char filename[MAX_LINE_LEN];
        FILE *file;
        char bytes[BYTES_PER_LINE];
        int i, j;
        int filename_provided = 0;
        int filesize;
        int bytes_read = 0;
        long pos = 0L;
        int data_only = 0;
        int Dunno_X = 0;
        if(!strcmp(argv[1], "-d"))
        {
            data_only = 1;
        }
        strcpy(filename, argv[2]);
        filename_provided = 1;
    
        if(filename_provided)
            file = fopen(filename, "rb");   /* Open the input file */
    
        while(!feof(file))
        {
            bytes_read = fread(bytes, 1, BYTES_PER_LINE, file);
            for(i = 0; i < BYTES_PER_LINE; i++)
            {
                    if((i < bytes_read)
                       && ((filesize - 1) != (pos - bytes_read + i)))
                    {
                        if(!Dunno_X)
                        {
                            printf("%02d ", bytes[i] & 0xff);
                        }
                    }
            }
            printf("\n");
        }
        fclose(file);
        return 0;
    }
    Last edited by cwr; 12-05-2005 at 06:48 PM.

  7. #7
    Logic Junkie
    Join Date
    Nov 2005
    Posts
    31
    Well, adding a comma - just print it after each number.

    this:
    printf("%02d ", bytes[i] & 0xff);

    becomes:
    printf("%02d, ", bytes[i] & 0xff);

    and as for printing to a file, open the file in append mode and write to there with fprintf(filename, //same as above);

  8. #8
    CNewbie
    Join Date
    Nov 2005
    Location
    Oz
    Posts
    31
    Sorry i'm a newbie,
    I made an effort to post what i had because otherwise i'm accussed on this forum of expecting others to write code for me.

    So i made the effort to post the working compiled C code
    that works without a problem in DevC/C++
    Just to avoid that crits.... I can't seem to win on this forum.

    Now it's not neat enough for people to help me... Than don't, maybe just offer some help on parsing or fwrite? or skip my message all together if it's that much trouble.

    I didn't ask anyone to write or rewrite anything.... (just for the record)

    I do appreciate your help CWR on doing so, and pointing out to me the issues with readability....

    To me the actual code still does the exact same thing. (but i now understand that to you people, a curly bracket means the difference of if you can't or can understand what i'm saying)

    Thanks to everyone for their....errr help..... i think..


    EDIT: Silfer - Thanks a lot!

    Thanks.... That was the kind of thing i was hoping for..... I really didn't think it was too difficult... It's just hard the more you look at the code, the less you see.. Appreciate the feedback.
    Last edited by Freestyler; 12-05-2005 at 07:31 PM.

  9. #9
    Logic Junkie
    Join Date
    Nov 2005
    Posts
    31
    The code does the same thing with formatting, yes, but it is readable. If Istart wrting lk th is, and persist at it for a long time, would you care to read my post? Neither would we.

  10. #10
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Just to avoid that crits.... I can't seem to win on this forum.
    It's not about winning, it's about learning. When people stop critizising you, you stop learning. Quzah might be a bit harsh at times, but most likely he's 100% right and if you manage to look through his sometimes rude words, he'll teach you a lot.

    When you progress in programming, creating a program that works becomes less of a priority and more of a standard. A program that works however is only half completed. It also has to be readable and simple. Not because some unknown guy on the internet likes it that way, but because we look at your program the same way you will look at it a year from now. You will have forgotten anything you knew about it. You will start like we do, from scratch with just your code. If that code looks clean and easy to understand, we ( and more important: *you* ) will be able to make changes to it. If it's unneccessary hard to understand, it's hard to make changes up to a point where you will simply rewrite all of it because that seems easier than understanding it.

    Be happy you get critizised, it's the only way to learn. Some people are polite, some people are harsh. Back in school I have to admit I liked the polite teachers a lot more mostly because I got better grades while being lazy. Looking back, the harsher teachers taught me more and were more effective in their methods. With hindsight, I think the teachers that were a pain in the behind were the better teachers.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  11. #11
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Quote Originally Posted by nvoigt
    It's not about winning, it's about learning. When people stop critizising you, you stop learning. Quzah might be a bit harsh at times, but most likely he's 100% right and if you manage to look through his sometimes rude words, he'll teach you a lot.

    When you progress in programming, creating a program that works becomes less of a priority and more of a standard. A program that works however is only half completed. It also has to be readable and simple. Not because some unknown guy on the internet likes it that way, but because we look at your program the same way you will look at it a year from now. You will have forgotten anything you knew about it. You will start like we do, from scratch with just your code. If that code looks clean and easy to understand, we ( and more important: *you* ) will be able to make changes to it. If it's unneccessary hard to understand, it's hard to make changes up to a point where you will simply rewrite all of it because that seems easier than understanding it.

    Be happy you get critizised, it's the only way to learn. Some people are polite, some people are harsh. Back in school I have to admit I liked the polite teachers a lot more mostly because I got better grades while being lazy. Looking back, the harsher teachers taught me more and were more effective in their methods. With hindsight, I think the teachers that were a pain in the behind were the better teachers.
    I completely agree. I wouldn't have passed my Geography GCSE if it wasn't for my teacher chasing me around (even pulling me out of assembly's!) making sure I had my coursework up to scratch. My Computing lecturer I have in College is extremely strict and punishing, but hell, she's the best lecturer I have.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,488
    > Not much i can do about that....
    Yes there is - study how others format their code, and learn.

    > It's likely the most readable C, i can read....
    Then you've already lost the game.

    > It should compile fine BTW....
    Compiler's don't give a rats-ass about formatting - just look at the crazy formatting of some IOCCC entries.

    > (Feel free to rewrite it neater if you can)
    Nope, that's your job.

    If you formatted code like that in a work environment, you'd be sacked within a week.

Popular pages Recent additions subscribe to a feed

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