Problem with argv[]

This is a discussion on Problem with argv[] within the C Programming forums, part of the General Programming Boards category; My program is supposed to read numbers from a .txt or .bin file and then print them into the opposite ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    6

    Problem with argv[]

    My program is supposed to read numbers from a .txt or .bin file and then print them into the opposite file type. When running the program, command line is ./programname -x file1 file2 (where x is -t for reading text or -b for reading binary). The problem is when it calls for the argv[1] value of either -t or -b the program exits.
    Any suggestions so it'll actually get to the read and writing stages of the problem?
    Thanks.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void getinfo(double a[], int n, FILE *file1, FILE *file2);
    
    int main(int argc, char *argv[])
    {
      FILE *fp1, *fp2;
      double a[100];
      int i;
      if (argc != 4) {
        printf("No file names given, program exit\n"); exit(EXIT_FAILURE);
      }
    
      if (argv[1] == "-t") {
        fp1 = fopen(argv[2], "r");
        if (fp1 == NULL) { printf("Error!\n"); exit(EXIT_FAILURE); }
        fp2 = fopen(argv[3], "wb");
        if (fp2 == NULL) { printf("Error!\n"); exit(EXIT_FAILURE); }
        getinfo(a, 100, fp1, fp2);
      }
      else if (argv[1] == "-b") {
        fp1 = fopen(argv[2], "rb");
        if (fp1 == NULL) { printf("Error!\n"); exit(EXIT_FAILURE); }
        fp2 = fopen(argv[3], "w");
        if (fp2 == NULL) { printf("Error!\n"); exit(EXIT_FAILURE); }
        getinfo(a, 100, fp1, fp2);
      }
      else{
        printf("Fail.\n");
        exit(EXIT_FAILURE);
      }
      fclose(fp1);
      fclose(fp2);
      return 0;
    }
    void getinfo(double a[], int n, FILE *file1, FILE *file2)
    {
      int i;
      do {
        for(i = 0; (i < n) && (a[i] != EOF); i++) {
          a[i] = fgetc(file1);
          fprintf(file2, "%g ", a[i]);
          printf("%g", a[i]);
        }
      } while (a[i] != EOF);
    }

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    if (argv[1] == "-t")
    This is not how you compare strings in C. You would probably want to include string.h and call strcmp to check for equality.

    Also, as you seem to be programming in Linux you might want to search for examples using "getopt_long" which is built for handling command line switches.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. problem in calling DLL
    By bhagwat_maimt in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2006, 09:43 PM

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