Issue
I want to do the following:
develop c program in such a way that in qemu terminal, executable named sort should be created.
when given "sort sample.txt", it should take contents of the sample and sort the text inside it in ascending order.
create an output file and dump this output.
I did like this :
#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\sample\xv6-public\data.txt";
char *strFileSummary = "C:\Users\sample\xv6-public\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;
}
'''
But, how to add it to qemu executables?
Solution
You are copying data around like crazy, even reallocating the strings with MAX_LENGTH
! that's not necessary at all. How can you forget what pointers are!
char* temporary;
for(i= 0; i < (noOfLines - 1); ++i) {
for(j = 0; j < ( noOfLines - i - 1); ++j) {
if(strcmp(strData[j], strData[j+1]) > 0) {
temporary=strData[j];
strData[j]=strData[j+1];
strData[j+1]=temporary;
}
}
}
I can't help you with the qemu part, isn't there a way to build it to the required architecture and putting in the PATH
?
Answered By - Sudip Bhattarai
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.