Need help.

This is a discussion on Need help. within the C Programming forums, part of the General Programming Boards category; Hello folks... I need some questions answered about a program im working on. Lemme first explain what I have... My ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    7

    Lightbulb Need help.

    Hello folks... I need some questions answered about a program im working on. Lemme first explain what I have... My program takes command line arguments. The command line arguments are names of 2 files. These files each contain letters

    file1:
    one
    two
    file2:
    three
    four

    from this I am suppose to have them print out the contents from both file1 and file2. This is easy enough.. i've done it all but the catch being I have to have the amount of memory used by the program has to be proportional to the size of the longest line of input. Well.. I've read over malloc about a hundred times and I'm still very very confused as to how to start this portion of the program. I've created the program without it being proportional by just setting a #define MAX_SIZE 80. If you could help me out that would be great!

    Greg T.
    gteng3@uic.edu

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    7

    follow up

    This is a homework assignment Im working on although im not asking for someone to write it for me.. although I've already wrote it for the most part... i just need some guidance on how to transform it to a memory proportional program.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    7

    My code thus far

    #define MAX_SIZE 80
    #include<stdio.h>
    #include<stdlib.h>


    /*Function to compare holder 1 and holder 2*/

    int comparison( char holder1[], char holder2[])
    {

    int i;
    int j;

    for( i = 0 ; i < (MAX_SIZE+1); i++ )

    {/*BeginFor*/

    if(holder1[i] < holder2[i])
    {
    return 1;
    }
    else
    {
    return 0;
    }

    }/*EndFor*/

    }






    main( int argc, char* argv[] )

    {/*Begin Main*/



    /*Declares 2 file pointers*/
    FILE* fp1;
    FILE* fp2;
    char holder1[MAX_SIZE+1];
    char holder2[MAX_SIZE+1];


    /*Initializes fp1 and fp2 for write according to command*/
    /*line arguments*/
    /*Also checks for validity of files*/
    if ( (fp1 = fopen( argv[1], "r" )) == NULL )
    {
    printf("\nCannot open the file: %s\n", argv[1]);
    }

    if ( (fp2 = fopen( argv[2], "r" )) == NULL )
    {
    printf("\nCannot open the file: %s\n", argv[2]);
    }



    /*Initializes the first lines*/
    fgets(holder1,MAX_SIZE+1,fp1);
    fgets(holder2,MAX_SIZE+2,fp2);

    /*While loop to constantly retrieve from file and compare.*/
    while(1)
    {/*Begin While*/



    /*Calls function comparison in order to compare current letters to see which is larger*/

    if( comparison(holder1,holder2) == 1 )
    {
    printf("%s", holder1);

    if( (fgets(holder1, MAX_SIZE+1, fp1)) == NULL ) {

    while( (fgets(holder2, MAX_SIZE+1, fp2)) != NULL )
    {/*BeginWhile*/
    printf("%s",holder2);
    }/*EndWhile*/
    fgets(holder1,MAX_SIZE+1,fp1);
    printf("%s",holder1);

    break;
    }

    }

    else {

    printf("%s",holder2);

    if( (fgets(holder2, MAX_SIZE+1, fp2)) == NULL ) {

    while( (fgets(holder1, MAX_SIZE+1, fp1)) != NULL )
    {/*BeginWhile*/
    printf("%s",holder1);
    }/*EndWhile*/

    fgets(holder1,MAX_SIZE+1,fp1);
    printf("%s",holder1);

    break;
    }
    }


    }/*End While*/





    fclose(fp1);
    fclose(fp2);


    }/*End Main*/

  4. #4
    Unregistered
    Guest
    You should modify your comparison function so it accepts the correct length of each array. Here, in your use, you don't _have_ to do this, but it is much safer.

    Actually, you can do it much easier if you just use 'strcmp' to compare the two words.

    if( !strcmp( str1, str2 ) ) ...strings are the same...

    Also, your function won't work correctly because you're assuming that the first one is less than the second. What happens if the second is less than the first? That is to say:

    compare( "one", "ond" )

    Here, since 'd' comes before 'e', your function would assume they're the same, because 'e' is not less than 'd'. You should do an == comparison instead. (In which case, just use strcmp.) Also, your function would fail here:

    compare( "onee", "one" );

    Again, it's much simpler to use 'strcmp'.

    Quzah.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    24
    Hmm, just a question, what are the real life applications of making memory proportional to the program's output, or is it just some gimmick your teacher is using to make the assignment harder?

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    7
    Your to assume that both files are already in lexicographic order.



    Well.. yes it was so that the program was a bit more challenging.

  7. #7
    Unregistered
    Guest
    Here's a thought: Why are you even reading entire words?
    Code:
    int skipword( FILE* fp )
    {
       char c;
       int i=0;
       while( !feof(fp) ) { c=fgetc(fp); if( isspace(c) ) break; i++; }
       return i;
    }
    
    int getwordlength( FILE* fp1, FILE *fp2 )
    {
       char fc1, fc2;
       maxLength=0,word2=0,word1=0;
    
       do
       {
          fc1 = fgetc( fp1 );
          fc2 = fgetc( fp2 );
    
          if( !isspace( fc1 ) ) word1++;
          if( !isspace( fc2 ) ) word2++;
          if( isspace( fc1 ) && !isspace( fc2 ) )
          { //word1 has ended, word2 has not
             word2+=skipword( fp2 );
             if( word2 > maxLength ) maxLength = word2;
             word1=word2=0;
          }
          else
          if( !isspace( fc1 ) && isspace( fc2 ) )
          { //word2 has ended, word2 has not
             word1+=skipword( fp1 );
             if( word1 > maxLength ) maxLength = word1;
             word1=word2=0;
          }
          // NOTE: Here, due to 'skipword', it is possible for
          // potentially odd behavior. That is to say, you can
          // no longer compare 'fc1' and 'fc2' at this point in
          // the loop, since they are not updated. Thus:
          if( isspace( fc1 ) && isspace( fc2 ) )
          { // here, we have not called skipword, if this case happens
             // however, both words are the same length
             if( word1 > maxLength ) maxLength = word1;
             word1 = word 2 = 0;
          }
       }
       while( !feof( fp1 ) && !feof( fp2 ) )
    Ok. There are flaws with this code. However, I'll let you decide if this is something you want to continue with. The basis of this example is that you can compare both of these files without even allocating any memory (aside for loop counters and crap).

    You can do it. Basicly, read both files and get the longest word length. You could do this even easier, if you just read one file at a time:
    Code:
    int wordlength( FILE*fp )
    {
       int length=0, longest=0;
       do {
          if( isspace( fgetc( fp ) ) {
             if( length > longest ) { longest=length; length = 0; }
          else
             length++;
       } while( !feof( fp ) );
       return longest;
    }
    Run the first file through this, then the second:

    l1 = wordlength( fp1 );
    l2 = wordlength( fp2 );

    longest = l1 > l2 ? l1 : l2;

    Now if you need to, you can make your buffers:

    char *buf1 = malloc( sizeof(char) *(longest+1) );
    char *buf2 = malloc( sizeof(char) *(longest+1) );

    However, you do not _need_ to allocate any buffer space if you play your cards right.

    Quzah.

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