Writing non-duplicates

This is a discussion on Writing non-duplicates within the C Programming forums, part of the General Programming Boards category; I have a file called Junk2.txt which looks like this when opened. 1 2 2 3 4 5 5 5 ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    25

    Writing non-duplicates

    I have a file called Junk2.txt which looks like this when opened.
    1
    2
    2
    3
    4
    5
    5
    5
    The file is already sorted from smallest to largest. The program that I have now can write all of the integers in the Junk2.txt file in reverse order, which is the first thing it needs to do, so all I have to do now is display it without the duplicate integers. So what should be displayed is
    1
    2
    3
    4
    5
    Can someone please point me in the right direction. Thanks for any help...It's very much appreciated. Here is the code I have written so far.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    	FILE *file;
    	char filename[300];
    
    	printf("Give file name:");
    	scanf("%s",filename);
    
    	file=fopen(filename,"r");
    	if(file==NULL)
    	{
    		perror(NULL);
    		return 1;
    	}
    
    	largeToSmall(file);
        fclose(file);
    	return 0;
    }
    
    largeToSmall(FILE *file)
    {
    	int count;
    	count=fgetc(file);
    	
    	if(count==EOF) 
    		return;
    	
    	largeToSmall(file);
    	putchar(count);
    }

  2. #2
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    you could have to varibles, one for the current character and one for the last. then you would just have to see if they match. or you could but it in a array and scan it.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    25

    Array format

    I think I could weed out the duplicates by using an array, but how could the code be changed so that it places each character into an array?

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    i just now notcied that your numbers are in order, using a array for it doesnt seem tight now. i tried to do it, but mine wont work right

    Code:
    int getfile(char *file) {
        FILE *data;
        data = fopen(file, "r");
        
        int current;
        int last;
        
        if(data == NULL) {
            perror(NULL);
            return 1;
        } else {
            while( (current = fgetc(data)) != EOF ) {
                if(current != last)
                    putchar(current);
                    
                last = current;
            }
        }
        
        fclose(data);
        
        return 0;
    }
    
    int main (){
        char filename[256];
        
        printf("Give file name:");
        scanf("%s",filename);
     
        getfile(filename);
    
        return 0;
    }
    any one see why?

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You don't really need an array, I'd suggest a method like mart has showed.

    mart: You have to remember that fgetc() will also receive newline characters as well as numbers I'll leave you to fix it... (and last isn't initialised the first time through the loop)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    You have to remember that fgetc() will also receive newline characters as well as numbers
    the little things kill me everytime................

    heres version 2
    Code:
    #include <stdio.h>
    
    int getfile(char *file) {
        FILE *data;
        data = fopen(file, "r");
        
        char current[2];
        int last = 0;
        
        if(data == NULL) {
            perror(NULL);
            return 1;
        } else {
            while( (fscanf(data, "%s",  current)) != EOF ) {
                    if(current[0] != last) {
                        printf("%c\n", current[0]);
                        last = current[0];
                    }
            }
        }
        
        fclose(data);
        
        return 0;
    }
    
    int main (){
        char filename[256];
        
        printf("Give file name:");
        scanf("%s",filename);
     
        getfile(filename);
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-20-2008, 08:57 AM
  2. arrays vs lists? And containers in general!
    By clegs in forum C++ Programming
    Replies: 22
    Last Post: 12-03-2007, 01:02 PM
  3. Very slow file writing of 'fwrite' function in C
    By scho in forum C Programming
    Replies: 6
    Last Post: 08-03-2006, 02:16 PM
  4. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  5. help! fifo read problem
    By judoman in forum C Programming
    Replies: 1
    Last Post: 08-16-2004, 09:19 AM

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