I'd like to know why a function executes fine when called from x but not when called from y

Posted by Roland on Stack Overflow See other posts from Stack Overflow or by Roland
Published on 2012-03-23T17:18:16Z Indexed on 2012/03/23 17:29 UTC
Read the original article Hit count: 223

Filed under:
|
|

When called from archive(), readcont(char *filename) executes fine! Called from runoptions() though, it fails to list the files "archived"! why is this?
The program must run in terminal. Use -h as a parameter to view the usage. This program is written to "archive" text files into ".rldzip" files. readcont( char *x) should show the files archived in file (*x)

a) Successful call

Use the program to archive 3 text files: rldzip.exe a.txt b.txt c.txt FILEXY -a archive() will call readcont and it will work showing the files archived after the binary FILEXY will be created.

b) Unsuccessful call

After the file is created, use: rldzip.exe FILEXY.rldzip -v You can see that the function crashes!

I'd like to know why is this happening!

/*
   Sa se scrie un program care:
   a) arhiveaza fisiere
   b) dezarhiveaza fisierele athivate
 */
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>

struct content{
  char *text;
  char *flname;
}*arc;

FILE *f;

void readcont(char *x){
  FILE *p;

  if((p = fopen(x, "rb")) == NULL){
    perror("Critical error: ");
    exit(EXIT_FAILURE);
  }
  content aux;
  int i;  
  fread(&i, sizeof(int), 1, p);
  printf("\nFiles in %s \n\n", x);
  while(i-- >1 && fread(&aux, sizeof(struct content), 1, p) != 0)
    printf("%s \n", aux.flname);          
  fclose(p);
  printf("\n\n");
}

void archive(int argc, char **argv){
  int i;

  char inttext[5000], textline[1000];
  //Allocate dynamic memory for the content to be archived!
  arc = (content*)malloc(argc * sizeof(content));
  for(i=1; i< argc; i++)
  {
    if((f = fopen(argv[i], "r")) == NULL){
      printf("%s: ", argv[i]);
      perror("");
      exit(EXIT_FAILURE);
    }
    while(!feof(f)){
      fgets(textline, 5000, f);
      strcat(inttext, textline);
    }
    arc[i-1].text = (char*)malloc(strlen(inttext) + 1);
    strcpy(arc[i-1].text, inttext);
    arc[i-1].flname = (char*)malloc(strlen(argv[i]) + 1);
    strcpy(arc[i-1].flname, argv[i]);
    fclose(f);
  }
  char *filen;
  filen=(char*)malloc(strlen(argv[argc])+1+7);
  strcpy(filen, argv[argc]);
  strcat(filen, ".rldzip");
  f = fopen(filen, "wb");
  fwrite(&argc, sizeof(int), 1, f);
  fwrite(arc, sizeof(content), argc, f);
  fclose(f);
  printf("Success! ");
  for(i=1; i< argc; i++)
  {

    (i==argc-1)? printf("and %s ", argv[i]) : printf("%s ", argv[i]);
  }
  printf("compressed into %s", filen);
  readcont(filen);
  free(filen);
}
void help(char *v){
  printf("\n\n----------------------RLDZIP----------------------\n\nUsage: \n\n Archive n files: \n\n%s $file[1] $file[2] ... $file[n] $output -a\n\nExample:\n%s a.txt b.txt c.txt output -a\n\n\n\nView files:\n\n %s $file.rldzip -v\n\nExample:\n %s fileE.rldzip -v\n\n", v, v, v, v);
}
void runoptions(int c, char **v){
  int i;
  if(c < 2){
    printf("Arguments missing! Use -h for help");
  }
  else{
    for(i=0; i<c; i++)
      if(strcmp(v[i], "-h") == 0){
        help(v[0]);
        exit(2);
      }
    for(i=0; i<c; i++)
      if(strcmp(v[i], "-v") == 0){
        if(c != 3){
          printf("Arguments misused! Use -h for help");
          exit(2);
        }
        else
        {
          printf("-%s-", v[1]);
          readcont(v[1]);
        }
      }
  }
  if(strcmp(v[c-1], "-a") == 0)
    archive(c-2, v);         
}

main(int argc, char **argv)
{
  runoptions(argc, argv);

}

© Stack Overflow or respective owner

Related posts about c

    Related posts about function