Thread: Writing non-duplicates

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

    Writing non-duplicates

    I was wondering if someone could give me some assistance with my program. What I have right now is a file called 'duplicates.txt' which contains the following integers I maually entered:
    1
    1
    2
    3
    3
    3
    4
    5

    I can read the file and write the file, the problem is I need to only write one of each number to the written file called 'non-duplicates.txt'. I don't know where to start with this. Here is the code that I currently have. Any help would be appreciated.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	FILE *file;
    	char buffer[10];
    
    	file = fopen("duplicates.txt", "r");
    	
    	while (fgets(buffer, sizeof(buffer), file))
    		printf("%s", buffer);
    	return(0);
    	fclose(file);
    
    	file = fopen("non-duplicates.txt", "w");
    	fwrite(buffer, sizeof(buffer), 10, file);	
    	fclose(file);
      
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You need to *remember* the numbers as you read them. Various methods are available including binary trees and arrays. Binary trees are more robust but are more complex to code... arrays are simpler, but are of fixed size and can be slow to search through. As you're a beginner, I'd suggest sticking with array for now though...
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

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, 02: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