AmperSand
AmperSand

Reputation: 71

How to sort data inside .txt files in C

I am new to C programming. Right now I'm writing a program in C which reads .txt files and stores the data into another txt file. For example:

open 20150101.txt

then get the data inside

2015010103I
2015010102O

then store it in 2015JAN.txt

Currently I'm having problems in sorting the contents of the .txt file. Could you help me?

int intCtr;
int intCtr2;
int intCtr3;
char strTempData[MAX_SIZE];

FILE * ptrFileLog; 
ptrFileLog = fopen(strFileName, "r"); 

while(fgets(strTRLog, MAX_SIZE, ptrFileLog) != NULL) {

FILE * ptrSummary;

ptrSummary = fopen(strFileSummary, "a");

for(intCtr = 0; intCtr < MAX_SIZE; intCtr++) {
    strcpy(strTempCopy[intCtr], strTRLog);
}

for(int intCtr = 0; intCtr < MAX_SIZE; intCtr++) {

    for(int intCtr2 = 6; intCtr2 < 7; intCtr2++) {
        if(strcmp(strTempCopy[intCtr -1], strTempCopy[intCtr]) > 0) {
            strcpy(strTempData, strTempCopy[intCtr]);
            strcpy( strTempCopy[intCtr], strTempCopy[intCtr - 1]);
            strcpy(strTempCopy[intCtr -1], strTempData);
        }
    }
}

for(int intCtr = 0; intCtr < 1; intCtr++) {
    fputs(strTempCopy[intCtr], ptrSummary);
}
  }
  fclose(ptrFileLog);   
 fclose(ptrSummary);

Upvotes: 2

Views: 24443

Answers (2)

DieMartin
DieMartin

Reputation: 76

First of all you should recheck the flow of your code. For instance your are opening strFileSummary several times (inside while loop) and closing it only once at the end. The use of some for loops are at least intriguing.

Also, It is not clear to me exactly what kind of sorting algorithm you are trying to implement there. My recommendation is not to reinvent the wheel and use stdlib's qsort function. You should only have to code a comparison function. See an example here: Using stdlib's qsort() to sort an array of strings

Upvotes: 0

Sridhar Nagarajan
Sridhar Nagarajan

Reputation: 1105

To solve this, i would recommend reading line by line and storing it in a string list. and sorting the list using any sorting algorithms( example here: bubble sort). and print the result in a new file. dont open a file inside while loop is not a good idea. In some senarious you may end up loosing the handler to the opened file.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX_LEN 100 // Length of each line in input file.

int main(void)
{
    char *strFileName = "C:\\Users\\sridhar\\untitled4\\data.txt";
    char *strFileSummary = "C:\\Users\\sridhar\\untitled4\\out.txt";
    char strTempData[MAX_LEN];
    char **strData = NULL; // String List
    int i, j;
    int noOfLines = 0;

    FILE * ptrFileLog = NULL;
    FILE * ptrSummary = NULL;

    if ( (ptrFileLog = fopen(strFileName, "r")) == NULL ) {
        fprintf(stderr,"Error: Could not open %s\n",strFileName);
        return 1;
    }
    if ( (ptrSummary = fopen(strFileSummary, "a")) == NULL ) {
        fprintf(stderr,"Error: Could not open %s\n",strFileSummary);
        return 1;
    }

    // Read and store in a string list.
    while(fgets(strTempData, MAX_LEN, ptrFileLog) != NULL) {
        // Remove the trailing newline character
        if(strchr(strTempData,'\n'))
            strTempData[strlen(strTempData)-1] = '\0';
        strData = (char**)realloc(strData, sizeof(char**)*(noOfLines+1));
        strData[noOfLines] = (char*)calloc(MAX_LEN,sizeof(char));
        strcpy(strData[noOfLines], strTempData);
        noOfLines++;
    }
    // Sort the array.
    for(i= 0; i < (noOfLines - 1); ++i) {
        for(j = 0; j < ( noOfLines - i - 1); ++j) {
            if(strcmp(strData[j], strData[j+1]) > 0) {
                strcpy(strTempData, strData[j]);
                strcpy(strData[j], strData[j+1]);
                strcpy(strData[j+1], strTempData);
            }
        }
    }
    // Write it to outfile. file.
    for(i = 0; i < noOfLines; i++)
        fprintf(ptrSummary,"%s\n",strData[i]);
    // free each string
    for(i = 0; i < noOfLines; i++)
        free(strData[i]);
    // free string list.
    free(strData);
    fclose(ptrFileLog);
    fclose(ptrSummary);
    return 0;
}

Upvotes: 4

Related Questions