Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 716
  • Last Modified:

How do I read a text file, sort the contents alphabetically and write the result to a text file

Hi
I am doing a correspondance C course and this question has got me stumped. I need to read a text file, sort the contents alphabetically and write the result to a text file using 3 functions:
1 - to read the input text file into an array
2 - sort the array using bubble sort algorithm
3 - write sorted array out to a text file

I have been trying to solve this for the last two days but keep going around in circles, I have tried using a function like char**readintext(void) and then use fgets to read in the text but I am having trouble with my pointers. I know I should be using malloc somewhere but just cannot get it working.

Any help appreciated thanks.
int main()   // Function main begins program execution 
{
	
	char **line;	// pointer to an array of strings
    lines=readin(); //call function
 
	//printf("/n%s",lines);
 
	
    return 0;
    
} // End function main 
 
 
char** readin(void)
{
	int lineNum = 0;
	char **line;		// pointer to an array of strings	
 
	
    // cfPtr = file pointer 
    FILE *cfPtr;
	char string[50];
 
 
     cfPtr = fopen("V:text.txt", "r");
    if(!cfPtr) {
        printf("Couldn't open file");
 
    }
    while(fgets(string, 50, cfPtr)) {
 
	printf("%s", string);
 
	
	//*lines = string;
	
  
    }
    fclose(cfPtr);
    return lines;
}

Open in new window

0
carrig01
Asked:
carrig01
1 Solution
 
CSecurityCommented:
this C++ code sorts alphabetically so nice but doesn't read/write in files. if you want in C++, i can alter the code in a way that you desire ;)
[about code: gives some string from user till user input -1, then sorts all input data and print them all.]
regards...
#include<iostream.h>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
vector <string> vec;
string token="";
cin>>token;
while(token!="-1"){
vec.push_back(token);
cin>>token;
}
sort(vec.begin(),vec.end());
for(size_t i=0;i<vec.size();i++){
cout<<endl<<i<<" : "<<vec[i];
}
int i ;cin>>i;
return 0;
}

Open in new window

0
 
oleberCommented:
reading the file, and printing it.

you have already a malloc example, you will need some more malloc to collect the lines and later you will need to implement the sort.

at the end, you will need to free all the memory.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
 
#define BUFFER_LENGTH 50
 
char* read_line(FILE *cfPtr) {
    char* line = NULL;
    char buffer[BUFFER_LENGTH];
    while ( fgets(buffer, BUFFER_LENGTH, cfPtr) != NULL ) {
        if ( line == NULL ) {
            line = (char*) malloc(strlen( buffer ) + 1);
            strcpy( line, buffer );
        } else {
            int line_length = strlen( line );
            char* tmp = (char*) malloc( strlen( buffer ) + line_length + 1 );
            strcpy( tmp, line );
            strcpy( tmp + line_length, buffer );
            free(line);
            line = tmp;
        }
        
        if ( buffer[ strlen( buffer ) - 1 ] == '\n' ) {
            return line;
        }
    }    
    return line;
}
 
char** read_file(char *file_path) {
    int lineNum = 0;
    char **lines;            // pointer to an array of strings       
 
    // cfPtr = file pointer 
    FILE *cfPtr = fopen(file_path, "r");
    if ( cfPtr == NULL ) {
        printf("Couldn't open file");
        return NULL;
    }
 
    char *line = NULL;
    while ( ( line = read_line(cfPtr) ) != NULL ) {
        printf("%d -> %s", strlen( line ), line);
        free(line);
    }
 
    fclose(cfPtr);
 
    return lines;
}
 
 
int main()   // Function main begins program execution 
{
    char **lines = read_file("code.c"); //call function
 
        
    return 0;
    
} // End function main 

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now