args

This is a discussion on args within the C Programming forums, part of the General Programming Boards category; I am writing a simple file splitter, and I need to know something. I am taking several arguements in for ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    4

    args

    I am writing a simple file splitter, and I need to know something. I am taking several arguements in for the program...and they go like so.
    fsplit <split/ammend> <file> <number of times to split>
    Now, the number of files to split is handled by argv[3], obviously, but what I need to do, is have that arg be an interger rather than a character, for obvious reasons. Is there anything I can do?

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Sure,

    intval = atoi(argv[3]);

    will do it.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    4
    Ok, the program is not even close to being complete and is still very simplistic, does not even feature a file splitting function yet...but, when I run it, it gives me an error Floating Point Exception...this error is not given during compilation...but post compile. What is the deal
    [code]
    /* 6unk software file splitter */

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    FILE *fb;
    int count;
    int count_buff[100];
    char buff;
    void usage() {
    puts("Invalid usage...\nUsage: fsplit <file> <parts>\n");
    }
    int numsplit;
    int main(int argc, char *argv[])
    {
    numsplit = atoi(argv[3]);
    int aft_split;
    aft_split = (count / numsplit);
    char split_buff[count];
    if(argc < 2) {
    usage();
    }
    if((fb = fopen(argv[2], "rb")) == NULL) {
    printf("Error opening %s", argv[2]);
    }
    while((buff = getc(fb)) != EOF) {
    count++;
    }
    printf("Size of file %s = %d\n", argv[2], count);
    printf("%s split into %d parts = %d per part\n", argv[2], numsplit, (aft_split - 1));

    }
    [\code]

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Originally posted by sm00t
    Ok, the program is not even close to being complete and is still very simplistic, does not even feature a file splitting function yet...but, when I run it, it gives me an error Floating Point Exception...this error is not given during compilation...but post compile. What is the deal
    Learn to use the Preview button so your post is correctly formatted.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You shouldn't be declaring variables in the middle of a code block, so move "int aft_split;" and "char split_buff[count];" to the top of main().

    For this:
    char split_buff[count];
    count needs to be const, If you're writing C89. In other words, the value needs to be hard coded.

    If you're writing C99, then both the previous two points are irrelevant, as that standard allows for those features.

    >>if (argc < 2)
    There's no point in checking the number of arguments after you've used argv[3] here:
    >>numsplit = atoi(argv[3]);
    Always check that the correct number of args are present before accessing them.

    >>while ((buff = getc(fb)) != EOF)
    This probably won't work properly as you've declared buff as a char. It needs to be an int. Check the FAQ.

    main() needs to return something too.

    Should the usage() function terminate the program? If your code, it simply issues the message then carries on regardless.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Dec 2003
    Posts
    4
    Ok...my code is fixed, to the poin that it compiles correctly, but it does not work.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    FILE *in, *in1, *in2, *out1, *out2;
    int numsplit = 2;
    int ccount;
    char buff1;
    char buff;
    int count;
    int aftsplit;
    int usage() {
    puts("Improper usage...\nUsage: fsplit -s(plit)-a(mmend) <file(1)> (<file2>)");
    return 0;
    }
    int main(int argc, char *argv[], char *file[]) {
            if(argc < 2) {
            usage();
            }
            if(argv[1] == "-s") {
            if((in = fopen(argv[2], "rb")) == NULL) {
                    printf("Error opening %s\n", argv[2]);
            }
            if((out1 = fopen("/tmp/.out1", "wb")) == NULL) {
                    puts("Error opening /tmp/.out1");
                    }
            if((out2 = fopen("/tmp/.out2", "wb")) == NULL) {
                    puts("Error opening /tmp/.out2");
                    }
            while((buff = getc(in)) != EOF) {
                    count++;
                    }
            aftsplit = (count / numsplit);
            while(ccount <= count) {
            buff = getc(in);
            while(count <= aftsplit) {
            putc(buff, out1);
            ccount++;
            }
            putc(buff, out2);
            }
                    fclose(out1);
                    fclose(out2);
                    fclose(in);
            }
            else if(argv[1] == "-a") {
            if((in1 = fopen(argv[2], "rb")) == NULL) {
                    printf("Error opening %s", argv[2]);
                    }
            if((in2 = fopen(argv[3], "rb")) == NULL) {
                    printf("Error opening %s", argv[3]);
                    }
            if((out1 = fopen("/tmp/.out", "wb")) == NULL) {
                    printf("Error, /tmp/.out is not empty...please delete and try again\n");
                    }
            while((buff = getc(in1)) != EOF) {
                    putc(buff, out1);
            }
            while((buff1 = getc(in2)) != EOF) {
            putc(buff1, out1);
            }
            printf("Done combining %s and %s", argv[2], argv[3]);
            fclose(in1);
            fclose(in2);
            fclose(out1);
            }
    /*      else {
             if((argv[1] != "-s") && (argv[1] != "-a")) {
            puts("Error...you did not choose to either -s(plit) or -a(ppend)");
    }
    } */
            return 0;
    }

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    > if(argv[1] == "-s")
    Use
    if( strcmp(argv[1],"-s") == 0 )

    > while((buff = getc(in1)) != EOF)
    You need to make buff and buff1 of type int, not char
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer confusion
    By Blackroot in forum C++ Programming
    Replies: 11
    Last Post: 09-12-2007, 12:44 AM
  2. Replies: 2
    Last Post: 10-24-2005, 01:32 PM
  3. Replies: 1
    Last Post: 06-29-2004, 05:23 PM
  4. Multiple Command Line Args
    By mart_man00 in forum C Programming
    Replies: 10
    Last Post: 08-16-2002, 02:15 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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