Bubble sort text file

This is a discussion on Bubble sort text file within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm a beginner at programming and would appreciate some help with this. I need to open a text file ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    13

    Bubble sort text file

    Hi, I'm a beginner at programming and would appreciate some help with this.
    I need to open a text file and then organize it alphabetically. So far I can open the file, and I know how to do a simple bubble sort, I'm just not sure how to pit the two of them together. Opening the file:
    Code:
    #include <stdio.h>
    void main ()
    {
    	char file_name[30];
    	FILE *file_ptr;
    	printf("Enter the name of the file to open >");
    	scanf("%s",file_name);
    
    file_ptr = fopen(file_name, "r");
    	if (file_ptr != NULL)
    		printf("File open OK\n");
    	else
    		printf("Error opening file %s\n", file_name);
    }
    And a the bubble sort would be something like:
    Code:
    void bubble_sort()   
    
     {   
    
         int temp;       
    
           
    
         for (int i = 0; i < size - 1; i++)         
    
             for (int j = 0; j < size - 1; j++)         
    
             {             
    
                 if (A[j] > A[j+1])             
    
                 {                 
    
                     temp = A[j];   
    
                     A[j] = A[j+1];  
    
                     A[j+1] = temp;  
    
                  }        
    
              }  
     }
    I just dont know how to go about implementing the bubble sort function once the file is open. I'm guessing it has to do with reading the data into an array but I dont know where to go from here. Any help would be greatly appreciated.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,524
    Read your file into A. A should be a type of array that defines operator >. Then, sort A with a working sort.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    13
    Thanks for the reply, I'm not the best at this though. How do I go about placing the data in the text file into the array?

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,524
    It depends on how the data in the file is arranged, really. I suggest you make your best effort, and if you get stuck, post your code again, along with any compiler messages, and an example file, so that I have something to fix.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    13
    Hey Whiteflags,

    This is a kind of Frankenstein of crappy college notes I find it difficult to make sense of:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void bubble_sort(FILE *ifp, FILE *ofp, int list[], int n);
    
    int main(int argc, char **argv)
    
    {
    	char file_name[30];
    	FILE *file_ptr, *file_ptra;
    	printf("Enter the name of the file to open >");
    	scanf("%s",file_name);
    
    	file_ptr = fopen(file_name[1], "r");
    	file_ptra = fopen(file_name[2], "w");
    	if (file_ptr != NULL)
    		printf("File open OK\n");
    	else
    		printf("Error opening file %s\n", file_name);
    
    	bubble_sort(FILE *ifp, FILE *ofp, int list[], int n);
    	fclose(ifp);
    	fclose(ofp);
    	return 0;
    
    }
    void bubble_sort(FILE *ifp, FILE *ofp, int list[], int n)
    {
    	int j, k;
    	boolean exchange_made;
    	int temp;
    	k = 0;
    	exchange_made = TRUE;
    // Make up to n - 1 passes through 
    	// array, exit early if no exchanges
    	// are made on previous pass
    	while ((k < n - 1) && exchange_made)
    	{
    		exchange_made = FALSE;
    		++k;
    	// Number of comparisons on kth pass
    		for (j = 0; j < n - k; ++j)
    		if (list[j] > list[j + 1])
    			{
    			// Exchange must be made
    				temp = list[j];	     	
    				list[j] = list[j + 1];
    				list[j + 1] = temp;
    				exchange_made = TRUE;
    				print_list(list, n);
    			}
    	}
    }
    I know its a complete mess, but basically I want to be able to open the file to change it, so I'm guessing the means I need "r" and "w", so once I've opened the file, I want to call my bubble sort function, alphabetise the text in the text file and create a new file. I know its a complete mess and I'm miles away from getting it, but even if you could point me in the right direction I'd really appreciate it. I hate our notes. Thanks again.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I suppose you should pick a language (this is the C++ board, the code you have is C). You can't open the same file twice at the same time. You will need to open the file for reading, read the file and store the data, close the file, sort the data, open the file for writing (which will in the process erase the file) and write the sorted data.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bubble Sort from Input File
    By creilly in forum C Programming
    Replies: 11
    Last Post: 10-12-2010, 01:31 AM
  2. Sort by Character Length, Text File
    By glj12 in forum C Programming
    Replies: 12
    Last Post: 01-23-2008, 05:04 PM
  3. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 02:17 PM
  4. Replies: 15
    Last Post: 10-31-2005, 07:29 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM

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