Thread: Characters

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    101

    Characters

    Hello,
    I am trying to make a program that will open up as many files as the user specifies in the command line (argv) and compare each file to see which has the most characters(bytes) and which file has the most words inside of it. I am having trouble figuring out how to manipulate the argv array so that i can accompany many user inputted filenames. i was thinking argv[i] and creating a loop, but i do not think that it works. Anyway can someone help me out here?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int i;
    int main(int argc,char **argv[])
    {
      char name[100],name2[100],d;
      int c,nw[w],w,numfile;
      FILE *f[n],*g;
      n=0;
      numfile=argc;
      for (i=1;argv <= numfile;i++){
        f[n]=fopen(argv[i],"r");
        if(f[n]==NULL){
          printf("Can't open %s \n",argv[i]);
          exit(0);
        }
      }
      // fgetc(f[n]);
      // while(c != EOF){
      //fputc(c,g);
      // c=getc(f);
        
    
    
        
    
    
      
     
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I'm pretty sure that argv[i] and creating a loop is going to work. You can't create nw[w] if you don't know what w is, nor f[n] if you don't know what n is.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Put all the code to open, process, and close a file inside a loop. Loop through argv[i] (you were close) from 0 to argc. That should do it!

    You're gonna run into problems if you try and open an undetermined amount of files. If that's what you'd really like to do, look into malloc() and dynamic memory allocation. But for practical purposes (depending on what you're doing to the files), I would just open one, process it, close it, move to the next one...

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    its not liking this line..
    Code:
     for (i=1;argv[i] <= argc;i++)
    im gettting this error in the terminal when compiling
    Code:
    2-9.c: In function ‘main’:
    2-9.c:12: warning: comparison between pointer and integer

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Lucid15 View Post
    its not liking this line..
    Code:
     for (i=1;argv[i] <= argc;i++)
    im gettting this error in the terminal when compiling
    Code:
    2-9.c: In function ‘main’:
    2-9.c:12: warning: comparison between pointer and integer
    OMT:
    Loop through argv[i] (you were close) from 0 to argc.


    test if i < argc
    Last edited by Adak; 02-09-2009 at 09:25 PM.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Wny not
    Code:
    for (i=1; i < argc; i++)
    instead?

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    still getting the same warning... i wnat argv at 1. i compile with ./a.out then followed by the inputted file names. So i would start reading at argv[1].

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    here is my unfinished code so far...
    Code:
    //Compares upto 100 files, and decides which one has the most characters(bytes) and which has the most words.
    #include <stdio.h>
    #include <stdlib.h>
    int i;
    int main(int argc,char **argv)
    {
      char name[100],name2[100],d;
      int c,w,n,numc,numw,numtotal;
      FILE *f[n],*g;
      n=0;
      numw=0;
      numtotal=0;
      for (i=1;argv[i] < argc;i++){
        f[n]=fopen(argv[i],"r");
        if(f[n]==NULL){
          printf("Can't open %s \n",argv[i]);
          exit(0);
        }
        else{
          fgetc(f[n]);
          while(c != EOF){
    	if(c==' '){
    	  numw++;
    	}
    	c=getc(f[n]);
          }
          if (c > numc){
    	c=numc;
          }
          if (numw > numtotal){
    	numw=numtotal;
          }
    
        }
        n++;
        exit(1);
      }
      }

  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    The problem before (and now) was that you were comparing
    Code:
    argv[i] < argc
    argv [ anything ] is a string pointer. argc is an integer - you can't compare them. But since argc is the number of elements in the array argv, it is an integer, and so you can compare them. You should be saying
    Code:
    i < argc
    .

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Lucid15 View Post
    still getting the same warning... i wnat argv at 1. i compile with ./a.out then followed by the inputted file names. So i would start reading at argv[1].
    But you have to test if i < argc, not argv[i] < argc.

    Beyond that, I'd need to see your changed code.

  11. #11
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    I also need to add in that i would like my program to print the file name that has the most characters and/or words and if there are some that are equal print ALL of those. Im confused now..

  12. #12
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    Here is my FIXED code as of NOW..
    Code:
    //Compares upto 100 files, and decides which one has the most characters(bytes) and which has the most words.
    #include <stdio.h>
    #include <stdlib.h>
    int i;
    int main(int argc,char **argv)
    {
      char name[100],name2[100],d;
      int c,w,n,numc,numw,numtotal;
      FILE *f[n],*g;
      n=0;
      numw=0;
      numtotal=0;
      for (i=1;i < argc;i++){
        f[n]=fopen(argv[i],"r");
        if(f[n]==NULL){
          printf("Can't open %s \n",argv[i]);
          exit(0);
        }
        else{
          fgetc(f[n]);
          while(c != EOF){
    	if(c==' '){
    	  numw++;
    	}
    	c=getc(f[n]);
          }
          if (c > numc){
    	c=numc;
          }
          if (numw > numtotal){
    	numw=numtotal;
          }
    
        }
        n++;
        exit(1);
      }
    
      }

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You'll have to add a char array to hold the longest file name you've found so far. I'd be tempted to use a struct with members like:

    longest file name
    file with the most chars
    int max_char
    filename with the most words
    int max_words

    I'm thinking of a way to do the "any that are equal part", but it hasn't coalesced yet.

    OK. I'd make a struct for each file, with info for it like char_num, word_num, filename, etc. Then make an array of those structs, and when you're done, sort that array for whatever char, word, filename length, etc. Any equal files will be obvious as you run through the sorted array of structs.

    I mean the array of structs will be sorted for filename length, then resorted for most words, then most chars, and etc. So you sort the array, then you'll resort it by another key, then resort it by yet another key, etc.
    Last edited by Adak; 02-09-2009 at 09:51 PM.

  14. #14
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    hmm...thank you. any more input? im still confused as all hell

  15. #15
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    when i try to run the program to make it just tell me how many characters are in a file i get a segmentation fault...
    Code:
    //Compares upto 100 files, and decides which one has the most characters(bytes) and which has the most words.
    #include <stdio.h>
    #include <stdlib.h>
    int i;
    int main(int argc,char **argv)
    {
      int c,w,n,max_char[n],max_words[n],numtotal,numwords;
      char most_words,most_chars;
      FILE *f[n],*g;
      n=0;
      numtotal=0;
      for (i=1;i <= argc;i++){
        f[n]=fopen(argv[i],"r");
        if(f[n]==NULL){
          printf("Can't open %s \n",argv[i]);
          exit(0);
          break;
        }
        else{
          fgetc(f[n]);
          while(c != EOF){
    	if(c==' '){
    	  numwords++;
    	}
    	c=getc(f[n]);
          }
         
        }
        n++;
        exit(1);
      }
      printf("%d",c);
    
      }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. HELP!!!!emergency ~expert please help
    By unknowppl in forum C Programming
    Replies: 1
    Last Post: 08-19-2008, 07:35 AM
  3. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  4. How do you check how many characters a user has entered?
    By engstudent363 in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 06:05 AM
  5. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM