obout FILE PROCESSING???

This is a discussion on obout FILE PROCESSING??? within the C Programming forums, part of the General Programming Boards category; my program is running, but logically wrong.. Assume that there are two text files. write a program that will determine ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    23

    obout FILE PROCESSING???

    my program is running, but logically wrong..

    Assume that there are two text files. write a program that will determine whether these two text files are identical or not..

    it doesn't important if the program print the content of each program.. the important is it compare the content of the first text file to another text file... and print only whether they're equal or not

    help me please and give me some advice....

    heres my program..

    Code:
    #include<stdio.h>
    void main(void)
    {
    FILE *inputf1;
    FILE *inputf2;
    int x,y;
    char filename1[40];
    char filename2[40];
    
    
    printf("Input name of the text file:\n");
    scanf("%s",&filename1);
    printf("Input another text filename:\n");
    scanf("%s",&filename2);
    if(filename1!=filename2)
    printf("they're not equal\n");
    
    if((inputf1 = fopen(filename1, "r")) == NULL)
    if((inputf2 = fopen(filename2, "r")) == NULL)
    {
    printf("ERROR: %s cannot be opened:\n",filename1);
    printf("ERROR: %s cannot be opened too:\n",filename2);
    getch();
    exit(1);
    }
    while((x = fgetc(inputf1)) !=EOF)
    while((y = fgetc(inputf2)) !=EOF)
    printf("%c",x);
    printf("%c",y);
    
    fclose(inputf1);
    fclose(inputf2);
    }
    so many Thanks for all of your help...

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Perhaps you can see your error now that I have taken the time to properly indent your code (well, as best it can be, I guess, without altering any logic).

    Code:
    #include<stdio.h>
    void main(void)
    {
    	FILE *inputf1;
    	FILE *inputf2;
    	int x,y;
    	char filename1[40];
    	char filename2[40];
    
    	printf("Input name of the text file:\n");
    	scanf("&#37;s",&filename1);
    	printf("Input another text filename:\n");
    	scanf("%s",&filename2);
    	if(filename1!=filename2) printf("they're not equal\n");
    
    	if((inputf1 = fopen(filename1, "r")) == NULL) if((inputf2 = fopen(filename2, "r")) == NULL)
    		{
    			printf("ERROR: %s cannot be opened:\n",filename1);
    			printf("ERROR: %s cannot be opened too:\n",filename2);
    			getch();
    			exit(1);
    		}
    	while((x = fgetc(inputf1)) !=EOF) while((y = fgetc(inputf2)) !=EOF) printf("%c",x);
    	printf("%c",y);
    
    	fclose(inputf1);
    	fclose(inputf2);
    }
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    23
    so whats the changes??? i do'nt see any changes their sir... anyway thanks...
    Last edited by lesrhac03; 03-27-2008 at 09:27 PM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by lesrhac03 View Post
    so whats the changes??? i do'nt see any changes their sir... anyway thanks...
    Since he explicitly stated that he did not change anything, I don't expect you to see any changes. However, you should see a line or three that look so incredibly amazingly wrong that you can't miss them. And since those lines are the same as your code, those are errors in your code as well.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    23
    sorry sir.. it was so hard for me to understand it.. the alignment of the codes and what code to be use in order to accomplish the given requirements...

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
    #include<stdio.h>
    void main(void) /* should be int main(void) - read FAQ */
    {
    	FILE *inputf1;
    	FILE *inputf2;
    	int x,y;
    	char filename1[40];
    	char filename2[40];
    
    	printf("Input name of the text file:\n");
    	scanf("&#37;s",&filename1); /* drop the & */
    	printf("Input another text filename:\n");
    	scanf("%s",&filename2);/* drop the & */
    	if(filename1!=filename2) printf("they're not equal\n"); /* use strcmp to compare strings*/
    
    	if((inputf1 = fopen(filename1, "r")) == NULL) 
    		if((inputf2 = fopen(filename2, "r")) == NULL)
    		{
    			printf("ERROR: %s cannot be opened:\n",filename1);
    			printf("ERROR: %s cannot be opened too:\n",filename2);
    			getch();
    			exit(1);
    		}
    /* why do you need 2 handles if you want filenames to be equal? */
    /* what if only one file failed to open? */
    	while((x = fgetc(inputf1)) !=EOF) 
    		while((y = fgetc(inputf2)) !=EOF) 
    			printf("%c",x); /* so you write the first char from the first file number of times
    equal to number of chars in the second file
    each other char you do not print at all
    
    what's the point?
    */
    	printf("%c",y);/* y is EOF - what is the point? */
    
    	fclose(inputf1);
    	fclose(inputf2);
    	/* return 0; - would be proper ending */
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    if(filename1!=filename2) printf("they're not equal\n");
    This compares the filenames (wrongly, use strcomp) and not the contents of the files.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Reading strings with scanf is prone to buffer overruns. See:
    Need help with altering info on a txt file
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Code:
    if((inputf1 = fopen(filename1, "r")) == NULL) 
    		if((inputf2 = fopen(filename2, "r")) == NULL)
    Er? So you only open the second file if the first failed?

    Banish yourself to the design board, this could be done with a flow chart in a few minutes. Seriously, It's like comparing strings, except you're comparing the contents of files. Compare the bytes (characters if you will), while they match AND you're not at the end of either file then the file's are identical.

    ie,
    Code:
    1 Open file A
    2 Open file B
    3 Read a byte from file A
    4 Read a byte from file B
    5 Compare the byte from A with the byte from B, if they match loop from step 3.
    6 Close A
    7 Close B
    You should be able to fill in the missing steps, checking for EOF and whatever.
    Last edited by zacs7; 03-28-2008 at 04:00 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. file processing updating record error
    By uuser in forum C Programming
    Replies: 2
    Last Post: 04-27-2003, 12:13 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

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