program ltrieuse; {$mode objfpc}{$H+} uses {$IFDEF UNIX}{$IFDEF UseCThreads} cthreads, {$ENDIF}{$ENDIF} Classes, SysUtils, CustApp { you can add units after this }; type { TMyApplication } TMyApplication = class(TCustomApplication) protected procedure DoRun; override; public constructor Create(TheOwner: TComponent); override; destructor Destroy; override; procedure WriteHelp; virtual; end; { TMyApplication } procedure TMyApplication.DoRun; var ErrorMsg: String; Liste1 : TStringList; // l'objet liste Infile, // nom du fichier entree Outfile : string; // nom du fichier sortie begin // quick check parameters ErrorMsg:=CheckOptions('h','help'); if ErrorMsg<>'' then begin ShowException(Exception.Create(ErrorMsg)); Halt; end; // parse parameters if HasOption('h','help') then begin WriteHelp; Halt; end; { add your program here } if ParamCount < 1 then writeln ('Un fichier en entree SVP'); if ParamCount >= 1 then begin Infile := ExtractFileName(ParamStr(1)); // recupere le nom du fichier entree writeln(' Fichier en entree : ', Infile); // ecrit sur la console writeln (' Debut de lecture : ',TimeToStr(Time)); If paramcount = 2 then Outfile := ExtractFileName(ParamStr(2)) // recupere le nom fichier sortie else Outfile := 'TRI'+Infile; // oubien le cree Liste1 := TStringList.Create; //construire l'objet liste try // utilisation de la liste dans un bloc d'interception des erreurs Liste1.LoadFromFile(infile); // ici on lit le fichier d'entree dans la liste writeln (' Fin de lecture : ',TimeToStr(Time)); writeln (' mombre de lignes : ',IntToStr(Liste1.count)); Liste1.Sort; // on trie la liste writeln (' Fin de tri : ',TimeToStr(Time)); Liste1.SaveToFile(Outfile); // et on la sauve dans le fichier de sortie finally // fin du bloc Liste1.free; // liberer la liste writeln (' Fichier de sortie : ', outfile); writeln (' Fin ecriture : ',TimeToStr(Time)); end; end; // stop program loop Terminate; end; constructor TMyApplication.Create(TheOwner: TComponent); begin inherited Create(TheOwner); StopOnException:=True; end; destructor TMyApplication.Destroy; begin inherited Destroy; end; procedure TMyApplication.WriteHelp; begin { add your help code here } writeln('Usage: ',ExeName,' -h'); end; var Application: TMyApplication; begin Application:=TMyApplication.Create(nil); Application.Title:='Ltrieusue'; Application.Run; Application.Free; end.
... GGGGATGTAGCTCAGATCGTATGCCGTCTTCTGCT[\n] GGGGATGTAGCTCAGATCGTATGCCGTCTTCTGCT[\n] GGGGATGTAGCTCAGATCGTATGCCGTCTTCTGCT[\n]
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> char* GetTime(char* lpBuffer, long nSize) { time_t nCurrentTime; struct tm* timeInfo; time(&nCurrentTime); timeInfo = localtime(&nCurrentTime); strftime(lpBuffer, nSize, "%H:%M:%S", timeInfo); return lpBuffer; } FILE *SafeOpenFile(char *lpFileName, char *lpMode, FILE **lpFile) { *lpFile = fopen(lpFileName, lpMode); if (! *lpFile) printf("Cannot open file: "%s"\n", lpFileName); return *lpFile; } long GetSizeOfFile(FILE* lpFile) { long nCurrentPos; long nResult; nCurrentPos = ftell(lpFile); fseek(lpFile, 0, SEEK_END); nResult = ftell(lpFile); fseek(lpFile, nCurrentPos, SEEK_SET); return nResult; } void** SetSize(void** lpArray, long nTypeSize, long nSize) { long* lpLongArray; long nCapacity; lpLongArray = (long*)*lpArray; if (! nSize) { if (lpLongArray) { free(&lpLongArray[-2]); lpLongArray = NULL; } } else { if (! lpLongArray) { lpLongArray = (long*)malloc(2 * sizeof(long) + nTypeSize * nSize); if (lpLongArray) { lpLongArray[0] = nSize; lpLongArray[1] = nSize; lpLongArray = &lpLongArray[2]; } } else { /* Si agrandissement */ if (lpLongArray[-1] <= nSize) { /* Si la capacité est suffisante */ if (lpLongArray[-2] >= nSize) lpLongArray[-1] = nSize; else { /* On double la capacité et on vérifie que c'est suffisant */ nCapacity = 2 * lpLongArray[-2]; if (nCapacity < nSize) nCapacity = nSize; lpLongArray = (long*)malloc(2 * sizeof(long) + nCapacity * nTypeSize); if (lpLongArray) { lpLongArray[0] = nCapacity; lpLongArray[1] = nSize; lpLongArray = &lpLongArray[2]; memcpy(lpLongArray, *lpArray, ((long*)(*lpArray))[-1] * nTypeSize); free(&((long*)(*lpArray))[-2]); } } } else { /* On ne désalloue que si c'est vraiment utile */ if (nSize * 2 > lpLongArray[-2]) lpLongArray[-1] = nSize; else { /* Allocation d'un espace plus petit et recopie du début */ lpLongArray = (long*)malloc(2 * sizeof(long) + nSize * nTypeSize); if (lpLongArray) { lpLongArray[0] = nSize; lpLongArray[1] = nSize; lpLongArray = &lpLongArray[2]; memcpy(lpLongArray, *lpArray, nSize * nTypeSize); free(&((long*)(*lpArray))[-2]); } } } } } *lpArray = lpLongArray; return lpArray; } long GetSize(void* lpArray) { long* lpLongArray; long nResult; nResult = 0; lpLongArray = (long*)lpArray; if (lpLongArray) nResult = lpLongArray[-1]; return nResult; } int Compare(const void* a, const void* b) { char* lpStr1; char* lpStr2; long nI; lpStr1 = *(char**)a; lpStr2 = *(char**)b; nI = 0; while ((lpStr1[nI] != '\n') && (lpStr1[nI] != '\r') && (lpStr2[nI] != '\n') && (lpStr2[nI] != '\r') && (lpStr1[nI] == lpStr2[nI])) nI++; return lpStr1[nI] - lpStr2[nI]; } int main() { FILE* lpInputFile; FILE* lpOutputFile; long nInputSize; char* lpFileContent; char* lpResultContent; char** lpSortingArray; char lpTime[20]; int bWasCrOrLf; long nI; long nJ; long nK; int nResult; nResult = 1; printf("Debut de lecture : %s\n", GetTime(lpTime, 20)); if (! SafeOpenFile("Input.txt", "rb", &lpInputFile)) goto the_end; if (! SafeOpenFile("TRIinput.txt", "wb", &lpOutputFile)) goto close_input; nInputSize = GetSizeOfFile(lpInputFile); lpFileContent = (char*)malloc(nInputSize + 1); if (! lpFileContent) { printf("Unable to allocate input buffer\n"); goto close_output; } if (fread(lpFileContent, 1, nInputSize, lpInputFile) != nInputSize) { printf("Unable to read input file\n"); goto free_content; } lpFileContent[nInputSize] = 0; /* Remplissage d'un tableau de pointeur sur les chaînes */ lpSortingArray = NULL; bWasCrOrLf = 1; for (nI = 0; nI < nInputSize; nI++) { if ((lpFileContent[nI] != '\n') && (lpFileContent[nI] != '\r')) { if (bWasCrOrLf) { SetSize((void**)&lpSortingArray, 4, GetSize(lpSortingArray) + 1); lpSortingArray[GetSize(lpSortingArray) - 1] = &lpFileContent[nI]; bWasCrOrLf = 0; } } else bWasCrOrLf = 1; } printf("Fin de lecture : %s\n", GetTime(lpTime, 20)); printf("Nombre de lignes : %ld\n", GetSize(lpSortingArray)); /* Tri */ qsort(lpSortingArray, GetSize(lpSortingArray), sizeof(char*), Compare); printf("Fin de tri : %s\n", GetTime(lpTime, 20)); /* Génération du fichier de sortie en mémoire */ lpResultContent = (char*)malloc(nInputSize + 1); if (! lpResultContent) { printf("Unable to allocate output buffer\n"); goto free_content; } nK = 0; for (nI = 0; nI < GetSize(lpSortingArray); nI++) { bWasCrOrLf = 0; nJ = 0; while (1) { if ((lpSortingArray[nI][nJ] == '\r') || (lpSortingArray[nI][nJ] == '\n')) bWasCrOrLf = 1; else if (bWasCrOrLf) break; lpResultContent[nK] = lpSortingArray[nI][nJ]; nK++; nJ++; } } lpResultContent[nInputSize] = 0; if (fwrite(lpResultContent, 1, nInputSize, lpOutputFile) != nInputSize) { printf("Unable to write output file\n"); goto free_result; } printf("Fin ecriture : %s\n", GetTime(lpTime, 20)); nResult = 0; free_result: free(lpResultContent); free_content: free(lpFileContent); close_output: fclose(lpOutputFile); close_input: fclose(lpInputFile); the_end: return nResult; }
il renvoit pas de segmentation fault.
lpResultContent = (char*)malloc(nInputSize + 1); if (! lpResultContent) { printf("Unable to allocate output buffer\n"); goto free_content; }
#!/bin/perl use strict; use warnings; my $input='/tmp/Input.txt'; my $output='output.txt'; my $time = localtime(); print "Début de lecture : $time\n"; open(F, "<$input") or die "can't open file $input\n"; my @tags; my $n=0; while (<F>) { chomp; push @tags, $_; $n++; } $time = localtime(); print "Fin de lecture : $time\n"; print "Nombre de lignes :$n\n"; open(FO, ">$output") or die "can't write in file $output"; foreach (sort @tags) { print FO $_,"\n"; } $time = localtime(); print "Fin d'écriture : $time\n";
# time nice -n -20 sort -S500M toto -o /dev/null real 0m45.331s user 0m44.763s sys 0m0.488s
# time nice -n -20 ./cs Debut de lecture : 00:02:33 Fin de lecture : 00:02:34 Nombre de lignes : 4000000 Fin de tri : 00:02:41 Fin ecriture : 00:02:42 real 0m9.009s user 0m8.409s sys 0m0.512s
# time nice -n -20 ./sortfile_simple toto /dev/null real 0m9.127s user 0m8.737s sys 0m0.360s
# time nice -n -20 ./sortfile -v toto /dev/null Reading from 'toto'... Read 4000000 lines. Sorting... Writting output to '/dev/null'... Stats: comparisons: 82698297 read time: 0.870s sort time: 5.761s write time: 1.528s Cleaning... real 0m8.195s user 0m7.768s sys 0m0.364s
# time nice -n -20 ./sortfile -vt2 toto /dev/null Reading from 'toto'... Read 4000000 lines. Sorting with 2 threads... Writting output to '/dev/null'... Stats: comparisons: 84705684 read time: 0.885s sort time: 3.464s write time: 1.669s Cleaning... real 0m6.051s user 0m8.789s sys 0m0.384s
# time nice -n -20 ./sortfile --zero --mmap -vt2 toto0 /dev/null Reading from 'toto0'... Read 4000000 lines. Sorting with 2 threads... Writting output to '/dev/null'... Stats: comparisons: 84705684 read time: 0.731s sort time: 3.497s write time: 1.681s Cleaning... real 0m5.930s user 0m8.957s sys 0m0.124s
#ifndef _GNU_SOURCE # define _GNU_SOURCE #endif #include <stdlib.h> #include <stdio.h> #include <errno.h> #include #include <getopt.h> #include <string.h> static int verbose = 0; /* The string comparison function */ int (*str_compare)(const char *, const char *) = strcmp; static char ** read_lines(FILE *in, size_t *length) { char **lines, **tmp_lines; size_t nlines = 0; char *buf = NULL, *buf_cpy; size_t buflen = 0; size_t i; ssize_t r; if (*length < 1) *length = 1000; lines = malloc(*length * sizeof(char *)); if (lines == NULL) { perror("malloc"); return NULL; } while (1) { r = getline(&buf, &buflen, in); if (r >= 0) { // Remove the trailing \n or \r\n if (r >2 && buf[r - 2] '\r' && buf[r - 1] == '\n') { buf[r - 2] = '\0'; r -= 2; } else if (r >1 && buf[r - 1] '\n') { buf[r - 1] = '\0'; r--; } if (r == 0) { continue; } // Expand the array if (*length == nlines) { *length += 1000; tmp_lines = realloc(lines, *length * sizeof(char *)); if (tmp_lines == NULL) { perror("realloc"); for (i = 0; i < nlines; i++) { free(lines[i]); } free(lines); free(buf); return NULL; } lines = tmp_lines; } // Copy the line from the getline buffer buf_cpy = malloc(r + 1); if (buf_cpy == NULL) { perror("malloc"); for (i = 0; i < nlines; i++) { free(lines[i]); } free(lines); free(buf); return NULL; } memcpy(buf_cpy, buf, r + 1); lines[nlines] = buf_cpy; nlines++; } else { break; } } *length = nlines; free(buf); return lines; } static void free_lines(char **lines, size_t len) { size_t i; for (i = 0; i < len; i++) { free(lines[i]); } free(lines); } static int write_lines(FILE *out, char **lines, size_t len) { size_t i; int r; for (i = 0; i < len; i++) { r = fprintf(out, "%s\n", lines[i]); if (r < 0) { perror("fprintf"); return -1; } } return 0; } // Basic comparison with str_compare static int cmp_lines(const void *p1, const void *p2) { return str_compare(*(char * const *) p1, *(char * const *) p2); } static void usage(const char *name) { fprintf(stderr, "Usage: %s [OPTION]... input_file output_file\n\n", name); fprintf(stderr, "Options:\n"); fprintf(stderr, " -h, --help " "this help message\n"); fprintf(stderr, " -c, --coll " "use 'strcoll' to order the lines\n"); fprintf(stderr, " -V, --vers " "use 'strverscmp' to order the lines\n"); fprintf(stderr, " -v, --verbose " "print progress messages to stderr\n\n"); fprintf(stderr, "Use "-" instead of the input or output file to use " "respectively stdin or stdout.\n"); fprintf(stderr, "The default comparison function is 'strcmp'.\n"); } int main(int argc, char *argv[]) { int exit_status = EXIT_SUCCESS; int opt; const char *in_filename, *out_filename; FILE *in_file, *out_file; char ** lines; size_t lines_length = 0; const char *prog_name = argv[0]; struct option long_options[] = { { "help", 0, NULL, 'h' }, { "verbose", 0, NULL, 'v' }, { "coll", 0, NULL, 'c' }, { "vers", 0, NULL, 'V' }, { 0, 0, 0, 0 } }; while ((opt = getopt_long(argc, argv, "hcVv", long_options, NULL)) >= 0) { switch (opt) { case 'v': verbose = 1; break; case 'h': usage(prog_name); exit(EXIT_SUCCESS); break; case 'c': str_compare = strcoll; break; case 'V': str_compare = strverscmp; default: usage(prog_name); exit(EXIT_FAILURE); break; } } argv += optind; argc -= optind; if (argc != 2) { usage(prog_name); exit(EXIT_FAILURE); } in_filename = argv[0]; out_filename = argv[1]; if (strcmp(in_filename, "-") != 0) { in_file = fopen(in_filename, "r"); if (in_file == NULL) { perror("fopen"); exit(EXIT_FAILURE); } } else { in_file = stdin; in_filename = "stdin"; } if (strcmp(out_filename, "-") != 0) { out_file = fopen(out_filename, "w"); if (out_file == NULL) { perror("fopen"); exit(EXIT_FAILURE); } } else { out_file = stdout; out_filename = "stdout"; } if (verbose) fprintf(stderr, "Reading from '%s'...\n", in_filename); lines = read_lines(in_file, &lines_length); if (lines == NULL) { exit_status = EXIT_FAILURE; goto close_end; } if (verbose) { fprintf(stderr, " Read %zd lines.\n", lines_length); } if (verbose) fprintf(stderr, "Sorting...\n"); qsort(lines, lines_length, sizeof(char *), cmp_lines); if (verbose) fprintf(stderr, "Writting output to '%s'...\n", out_filename); if (write_lines(out_file, lines, lines_length) < 0) { exit_status = EXIT_FAILURE; goto free_end; } free_end: if (verbose) fprintf(stderr, "Cleaning...\n"); free_lines(lines, lines_length); close_end: fclose(in_file); fclose(out_file); return exit_status; }