I'm trying to make a program that will ask the user to which file they want to sort, sort it, then output the sorted text into a user specified file. the text file will contain names and the program should make lowercase and uppercase letter the same.

#include<stdio.h>
#include<stdlib.h>      
                
#define LEN 80
#define MAX_NAME 40
                
void EnterFile(char file[MAX_NAME][LEN]);
void ReadingFile(char file[MAX_NAME][LEN]);
int CompareStrings(char name1[LEN], char name2[LEN]);
void SwapStrings(char name1[LEN], char name2[LEN]);
void BubbleSort( char file[MAX_NAME][LEN]);
void OutputFile(char file2[MAX_NAME][LEN]);
                 
                  
        void EnterFile(char file[MAX_NAME][LEN]){
                FILE *pef;
                           
                printf("enter the name of the file to sort:");
                scanf("%s", &file);
                
                pef=fopen("file", "r");
                        if (pef == NULL) {
                         printf("I couldn't open %s.\n", file);
                         exit(0);}
                }
                        
        void ReadingFile(char file[MAX_NAME][LEN]){
                FILE *pef;
                pef=fopen("file", "r");
        
                char name;
 while(fscanf(pef, "%s", name ) != NULL)
                fclose(pef);
                }  
                

        int CompareStrings(char s1[LEN], char s2[LEN]){

                int i;

                for(i=0; i<LEN; i++)
                  {  if (s1[i]>s2[i])
                        { return(1); }
                     if (s1[i]<s2[i])
                        { return(-1); }
                     if (s1[i] == 0 || s2[i] == 0)
                        { break;}
                  }
                }
                
        void SwapStrings(char name1[LEN], char name2[LEN]) {
                int i;
                char c;
                 
                for(i=0; i<LEN; i++)
                  { c = name1[i];
                    name1[i] = name2[i];
                    name2[i] = c;
                  }
                }
                     
        void BubbleSort( char file[MAX_NAME][LEN]){

                int p, i;
                
                for(p=1; p<MAX_NAME; p++)
                  { for(i=0; i<MAX_NAME-1; i++)
                    { if (CompareStrings(file[i], file[i+1]) > 0)
                        {SwapStrings(file[i], file[i+1]);
                          }
                        }
                   }
                }
                  
                        
        void OutputFile(char file2[MAX_NAME][LEN]) {
                FILE *fp;
                int i;
                char name;
                   
                printf("enter the name of the output file:\n");
                scanf("%s", &file2);
                fp=fopen("%d", "a");
                
                printf( "Can't open %s", file2);
                 
                for(i=0; i<=LEN; ++i){
                  for(i=0; i< MAX_NAME; i++)
                  {print("%s", file2[i]);}
                  }
                }  
                 
                     
        int main() {
 int main() {
                
        char file[LEN][MAX_NAME];
                
        EnterFile(file);
        ReadingFile(file);
        CompareStrings(name);
        BubbleSort(name);  
        OutputFile(name);
                    
        return 0;
        }

Recommended Answers

All 5 Replies

You forgot to mention the problem that you are facing with your project assignment, without that it is not possible to help .

You forgot to mention the problem that you are facing with your project assignment, without that it is not possible to help .

I'm not sure how to combine the use of bubble sorting and c I/O functions. After opening the file, I don't know how to read the contents.

I'm not sure how to combine the use of bubble sorting and c I/O functions. After opening the file, I don't know how to read the contents.

Do you have a book? If so, there's a chapter on I/O (input and output). Might want to read that. if you don't have a book, you need to get one.

god forbid someone ask a legitimate question here without getting told to RTFM. i see now how Walt has so many 'solved' threads.

one problem is youre putting your filenames in quotes. otherwise, its pretty simple, just use "fopen" and "fgets" in a this manner.

printf("enter filename: ");    
    fgets(filename, 120, stdin);
    filename[strlen(filename) - 1] = 0;

    if ( (fptr = fopen (filename, "r")) != NULL)
    {
         while (fgets (buffer, 30, fptr) != NULL)
         {
              buffer[strlen(buffer) - 1] = 0;
            
              <etc...>

at which point you will now have the first line from your file in the string "buffer', and notice that the newline is being replaced by a NULL character on line 9

some assumptions are being made ... importantly, that there will never be a single line in your file that is larger than 30 chars in length.

if you need to read longer lines, change the argument in fgets (and size 'buffer') accordingly

I just thought i'd point out this nugget of wisdom

I'm not sure how to combine the use of bubble sorting and c I/O functions. After opening the file, I don't know how to read the contents.

Do you have a book? if you don't have a book, you need to get one.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.