I have the program below that reads numbers from a text file and then sorts them
ID: 3827599 • Letter: I
Question
I have the program below that reads numbers from a text file and then sorts them in an output file. I need to add a bit of code that will count the number of numbers in each the input and output text files and display this information in the format shown below.
Enter filename: testfile . txt
Input file testfile . txt contains 999 numbers
Out put file testfile _out.txt contains 999 numbers
#include <stdio.h>
#include <stdlib.h>
#define MAX_FILE_NAME 100
struct node{
float data;
struct node *next;
}*start;
void sortedInsert(float val){
struct node *newNode = (struct node *)malloc(sizeof(struct node *));
newNode->data = val;
struct node *current;
if (start == NULL || start->data >= newNode->data){
newNode->next = start;
start = newNode;
}
else{
current = start;
while (current->next != NULL && current->next->data < newNode->data){
current = current->next;
}
newNode->next = current->next;
current->next = newNode;
}
}
int main(){
char *input = (char *)malloc(sizeof(char) * 20);
char *output = (char *)malloc(sizeof(char) * 20);
printf("Enter the input filename: ");
scanf("%s", input);
sprintf(output, "%s_out", input);
FILE *fp;
while(!(fp = fopen(input, "r"))){
printf("Input file does not exist. Enter another filename: ");
scanf("%s", input);
}
float item;
while(fscanf(fp, "%f", &item) != EOF){
sortedInsert(item);
}
fclose(fp);
struct node *iterator = start;
FILE *fpo = fopen(output, "w");
while(iterator != NULL){
fprintf(fpo, "%f ", iterator->data);
iterator = iterator->next;
}
printf("Please check the output file %s ", output);
free(input);
free(output);
iterator = start;
while(iterator != NULL){
struct node *temp = iterator;
iterator = iterator->next;
free(temp);
}
}
Explanation / Answer
#include<stdio.h>
#include<malloc.h>
#include<string.h>
struct node{
float data;
struct node *next;
}*start;
void sortedInsert(float val){
struct node *newNode = (struct node *)malloc(sizeof(struct node *));
newNode->data = val;
struct node *current;
if (start == NULL || start->data >= newNode->data){
newNode->next = start;
start = newNode;
}
else{
current = start;
while (current->next != NULL && current->next->data < newNode->data){
current = current->next;
}
newNode->next = current->next;
current->next = newNode;
}
}
int main(){
char *input = (char *)malloc(sizeof(char) * 20);
char *output = (char *)malloc(sizeof(char) * 20);
int x=0,y=0;
int i=0;
printf("Enter filename: ");
scanf("%s", input);
FILE *fp;
while(!(fp = fopen(input, "r"))){
printf("Input file does not exist. Enter another filename: ");
scanf("%s", input);
}
float item;
while(fscanf(fp, "%f", &item) != EOF){
sortedInsert(item);
x++;
}
fclose(fp);
printf("Input file %s contains %d numbers",input,x);
i=strlen(input)-4;
input[i]='';
sprintf(output, "%s_out.txt", input);
struct node *iterator = start;
FILE *fpo = fopen(output, "w");
while(iterator != NULL){
fprintf(fpo, "%f ", iterator->data);
iterator = iterator->next;
y++;
}
//printf("Please check the output file %s ", output);
printf(" Output file %s contains %d numbers",output,y);
free(input);
free(output);
iterator = start;
while(iterator != NULL){
struct node *temp = iterator;
iterator = iterator->next;
free(temp);
}
}
The modified code is highlighted. Two variables x and y are used to count the number of numbers in the file. And, .txt word is removed from the input file name to give a proper name to the output filename. And the required print statements are added.