Thread: Sorting data retrieved from input file

  1. #1
    Registered User
    Join Date
    Jul 2003

    Sorting data retrieved from input file


    Can anyone get me started with a code example of how I to sort the data I pulled from my input file, I don't want to modiy the input file I just want to sort what I got from it and The number of strings can get very very large . I just want a code example to get me started.

    The program opens and reads data from a text file the program then finds the string its looking and processes it, but here's the problem the string may show up more then once in the data file but I don't want the program to process it twice. So if it finds the same string again during it's search through the data file it should disregards it.


  2. #2
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    try this:
    #include <stdio.h>
    #include <string.h>
    #define FILENAME	"/home/lynux/test.txt" //file location
    #define MAX			256				// MAX number of chars per line
    int main()
    	FILE* fp;
    	char buf[MAX];
    	char* mystring = "search string here\n";
    	int lines=0;
    	fp = fopen(FILENAME,"r");
    			printf("%s found on line %d\n",mystring,lines);
    			//return 0;
    			//remove the above comments if you want to disregard the rest of the search
    	return 0;
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  3. #3
    Registered User
    Join Date
    Jul 2003


    thx for your reply.

    But the problem with the code example you provided is that my porgram can't do a serach while I'm searching for the strings in the input file, I need a piece of code that that can search and disregard strings that are the "same" after the program is done looking for all the strings from the input file.

    Can I use sort -u would that work?
    Can I store them in char * list[] after the search is done from the input file and then do a strcmp or use a sort algorithm?

    kind regards,

  4. #4
    Registered User
    Join Date
    Jul 2003
    Another approach would be to use a data structure that sorts its elements as it is constructed, and discards duplicate information as a result of the sorting. In essence, giving you the sort and duplicate removal for free. This data structure should provide efficient lookup for your processing, and it should be easy to implement, especially if it is the first time you have used it. There is a data structure that does all of this, and that is a binary search tree. If you already know what they are, then it would be an excellent time to use one. If you don't know what they are, then it would be an excellent time to learn them. "The C Programming Language Second Edition" by Kernighan and Ritchie provides a simple illustration of one starting on page 139. Coincidentally, it solves a problem very similar to the one you describe. "The Practice of Programming" by Kernighan and Pike describe them in Chapter 2, and also provide working implementations of these and other popular data structures that you may not know yet. Not to mention, there is an abundance of information around the net.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you input data from a file into an array?
    By jorgejags in forum C Programming
    Replies: 5
    Last Post: 11-03-2008, 02:48 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. reading a columns input data file
    By vk13wp in forum C Programming
    Replies: 6
    Last Post: 04-28-2003, 01:32 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM