Thread: Reading lines from a file

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    67

    Reading lines from a file

    I want to compare two text files and output the lines that are different + the number of the line where two lines are different.The lenght of the line is limited to 80 characters. The two text files are passed as a command line arguments.

    Here is my code, the thing compiles ok, but the result's are not what I have expected. The program I tried to write was to read only 10 lines and compare them I don't know yet how to compare the lines until the files reach their end...gotta do something with EOF constant...okej here is my code.

    Code:
     #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    int main (int argc, char *argv[]) {
    
      char line1[]=""; //here wil be stored the lines of a first file
      char line2[]=""; //..of the second one
      int line_number=0;
      
      FILE *dat1, *dat2;
    
      if(argc<3){
        printf("You have to input two arguments!\n");
        exit(1);
      }
    
      dat1=fopen(argv[1],"r"); //opening each file for reading
      dat2=fopen(argv[2],"r");
     
      while(line_number<=10){ 
    
         fgets(line1,80,dat1); //gets the first 80 characters of a line in dat1 and 
                                           //stores them in line1
         fgets(line2,80,dat2); //same here ...stores the line from dat2 into line2
        if(strcmp(line1,line2)!=0) //compare two strings, if they are not equal...
          { 
    	printf("These lines don't match:\n");
            line_number++;
            printf("%s   AND %s \t at line number %d\n",line1,line2,line_number); 
          }
    
        else line_number++; //we still have to increment the line number, 
                                           //even though the two lines are equal
      
      }
      fclose(dat1);
      fclose(dat2);
    
    }
    The output is kind of strange: For example if I put these three lines in one file
    Simon
    Says
    Go Home

    and I put the same three in the other one I get something like this:
    Code:
     These lines don't match:
     imon AND Simon  at line number :1
     ays  AND  Says    at line number:2
     o Home AND Go Home at line number:3
    Looks like that the first character at the first file is always cut off. Why?
    I also want for program to compare all the lines until the end of the file. Any suggestions?

    Thanks!

  2. #2
    Registered User
    Join Date
    Apr 2005
    Posts
    67
    It looks like it only compares line1 with itself, but cutting off first character...Im confused. Can someone try to input their files, thanks.

  3. #3
    ---
    Join Date
    May 2004
    Posts
    1,379
    You should be allocating space for these.
    Code:
    char line1[]="";
    char line2[]="";
    Code:
    #define BUFSIZ 80
    
    char *line1;
    line1 = malloc(sizeof(char)*BUFSIZ);

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    Several things to add, as sand_man said you need to allocate space for line1 and line2. Alternatively you could just make it as an array of character of size BUFSIZ. You should also ideally check for fopen and fclose (not done here) return values. To indicate if opening the file was successful or not. To go through the file without limiting the line numbers, you can simply use the return value of fgets(). Simply terminate the loop whenever the first file hits EOF (indicated by a return in NULL of fgets()). You can also fix up some things such as "magic numbers", but this code basically shows how to loop through the file:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    int main (int argc, char *argv[]) {
    
      char line1[BUFSIZ]; //here wil be stored the lines of a first file
      char line2[BUFSIZ]; //..of the second one
      int line_number=0;
    
      FILE *dat1, *dat2;
    
      if(argc<3){
        printf("You have to input two arguments!\n");
        exit(1);
      }
    
      dat1=fopen(argv[1],"r"); //opening each file for reading
      if (dat1 == NULL)
            exit(1);
    
      dat2=fopen(argv[2],"r");
      if (dat2 == NULL)
            exit(1);
    
      while( ((fgets(line1, 80, dat1)) != NULL) && (fgets(line2, 80, dat2)) != NULL)
      {
    
        if(strcmp(line1,line2)!=0) //compare two strings, if they are not equal...
          {
            printf("These lines don't match:\n");
            line_number++;
            printf("%s   AND %s \t at line number %d\n",line1,line2,line_number);
          }
      }
    
      fclose(dat1);
      fclose(dat2);
    
      return 0;
    }
    The cost of software maintenance increases with the square of the programmer's creativity.

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    67
    Thanks Orion and Sand_man!! Helped a lot

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    67
    I have yet a little question. Where is BUFSIZ declared? And does it mean 80? Cause the above code that Orion modified compiled ok. Ill try if it works.

  7. #7
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Quote Originally Posted by blackswan
    I have yet a little question. Where is BUFSIZ declared? And does it mean 80? Cause the above code that Orion modified compiled ok. Ill try if it works.
    You should check stdio.h, BUFSIZ usually means 512...
    Probably you'll find something like this #define BUFSIZ 512
    But be careful, C Standard says BUFSIZ is at least 256 and does not specify exact value...

    - Micko
    Last edited by Micko; 04-26-2005 at 04:17 PM.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  8. #8
    Registered User
    Join Date
    Apr 2005
    Posts
    67
    Veseli Bosanac hvala za obrazložitev!
    I like the Incredibles too!

  9. #9
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    You're welcome komsija
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  10. #10
    Registered User
    Join Date
    Apr 2005
    Posts
    67
    Orion, thanks for modifications, the code works, you only forgot to increase the line number, if the lines are the same. THanks for help!

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. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. Reading file into edit - losing new lines
    By eam in forum Windows Programming
    Replies: 3
    Last Post: 11-08-2003, 01:07 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM